package com.fdkankan.platform.user.service.sso;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.LoginType;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.exception.BusinessException;
import com.fdkankan.common.user.SSOLoginHelper;
import com.fdkankan.common.util.Base64Converter;
import com.fdkankan.common.util.PasswordUtils;
import com.fdkankan.platform.order.entity.SpaceSdk;
import com.fdkankan.platform.order.service.ISpaceSdkService;
import com.fdkankan.platform.user.entity.Manager;
import com.fdkankan.platform.user.request.RequestManager;
import com.fdkankan.platform.user.request.RequestSdkVersion;
import com.fdkankan.platform.user.service.IManagerService;
import com.fdkankan.platform.user.vo.SSOManagerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SsoManagerService {

    @Autowired
    private IManagerService managerService;
    @Autowired
    private ISpaceSdkService spaceSdkService;
    @Autowired
    private SSOLoginHelper ssoLoginHelper;

    public JSONObject login(RequestManager param) {
        SSOManagerVo managerEntity = getVoByUserName(param.getUserName());
        if (managerEntity == null){
            throw new BusinessException(ErrorCode.USER_NOT_EXIST);
        }
        if(managerEntity.getIsCompany() != 0){
            throw new BusinessException(ErrorCode.NOT_PERMISSION);
        }
      return this.loginCommon(param,managerEntity);
    }

    public JSONObject companyLogin(RequestManager param) {
        SSOManagerVo managerEntity = getVoByUserName(param.getUserName());
        if (managerEntity == null){
            throw new BusinessException(ErrorCode.USER_NOT_EXIST);
        }
        if(managerEntity.getIsCompany() != 1){
            throw new BusinessException(ErrorCode.NOT_PERMISSION);
        }
        return this.loginCommon(param,managerEntity);
    }

    private JSONObject loginCommon(RequestManager param, SSOManagerVo managerEntity){
        //对前端传的密码解密
        String password = Base64Converter.decode(Base64Converter.subText(param.getPassword()));
        param.setPassword(password);
        String encryptPwd = PasswordUtils.encrypt(param.getUserName(), param.getPassword(), PasswordUtils.getStaticSalt());
        if (!encryptPwd.equals(managerEntity.getPassword())){
            throw new BusinessException(ErrorCode.PASSWORD_ERROR);
        }
        String token = ssoLoginHelper.login(LoginType.MANAGER.code(), managerEntity.getUserName());
        JSONObject obj = new JSONObject();
        obj.put("token", token);
        return obj;
    }

    public String logout(String token) {
        ssoLoginHelper.logout(token);
        return ServerCode.SUCCESS.message();
    }

    public Page<SpaceSdk> spaceList(RequestSdkVersion param) {
        return spaceSdkService.getPageByParam(param);
    }

    private SSOManagerVo getVoByUserName(String userName){
        SSOManagerVo vo = null;
        Manager manager = managerService.getByUserName(userName);
        if(manager!=null){
            vo = new SSOManagerVo();
            BeanUtils.copyProperties(manager,vo);
        }
        return vo;
    }
}
