package com.qmth.distributed.print.business.service.impl;

import com.qmth.boot.tools.signature.SignatureEntity;
import com.qmth.boot.tools.signature.SignatureType;
import com.qmth.distributed.print.business.service.SsoService;
import com.qmth.teachcloud.common.config.DictionaryConfig;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicSchool;
import com.qmth.teachcloud.common.entity.SysOrg;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.enums.RoleTypeEnum;
import com.qmth.teachcloud.common.enums.userPush.SpecialPrivilegeEnum;
import com.qmth.teachcloud.common.service.CommonCacheService;
import com.qmth.teachcloud.common.service.SysOrgService;
import com.qmth.teachcloud.common.service.SysUserService;
import com.qmth.teachcloud.common.sync.CloudMarkingTaskUtils;
import com.qmth.teachcloud.common.util.ServletUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Date: 2021/5/20.
 */
@Service
public class SsoServiceImpl implements SsoService {
    private final static Logger log = LoggerFactory.getLogger(SsoServiceImpl.class);

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    CloudMarkingTaskUtils stmmsUtils;

    @Resource
    DictionaryConfig dictionaryConfig;

    @Resource
    CommonCacheService commonCacheService;

    @Resource
    SysOrgService sysOrgService;

    @Override
    public Map<String, Object> markerLoginInfo() {
        try {
            SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
            SpecialPrivilegeEnum userSpecialPrivilege = sysUserService.findUserSpecialPrivilegeByUserId(sysUser.getId());
            if (SpecialPrivilegeEnum.UNIDENTIFIED.equals(userSpecialPrivilege)
                    || SpecialPrivilegeEnum.SUBJECT_HEADER.equals(userSpecialPrivilege)) {
                throw ExceptionResultEnum.ERROR.exception("该用户没有评卷员角色，无法登录");
            }
            return stmmsUtils.markLogin(sysUser);
        } catch (Exception e) {
            throw ExceptionResultEnum.ERROR.exception(e.getMessage());
        }
    }

    @Override
    public Map<String, Object> markerLeaderLoginInfo() {
        try {
            SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
            SpecialPrivilegeEnum userSpecialPrivilege = sysUserService.findUserSpecialPrivilegeByUserId(sysUser.getId());
            if (SpecialPrivilegeEnum.UNIDENTIFIED.equals(userSpecialPrivilege)
                    || SpecialPrivilegeEnum.MARKER.equals(userSpecialPrivilege)) {
                throw ExceptionResultEnum.ERROR.exception("该用户没有科组长角色，无法登录");
            }
            return stmmsUtils.markLeaderLogin(sysUser);
        } catch (Exception e) {
            throw ExceptionResultEnum.ERROR.exception(e.getMessage());
        }
    }

    /**
     * 教研分析单点登录
     *
     * @param loginName
     * @param role
     * @param orgName
     * @param realName
     * @param mobileNumber
     * @param enable
     * @param returnUrl
     * @return
     * @throws IOException
     */
    @Override
    public Map<String, Object> analysisLogin(String loginName, RoleTypeEnum role, String orgName, String realName, String mobileNumber, Boolean enable, String returnUrl) throws IOException {
        SysUser sysUser = (SysUser) ServletUtil.getRequestUser();
        BasicSchool basicSchool = commonCacheService.schoolCache(sysUser.getSchoolId());
        List<SysOrg> sysOrgList = sysOrgService.findByConnectByParentId(sysUser.getOrgId(), false, true);
        if (CollectionUtils.isEmpty(sysOrgList)) {
            throw ExceptionResultEnum.ERROR.exception("未找到学院信息");
        }

        Map<String, Object> map = new HashMap<>();
        map.computeIfAbsent("loginName", v -> loginName);
        map.computeIfAbsent("role", v -> role);
        map.computeIfAbsent("orgName", v -> sysOrgList.get(0).getName());
        map.computeIfAbsent("realName", v -> realName);
        map.computeIfAbsent("mobileNumber", v -> mobileNumber);
        map.computeIfAbsent("returnUrl", v -> returnUrl);

        Long timestamp = System.currentTimeMillis();
        String accessToken = SignatureEntity.build(SignatureType.SECRET, SystemConstant.METHOD, dictionaryConfig.reportOpenDomain().getSsoLoginApi(), timestamp, basicSchool.getAccessKey(), basicSchool.getAccessSecret());
        map.computeIfAbsent("time", v -> timestamp);
        map.computeIfAbsent("authorization", v -> accessToken);

        String hostUrl = dictionaryConfig.reportOpenDomain().getHostUrl();
        Optional.ofNullable(hostUrl).orElseThrow(() -> ExceptionResultEnum.ERROR.exception("未配置教研分析host"));

        if (hostUrl.contains("*")) {
            hostUrl = hostUrl.replace("*", basicSchool.getCode());
        }
        map.put("redirectUrl", hostUrl + dictionaryConfig.reportOpenDomain().getSsoLoginApi());
        return map;
    }
}
