package com.jsowell.thirdparty.platform.service.impl;

import com.alibaba.fastjson2.JSON;
import com.google.common.collect.Lists;
import com.jsowell.common.enums.thirdparty.ThirdPlatformTypeEnum;
import com.jsowell.common.util.JWTUtils;
import com.jsowell.common.util.StringUtils;
import com.jsowell.pile.service.ThirdPartyPlatformConfigService;
import com.jsowell.pile.thirdparty.CommonParamsDTO;
import com.jsowell.pile.vo.ThirdPartySecretInfoVO;
import com.jsowell.thirdparty.lianlian.vo.AccessTokenVO;
import com.jsowell.thirdparty.platform.service.ThirdPartyPlatformService;
import com.jsowell.thirdparty.platform.factory.ThirdPartyPlatformFactory;
import com.jsowell.thirdparty.platform.util.Cryptos;
import com.jsowell.thirdparty.platform.util.ThirdPartyPlatformUtils;
import com.jsowell.thirdparty.service.ThirdpartySecretInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ZhongDianLianPlatformServiceImpl implements ThirdPartyPlatformService {
    // 平台类型
    private final String thirdPlatformType = ThirdPlatformTypeEnum.NING_BO_PLATFORM.getTypeCode();

    @Autowired
    private ThirdPartyPlatformConfigService thirdPartyPlatformConfigService;

    @Autowired
    private ThirdpartySecretInfoService thirdpartySecretInfoService;

    @Override
    public void afterPropertiesSet() throws Exception {
        ThirdPartyPlatformFactory.register(thirdPlatformType, this);
    }

    @Override
    public void printServiceName() {
        // System.out.println("当前类名：" + this.getClass().getSimpleName());
    }

    /**
     * query_token 获取token，提供给第三方平台使用
     *
     * @param dto
     * @return
     */
    @Override
    public Map<String, String> queryToken(CommonParamsDTO dto) {
        AccessTokenVO vo = new AccessTokenVO();
        // 0:成功；1:失败
        int succStat = 0;
        // 0:无；1:无此对接平台；2:密钥错误； 3～99:自定义
        int failReason = 0;

        String operatorId = StringUtils.isNotBlank(dto.getOperatorID()) ? dto.getOperatorID() : dto.getPlatformID();
        // 通过operatorId 查出 operatorSecret
        ThirdPartySecretInfoVO thirdPartySecretInfoVO = thirdpartySecretInfoService.queryByOperatorId(operatorId);
        if (thirdPartySecretInfoVO == null) {
            failReason = 1;
            succStat = 1;
        } else {
            String theirOperatorSecret = thirdPartySecretInfoVO.getTheirOperatorSecret();
            String dataSecret = thirdPartySecretInfoVO.getOurDataSecret();
            String dataSecretIv = thirdPartySecretInfoVO.getOurDataSecretIv();
            // 解密data 获取参数中的OperatorSecret
            String decrypt = Cryptos.decrypt(dto.getData(), dataSecret, dataSecretIv);
            String inputOperatorSecret = null;
            if (StringUtils.isNotBlank(decrypt)) {
                inputOperatorSecret = JSON.parseObject(decrypt).getString("OperatorSecret");
                if (StringUtils.isBlank(inputOperatorSecret)) {
                    inputOperatorSecret = JSON.parseObject(decrypt).getString("PlatformSecret");
                }
            }
            // 对比密钥
            List<String> operatorSecretList = Lists.newArrayList(theirOperatorSecret, thirdPartySecretInfoVO.getOurOperatorSecret());
            if (!operatorSecretList.contains(inputOperatorSecret)) {
                failReason = 1;
                succStat = 1;
            } else {
                // 生成token
                String token = JWTUtils.createToken(operatorId, theirOperatorSecret, JWTUtils.ttlMillis);
                vo.setAccessToken(token);
                vo.setTokenAvailableTime((int) (JWTUtils.ttlMillis / 1000));
            }
        }
        // 组装返回参数
        vo.setPlatformId(operatorId);
        vo.setFailReason(failReason);
        vo.setSuccStat(succStat);

        Map<String, String> resultMap = ThirdPartyPlatformUtils.generateResultMap(vo, thirdPartySecretInfoVO);
        return resultMap;
    }

}
