package org.ta.pddserver.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.ta.pddserver.config.PddAPIConfig;
import org.ta.pddserver.entity.LogsPddIoEntity;
import org.ta.pddserver.model.pddgd.request.GDTicketListRequest;
import org.ta.pddserver.model.pddjy.request.JYBaseRequestModel;
import org.ta.pddserver.model.pddlg.request.LGBaseModel;
import org.ta.pddserver.service.LogsPddIoService;
import org.ta.pddserver.service.impl.LogsPddIoImpl;

import java.util.HashMap;
import java.util.Map;


@Slf4j
public class PddHttpTool {

    private static LogsPddIoImpl logsPddIoImpl;

    public static void setLogsToolService(LogsPddIoImpl impl) {
        logsPddIoImpl = impl;
    }

    public static String postRequest(JYBaseRequestModel request) {

        MultiValueMap<String, String> params = ObjectCommonTool.objectToMultiValueMap(request);
        RestTemplate restTemplate = new RestTemplate();
        // 1. 设置请求头：指定 Content-Type
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

        // 2. 封装请求实体（参数 + 头信息）
        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(params, headers);

        // 3. 发送 POST 请求，获取响应
        ResponseEntity<String> response = restTemplate.postForEntity(PddAPIConfig.URL, requestEntity, String.class);

        // 4. 返回响应体（根据实际需求处理响应状态码、响应头）
        return response.getBody();
    }

    public static String sendJsonPost(Object requestObj) {
        log.info("REQ:{}", JSONObject.toJSONString(requestObj));

//        if (requestObj != null) {
//            throw new RuntimeException("强制截断");
//        }
        // 自动序列化 requestObj 为 JSON，并设置 Content-Type: application/json
        HttpHeaders headers = new HttpHeaders();
        // headers.set("X-Canary-Staging", "staging"); // 测试联调，专用
        headers.add("Content-Type", "application/json;charset=UTF-8");
        HttpEntity<String> requestEntity = new HttpEntity<>(JSONObject.toJSONString(requestObj), headers);

        // 2. 封装请求实体（参数 + 头信息）
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> resp = restTemplate.postForEntity(PddAPIConfig.URL, requestEntity, String.class);
        log.info("RESP:{}", resp.getBody());
        if (logsPddIoImpl != null) {
            LogsPddIoEntity logsPddIoEntity = new LogsPddIoEntity();
            logsPddIoEntity.setAction("JY");
            logsPddIoEntity.setReq(JSONObject.toJSONString(requestObj));
            logsPddIoEntity.setResp(resp.getBody());
            logsPddIoImpl.save(logsPddIoEntity);
        }
        return resp.getBody();
    }

    public static String sendJsonPostGD(Object requestObj) {
        log.info("REQ:{}", JSONObject.toJSONString(requestObj));
        // 自动序列化 requestObj 为 JSON，并设置 Content-Type: application/json
        HttpHeaders headers = new HttpHeaders();
        // headers.set("X-Canary-Staging", "staging"); // 测试联调，专用
        headers.add("Content-Type", "application/json;charset=UTF-8");
        HttpEntity<String> requestEntity = new HttpEntity<>(JSONObject.toJSONString(requestObj), headers);

        // 2. 封装请求实体（参数 + 头信息）
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> resp = restTemplate.postForEntity(PddAPIConfig.URL_GD, requestEntity, String.class);
        log.info("RESP:{}", resp.getBody());
        if (logsPddIoImpl != null) {
            LogsPddIoEntity logsPddIoEntity = new LogsPddIoEntity();
            logsPddIoEntity.setAction("GD");
            logsPddIoEntity.setReq(JSONObject.toJSONString(requestObj));
            logsPddIoEntity.setResp(resp.getBody());
            logsPddIoImpl.save(logsPddIoEntity);
        }
        return resp.getBody();
    }

    public static String sendJsonPostGD2(Object requestObj) {
//        log.info("REQ:{}", JSONObject.toJSONString(requestObj));
        // 自动序列化 requestObj 为 JSON，并设置 Content-Type: application/json
        HttpHeaders headers = new HttpHeaders();
        // headers.set("X-Canary-Staging", "staging"); // 测试联调，专用
        headers.add("Content-Type", "application/json;charset=UTF-8");
        HttpEntity<String> requestEntity = new HttpEntity<>(JSONObject.toJSONString(requestObj), headers);

        // 2. 封装请求实体（参数 + 头信息）
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> resp = restTemplate.postForEntity(PddAPIConfig.URL_GD, requestEntity, String.class);
//        log.info("RESP:{}", resp.getBody());
        // 不保存该日志，方式数量过多
        /*
        if (logsPddIoImpl != null) {
            LogsPddIoEntity logsPddIoEntity = new LogsPddIoEntity();
            logsPddIoEntity.setAction("GD");
            logsPddIoEntity.setReq(JSONObject.toJSONString(requestObj));
            logsPddIoEntity.setResp(resp.getBody());
            logsPddIoImpl.save(logsPddIoEntity);
        }

         */
        return resp.getBody();
    }

    public static String sendJsonPostBase(Object requestObj) {
        log.info("REQ:{}", JSONObject.toJSONString(requestObj));

//        if (requestObj != null) {
//            throw new RuntimeException("强制截断");
//        }
        // 自动序列化 requestObj 为 JSON，并设置 Content-Type: application/json
        HttpHeaders headers = new HttpHeaders();

        // headers.set("X-Canary-Staging", "staging"); // 测试联调，专用
        headers.add("Content-Type", "application/json;charset=UTF-8");
        HttpEntity<String> requestEntity = new HttpEntity<>(JSONObject.toJSONString(requestObj), headers);

        // 2. 封装请求实体（参数 + 头信息）
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> resp = restTemplate.postForEntity(PddAPIConfig.BASE_URL, requestEntity, String.class);
        log.info("RESP:{}", resp.getBody());
        if (logsPddIoImpl != null) {
            LogsPddIoEntity logsPddIoEntity = new LogsPddIoEntity();
            logsPddIoEntity.setAction("JY");
            logsPddIoEntity.setReq(JSONObject.toJSONString(requestObj));
            logsPddIoEntity.setResp(resp.getBody());
            logsPddIoImpl.save(logsPddIoEntity);
        }
        return resp.getBody();
    }
}
