package com.sptg.permissions.biz.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sptg.permissions.api.conf.CustInfoVerifyConfig;
import com.sptg.permissions.api.entity.CustInfoResponse;
import com.sptg.permissions.api.entity.CustInfoVerify;
import com.sptg.permissions.api.feigns.CustInfoVerifyFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequiredArgsConstructor
@Slf4j
public class CustInfoVerifyController implements CustInfoVerifyFeign {


    @Override
    @PostMapping(value = "/custInfoVerify/verify")
    public boolean verify(@RequestBody CustInfoVerify custInfoVerify) {
        String certNumber = custInfoVerify.getCertNumber();
        String custName = custInfoVerify.getCustName();
        if(!StringUtils.hasText(certNumber) || !StringUtils.hasText(custName)){
            Assert.isTrue(false, "身份证和姓名不能为空");
        }
        //姓名、身份证号去首尾空格
        custInfoVerify.setCertNumber(certNumber.trim());
        custInfoVerify.setCustName(custName.trim());
        RestTemplate restTemplate = new RestTemplate();
//        HttpHeaders headers = new HttpHeaders();
        String uuid = UUID.randomUUID().toString();
        Map<String, String> headers = new HashMap<>();
        CustInfoVerifyConfig config = new CustInfoVerifyConfig();
        headers.put("X-APP-ID", config.getAppId());
        headers.put("X-APP-KEY", config.getAppKey());
        headers.put("X-CTG-Request-ID",uuid);
        headers.put("Content-Type", "text/xml;charset=utf-8");
        Map<String, Object> logMap = new HashMap<>();
        logMap.put("header", headers);
        logMap.put("body", custInfoVerify);
//        HttpEntity<String> requestEntity = new HttpEntity<>(JSONUtil.toJsonStr(custInfoVerify), headers);
        ParameterizedTypeReference<CustInfoResponse> reference = new ParameterizedTypeReference<CustInfoResponse>() {};
        String url = config.getHost();
        URI uri = URI.create(url);
        logMap.put("url", uri);
        try {
            log.warn("=====================身份证验证请求数据=========================");
            String bodyString = JSONUtil.toJsonStr(custInfoVerify);
            log.warn(bodyString);
            String result = HttpUtil.createPost(uri.toString()).addHeaders(headers).body(bodyString).execute().body();
            log.warn("=====================身份证验证响应数据=========================");
            log.warn(result);
            JSONObject data = JSONUtil.parseObj(result);
            if("0".equals(data.getStr("code"))){
                return true;
            }else{
                return false;
            }
        }catch (Exception e){
            e.printStackTrace();
            log.warn("=====================身份证验证失败=========================");
            log.warn(e.getMessage());
            return false;
        }
    }
}
