package ink.wgink.login.base.service.impl;

import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.login.base.consts.IUserCenterConst;
import ink.wgink.login.base.manager.ConfigManager;
import ink.wgink.login.base.service.IOAuthService;
import ink.wgink.properties.BaseProperties;
import ink.wgink.properties.ServerProperties;
import ink.wgink.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: OAuthServiceImpl
 * @Description: Oauth认证
 * @Author: wanggeng
 * @Date: 2021/7/30 10:57 上午
 * @Version: 1.0
 */
@Service
public class OAuthServiceImpl implements IOAuthService {

    @Autowired
    private ServerProperties serverProperties;
    @Autowired
    private BaseProperties baseProperties;

    @Override
    public Map<String, String> getPageParams(HttpServletRequest request) {
        // session中设置refer验证token
        String referHeaderToken = UUIDUtil.getUUID();
        request.getSession().setAttribute(IUserCenterConst.REFER_TOKEN, referHeaderToken);

        Map<String, String> config = ConfigManager.getInstance().getConfig();
        Map<String, String> pageParams = new HashMap<>(16);
        pageParams.put(IUserCenterConst.REFER_TOKEN, referHeaderToken);

        Object errorMessage = request.getSession().getAttribute(IUserCenterConst.ERROR_MESSAGE);
        Object loginUsername = request.getSession().getAttribute(IUserCenterConst.LOGIN_USERNAME);
        pageParams.put(IUserCenterConst.ERROR_MESSAGE, errorMessage == null ? null : errorMessage.toString());
        pageParams.put(IUserCenterConst.LOGIN_USERNAME, loginUsername == null ? null : loginUsername.toString());
        // 服务地址
        pageParams.put(IUserCenterConst.SERVER_URL, serverProperties.getUrl());
        // 系统LOGO
        pageParams.put(IUserCenterConst.SYSTEM_LOGO, config.isEmpty() ? null : config.get(IUserCenterConst.SYSTEM_LOGO));
        // 背景图片
        pageParams.put(IUserCenterConst.LOGIN_BACKGROUND_IMAGES, config.isEmpty() ? null : config.get(IUserCenterConst.LOGIN_BACKGROUND_IMAGES));
        // 系统标题
        pageParams.put(IUserCenterConst.SYSTEM_TITLE, config.isEmpty() ? null : config.get(IUserCenterConst.SYSTEM_TITLE));
        // 系统标题大小，默认26
        pageParams.put(IUserCenterConst.SYSTEM_TITLE_SIZE, config.isEmpty() ? null : config.get(IUserCenterConst.SYSTEM_TITLE_SIZE));
        // 系统子标题
        pageParams.put(IUserCenterConst.SYSTEM_SUB_TITLE, config.isEmpty() ? null : config.get(IUserCenterConst.SYSTEM_SUB_TITLE));
        // 系统子标题大小，默认16
        pageParams.put(IUserCenterConst.SYSTEM_SUB_TITLE_SIZE, config.isEmpty() ? null : config.get(IUserCenterConst.SYSTEM_SUB_TITLE_SIZE));
        // 版权年份
        pageParams.put(IUserCenterConst.COPY_RIGHT_YEAR, config.isEmpty() ? null : config.get(IUserCenterConst.COPY_RIGHT_YEAR));
        // 版权所属
        pageParams.put(IUserCenterConst.COPY_LEFT, config.isEmpty() ? null : config.get(IUserCenterConst.COPY_LEFT));
        // 官方链接
        pageParams.put(IUserCenterConst.OFFICIAL_URL, config.isEmpty() ? null : config.get(IUserCenterConst.OFFICIAL_URL));
        // 开启验证码
        pageParams.put(ISystemConstant.VERIFICATION_CODE, config.isEmpty() ? null : config.get(ISystemConstant.VERIFICATION_CODE));
        // 登录框位置
        pageParams.put(IUserCenterConst.LOGIN_BOX_POSITION, config.isEmpty() ? null : config.get(IUserCenterConst.LOGIN_BOX_POSITION));
        // 登录表单action
        pageParams.put(IUserCenterConst.LOGIN_FORM_ACTION, baseProperties.getLoginProcess().startsWith("/") ? baseProperties.getLoginProcess().substring(1) : baseProperties.getLoginProcess());
        return pageParams;
    }
}
