package com.yc.ship.module.trade.utils;

import com.alibaba.fastjson.JSONObject;
import com.anji.captcha.util.MD5Util;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.http.HttpUtils;
import com.yc.ship.module.infra.api.config.ConfigApi;
import com.yc.ship.module.trade.api.insurance.dto.InsuranceApplyReqDTO;
import com.yc.ship.module.trade.api.insurance.dto.InsuranceCancelReqDTO;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception0;

/**
 * 阳光保险接口工具类
 */
@Slf4j
@Component
public class InsuranceUtilBak {

    //测试环境
//    public final String HOST = "https://lbb-admin.anyitech.ltd"; //测试域名
//
//    public final String VALIDATE_URL = "/policy/validateJson.do"; // 请求校验接口
//
//    public final String RECEIVE_URL = "/policy/receiveJson.do"; // JSON投/退保接口
//
//    public final String QUERY_URL = "/policy/querystatus.do"; // 保单状态查询接口
//
//    public final String APPID = "123456";   // appId
//
//    public final String KEY = "goldpalm"; // key

    //  正式环境

    public final String HOST = "http://www.admin.airiskeys.com"; //测试域名

    public final String VALIDATE_URL = "/policy/validateJson.do"; // 请求校验接口

    public final String RECEIVE_URL = "/policy/receiveJson.do"; // JSON投/退保接口

    public final String QUERY_URL = "/policy/querystatus.do"; // 保单状态查询接口

    public final String DOWNLOAD_URL = "/policy/downloadpdf.do"; // 保单下载接口

    public final String APPID = "449ddc34";   // appId

    public final String KEY = "2HVEZB4Z"; // key

    private final Integer VALIDATE_LEVEL = 0; //校验级别，其值有 0/1

    @Resource
    private ConfigApi configApi;

    /**
     * REST模板
     */
    private RestTemplate restTemplate;

    /**
     * 构造函数
     */
    public InsuranceUtilBak() {
        this.restTemplate = new RestTemplate();
    }



    /**
     * 发送投保请求
     * 将投保请求发送到阳光系统
     *
     * @param request 投保请求
     * @return 投保响应
     */
    public CommonResult sendInsuranceApply(InsuranceApplyReqDTO request) {
        try {
            String reqJson = JSONObject.toJSONString(request);
            String sign = SignUtil.generateMD5Sign(APPID, reqJson, KEY);

            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
            params.add("appId", APPID);
            params.add("req", reqJson);
            params.add("sign", sign);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

            HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);

            log.info("发送投保请求到阳光系统: {}", HOST + RECEIVE_URL);
            log.info("请求参数: appId={}, req={}, sign={}", APPID, reqJson, sign);

            // 发送请求并获取响应
            ResponseEntity<byte[]> response = restTemplate.exchange(
                    HOST + RECEIVE_URL,
                    HttpMethod.POST,
                    entity,
                    byte[].class
            );

            log.info("阳光系统响应状态码: {}", response.getStatusCode());

            // 手动处理响应字节流，确保使用UTF-8编码
            byte[] responseBytes = response.getBody();
            String responseBody = null;
            if (responseBytes != null) {
                try {
                    responseBody = new String(responseBytes, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    log.error("解析响应内容编码失败", e);
                    responseBody = new String(responseBytes);
                }
            }

            log.info("阳光系统响应内容: {}", responseBody);
            JSONObject responseBodyJson = JSONObject.parseObject(responseBody);

            if (response.getStatusCode() == HttpStatus.OK) {
                String status = responseBodyJson.getString("status");
                if ("FAIL".equals(status)) {
                    return CommonResult.error(500, "阳光系统投保返回错误: " + response.getStatusCode() + responseBody);
                } else {
                    return CommonResult.success(responseBody);
                }
            } else {
                return CommonResult.error(500, "阳光系统投保返回错误: " + response.getStatusCode() + responseBody);
            }

        } catch (Exception e) {
            log.error("发送投保请求失败", e);
            return CommonResult.error(500, "发送投保请求失败: " + e.getMessage());
        }
    }

