package com.hr.externelSystem;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hr.dto.ApiLogDto;
import com.hr.dto.GeetestConfigDTO;
import com.hr.dto.GeetestDTO;
import com.hr.param.GeestParam;
import com.hr.repository.domain.IpInterfacePO;
import com.hr.repository.domain.IpOrderPO;
import com.hr.repository.domain.IpTokenInterfacePO;
import com.hr.util.HmacSHA256;
import com.hr.util.HttpUtils;
import com.hr.vo.CallbackResponseVO;
import com.hr.vo.GeestVO;
import com.hr.vo.ServiceTypeVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.security.SignatureException;
import java.util.HashMap;
import java.util.Map;

/**
 * 极验验证 服务
 */
@Service
@Slf4j
public class GeestService implements BizHandler<GeestParam, GeestVO> {
    @Override
    public ServiceTypeVO getType() {
        return new ServiceTypeVO("Geest", "极验一键登录");
    }

    @Override
    public GeestVO handle(GeestParam geestParam, IpInterfacePO ipInterfacePO, ApiLogDto logDto) {
        GeestVO vo = new GeestVO();
        vo.setCode(500);
        Map<String, Object> requestMap = new HashMap<>();
        long systemTimeMills = System.currentTimeMillis();
        String mobile = null;
        requestMap.put("process_id", geestParam.getProcessId());
        String interfaceConfig = ipInterfacePO.getInterfaceConfig();
        GeetestConfigDTO geetestConfigDTO = JSON.parseObject(interfaceConfig, GeetestConfigDTO.class);

        String geestAppId = geetestConfigDTO.getAppId();
        String geestAppKey = geetestConfigDTO.getAppKey();
        String url = geetestConfigDTO.getUrl();

        log.info("使用的geestAppId: {}, 使用的geestAppKey: {}", geestAppId, geestAppKey);
        String originText = geestAppId + "&&" + systemTimeMills;
        String appKey = geestAppKey;
        try {
            String sign = HmacSHA256.sha256_HMAC(originText, appKey);
            requestMap.put("sign", sign);
            requestMap.put("token", geestParam.getToken());
            requestMap.put("timestamp", systemTimeMills);
            Map<String, String> head = new HashMap<>();
            head.put("Content-Type", "application/json");
            String json = JSON.toJSONString(requestMap);
            log.info("极验请求参数:{}",json);
            logDto.setUrl(url);
            logDto.setRequestParam(json);
            HttpResponse httpResponse = HttpUtils.doPost(url, null, null, head, null,json);
            String responseStr = EntityUtils.toString(httpResponse.getEntity());
            logDto.setResponseParam(responseStr);
            log.info("极验验证结果:{}",responseStr);
            GeetestDTO dto = JSON.parseObject(responseStr, GeetestDTO.class);
            if(dto.getStatus() != null && dto.getStatus() == 200){
                mobile = dto.getResult();
                vo.setCode(200);
                vo.setMobile(mobile);
            }
            if(dto.getStatus()!= null){
                logDto.setHttpStatus(dto.getStatus().toString());
                if(dto.getStatus() == 200){
                    mobile = dto.getResult();
                    vo.setCode(200);
                    vo.setMobile(mobile);
                }
            }
            logDto.setErrorMessage(dto.getError_msg());
        } catch (SignatureException e) {
            log.error("极验--解密失败",e);
            vo.setErrMsg("解密失败");
        } catch (Exception e) {
            log.error("极验--调用服务器失败失败",e);
            vo.setErrMsg("调用服务器失败");
        }
        return vo;
    }

    @Override
    public Class getRequestClass() {
        return GeestParam.class;
    }

    @Override
    public CallbackResponseVO doCallBack(IpOrderPO ipOrderPO, JSONObject callbackParam) {
        return null;
    }
}
