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.model.pddjy.request.JYBaseRequestModel;
import org.ta.pddserver.model.pddlg.request.LGBaseModel;

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


@Slf4j
public class PddHttpTool {
//    private static final String URL = "https://opengw-api.hutaojie.com/ark/router";
//    private static final String URL_GD = "https://opengw-api.hutaojie.com/api/router";
    private static final String URL = "https://ark-api.pinduoduo.com/ark/router";
    private static final String URL_GD = "https://gw-api.pinduoduo.com/api/router";

    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(URL, requestEntity, String.class);

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

    public static String sendJsonPost(Object requestObj) {
        log.info("REQ:{}", JSONObject.toJSONString(requestObj));
        // 自动序列化 requestObj 为 JSON，并设置 Content-Type: application/json
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> resp = restTemplate.postForEntity(URL, requestObj, String.class);
        return resp.getBody();
    }

    public static String sendJsonPostGD(Object requestObj) {
        log.info("REQ:{}", JSONObject.toJSONString(requestObj));
        // 自动序列化 requestObj 为 JSON，并设置 Content-Type: application/json
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> resp = restTemplate.postForEntity(URL_GD, requestObj, String.class);
        return resp.getBody();
    }
}