    /**
     * 校验保险请求
     * 在调用投保接口前，为了提高投保成功率，可先调用校验接口，检查客户系统组织的请求是否符合预期
     *
     * @param request 投保请求
     * @return 校验响应
     */
    public CommonResult validateInsuranceRequest(InsuranceApplyReqDTO request) {
        try {
            String reqJson = JSONObject.toJSONString(request);

            // 构建校验请求参数
            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
            params.add("appId", APPID);
            params.add("req", reqJson);
            params.add("validateLevel", VALIDATE_LEVEL.toString());

            // 生成签名
            Map<String, String> signParams = new HashMap<>();
            signParams.put("appId", APPID);
            signParams.put("req", reqJson);
            signParams.put("validateLevel", VALIDATE_LEVEL.toString());
            String sign = SignUtil.generateMD5Sign(signParams, KEY);
            params.add("sign", sign);

            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

            HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);

            // 校验接口地址
            String validateApiUrl = HOST + VALIDATE_URL;

            log.info("发送校验请求到阳光系统: {}", validateApiUrl);
            log.info("请求参数: appId={}, validateLevel={}, sign={}", APPID, VALIDATE_LEVEL, sign);

            // 发送请求并获取响应
            ResponseEntity<byte[]> response = restTemplate.exchange(
                    validateApiUrl,
                    HttpMethod.POST,
                    entity,
                    byte[].class
            );

            log.info("阳光系统校验响应状态码: {}", response.getStatusCode());
            // 手动处理响应字节流，确保使用UTF-8编码
            byte[] responseBytes = response.getBody();
            String responseBody = null;
            if (responseBytes != null) {
                try {
                    responseBody = new String(responseBytes, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    log.error("解析响应内容编码失败", e);
                    responseBody = new String(responseBytes);
                }
            }

            log.info("阳光系统校验响应内容: {}", responseBody);
            JSONObject responseBodyJson = JSONObject.parseObject(responseBody);
            if ("SUCCESS".equals(responseBodyJson.getString("status"))) {
                return CommonResult.success(responseBody);
            } else {
                // 响应失败，返回错误信息
                return CommonResult.error(500, "阳光系统校验返回错误: " + response.getStatusCode() + responseBody);
            }
        } catch (Exception e) {
            log.error("发送校验请求失败", e);
            throw exception0(500, "发送校验请求失败: " + e.getMessage(), e);
        }
    }

    /**
     * 发送退保请求
     * 将退保请求发送到阳光系统
     *
     * @param request 退保请求
     * @return 退保响应
     */
    public CommonResult sendInsuranceCancel(InsuranceCancelReqDTO request) {
        try {
            String reqJson = JSONObject.toJSONString(request);
            String sign = SignUtil.generateMD5Sign(APPID, reqJson, KEY);

            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
            params.add("appId", APPID);
            params.add("req", reqJson);
            params.add("sign", sign);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, headers);
            log.error("发送退保请求到阳光系统: {}", HOST + RECEIVE_URL);
            log.error("请求参数: appId={}, req={}, sign={}", APPID, reqJson, sign);
            // 发送请求并获取响应
            ResponseEntity<byte[]> response = restTemplate.exchange(
                    HOST + RECEIVE_URL,
                    HttpMethod.POST,
                    entity,
                    byte[].class
            );
            log.error("阳光系统响应状态码: {}", response.getStatusCode());
            // 手动处理响应字节流，确保使用UTF-8编码
            byte[] responseBytes = response.getBody();
            String responseBody = null;
            if (responseBytes != null) {
                responseBody = new String(responseBytes, StandardCharsets.UTF_8);
            }
            log.error("阳光系统响应内容: {}", responseBody);
            JSONObject responseBodyJson = JSONObject.parseObject(responseBody);
            if (response.getStatusCode() == HttpStatus.OK) {
                String status = responseBodyJson.getString("status");
                if ("FAIL".equals(status)) {
                    return CommonResult.error(500, "阳光系统退保返回错误: " + response.getStatusCode() + responseBody);
                } else {
                    return CommonResult.success(responseBody);
                }
            } else {
                return CommonResult.error(500, "阳光系统退保返回错误: " + response.getStatusCode() + responseBody);
            }

        } catch (Exception e) {
            log.error("发送退保请求失败", e);
            return CommonResult.error(500, "发送退保请求失败: " + e.getMessage());
        }
    }

    public CommonResult queryInsurance(String orderNo) {
        try {
            if(StringUtils.isBlank(orderNo)) {
                return CommonResult.error(500, "订单号不能为空");
            }
            Map<String, String> request = new LinkedHashMap<>();
            request.put("appId", APPID);
            request.put("externalOrderNo", orderNo);
            request.put("key", KEY);
            String sign = MD5Util.md5(APPID + orderNo + KEY);

            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
            params.add("appId", APPID);
            params.add("externalOrderNo", orderNo);
            params.add("sign", sign);
            String url = HOST + QUERY_URL + "?appId=" + APPID + "&externalOrderNo=" + orderNo + "&sign=" + sign;
            log.error("发送查询请求到阳光系统: {}", url);
            String s = HttpUtils.get(url, null);
            log.error("阳光系统查询响应内容: {}", s);
            //{"phase":"INSURE","externalOrderNo":"tys-20261001-YC-14",
            // "paiedAmount":"10.00","total":"1","insuredCount":"1","insureProcessingCount":"0",
            // "cancelledCount":"0","cancelProcessingCount":"0","refundedAmount":"0.0",
            // "cipher":"26e2e422d370653274ee74ef3edc7f93","policies":[{"externalPolicyNumber":"2037220248460828673",
            // "service":"applyTeam","status":"SUCCESS","msg":"投保成功","policyNo":"HW61927008L7JB7J8Q00"}]}
            return CommonResult.success(s);
        } catch (Exception e) {
            log.error("发送查询请求失败", e);
            return CommonResult.error(500, "发送查询请求失败: " + e.getMessage());
        }
    }

    public void downloadInsurance(String orderNo) {
        try {
            Map<String, String> request = new LinkedHashMap<>();
            request.put("appId", APPID);
            request.put("policyNo", orderNo);
            request.put("key", KEY);
            String sign = MD5Util.md5(APPID + orderNo + KEY);

            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
            params.add("appId", APPID);
            params.add("policyNo", orderNo);
            params.add("sign", sign);
            String apiUrl = HOST + DOWNLOAD_URL + "?appId=" + APPID + "&policyNo=" + orderNo + "&sign=" + sign;
            log.error("发送下载请求到阳光系统: {}", apiUrl);
            // 指定接口地址
            // 创建 OkHttp 客户端对象
            OkHttpClient client = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS) // 设置连接超时时间为15秒
                    .readTimeout(15, TimeUnit.SECONDS) // 设置读取超时时间为15秒
                    .build();
            // 创建请求对象
            Request request1 = new Request.Builder()
                    .url(apiUrl)
                    .build();
            // 发起请求
            Response response = client.newCall(request1).execute();
            // 获取响应体
            ResponseBody responseBody = response.body();
            // 创建输出流
            FileOutputStream outputStream = new FileOutputStream("D:/example.pdf");
            // 将响应体的数据写入输出流，生成 PDF 文件
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = responseBody.byteStream().read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            // 关闭流
            responseBody.byteStream().close();
            outputStream.close();
            //{"phase":"INSURE","externalOrderNo":"tys-20261001-YC-14",
            // "paiedAmount":"10.00","total":"1","insuredCount":"1","insureProcessingCount":"0",
            // "cancelledCount":"0","cancelProcessingCount":"0","refundedAmount":"0.0",
            // "cipher":"26e2e422d370653274ee74ef3edc7f93","policies":[{"externalPolicyNumber":"2037220248460828673",
            // "service":"applyTeam","status":"SUCCESS","msg":"投保成功","policyNo":"HW61927008L7JB7J8Q00"}]}
        } catch (Exception e) {
            log.error("发送下载请求失败", e);
        }
    }

    public CommonResult queryInsuranceNew(String orderNo,Long orderId) {
        try {
            if(StringUtils.isBlank(orderNo)) {
                return CommonResult.error(500, "订单号不能为空");
            }
            Map<String, String> request = new LinkedHashMap<>();
            request.put("appId", APPID);
            request.put("externalOrderNo", orderNo);
            request.put("key", KEY);
            String sign = MD5Util.md5(APPID + orderNo + KEY);

            MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
            params.add("appId", APPID);
            params.add("externalOrderNo", orderNo);
            params.add("sign", sign);
            String url = HOST + QUERY_URL + "?appId=" + APPID + "&externalOrderNo=" + orderNo + "&sign=" + sign;
            log.error(orderNo+"发送查询请求到阳光系统: {}", url);
            String s = HttpUtils.get(url, null);
            log.error(orderNo+"阳光系统查询响应内容: {}", s);

            return CommonResult.success(s);
        } catch (Exception e) {
            log.error("发送查询请求失败", e);
            return CommonResult.error(500, "发送查询请求失败: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        InsuranceUtilBak insuranceUtil = new InsuranceUtilBak();
        CommonResult commonResult = insuranceUtil.queryInsurance("2043342644000714753");
//        insuranceUtil.downloadInsurance("PEAK20264205QA00A00009");
        System.out.println(commonResult);


    }
}
