package com.crm.ucard.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.crm.rely.backend.util.HttpUtil;
import com.crm.rely.backend.util.MapUtil;
import com.crm.ucard.config.WasabiConfig;
import com.crm.ucard.model.TwoTuple;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Connection;

import java.io.File;
import java.util.HashMap;
import java.util.Map;


/**
 * @author: test
 * @date: 2024/03/25 17:03
 * @describe:
 */
@Slf4j
public class WasabiCommonUtil {

    public static final String HEADER_SIGNATURE = "X-WSB-SIGNATURE";
    public static final String HEADER_API_KEY = "X-WSB-API-KEY";

    public static TwoTuple<String, String> post(String url, WasabiConfig config) {
        return post(url, new JsonObject().toString(), config);
    }

    /**
     * post json
     *
     * @param url
     * @param paramStr
     * @return
     */
    public static TwoTuple<String, String> post(String url, String paramStr, WasabiConfig config) {
        long startTime = System.currentTimeMillis();
        Connection.Response response = null;
        try {
            String signature = RsaUtils.signSHA256(paramStr, config.getPrivateKey());

            Map<String, String> headerMap = new HashMap<>(2);
            headerMap.put(HEADER_API_KEY, config.getApiKey());
            headerMap.put(HEADER_SIGNATURE, signature);

            log.info("sendRequestJson--url:{},headerMap:{}, ret: {}", url, headerMap, paramStr);
            response = HttpUtil.post(url, headerMap, paramStr);
            log.info("sendPostJson--url:{}, ret: {}, cost: {}", url, response.body(), System.currentTimeMillis() - startTime);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        String signature = response.header(HEADER_SIGNATURE);
        log.info("res signature:{}", signature);
        return new TwoTuple<>(signature, response.body());
    }

    /**
     * post json
     * @param url
     * @param file
     * @param paramMap
     * @return
     */
    public static TwoTuple<String, String> postFile(String url, File file, Map<String, String> paramMap, WasabiConfig config) {
        long startTime = System.currentTimeMillis();
        Connection.Response response = null;
        try {
            String signature = RsaUtils.signSHA256(new JSONObject().toJSONString(), config.getPrivateKey());

            Map<String, String> headerMap = new HashMap<>(2);
            headerMap.put(HEADER_API_KEY, config.getApiKey());
            headerMap.put(HEADER_SIGNATURE, signature);

            Map<String, File> fileMap = MapUtil.singletonMap("file", file);

            log.info("sendRequestJson--url:{},req signature：{}, ret: {}", url, signature, JSON.toJSONString(fileMap));
            response = HttpUtil.post(url, headerMap,paramMap, fileMap);
            log.info("sendPostJson url {}, param {}.  ret: {}, cost: {}", url,
                    getExeFileString(JSON.toJSONString(fileMap)), response.body(),
                    System.currentTimeMillis() - startTime);
        } catch (Exception e) {
            log.error("exception", e);
        }
        String signature = response.header(HEADER_SIGNATURE);
        log.info("res signature:{}", signature);
        return new TwoTuple<>(signature, response.body());
    }

    private static String getExeFileString(String param) {
        if (StringUtils.isNotBlank(param)) {
            return param.replaceAll("(\"[a-zA-Z]*template\":)(\"[^\"]+\")", "#");
        }
        return param;
    }
    
    /**
     * post json
     *
     * @param url
     * @param body
     * @return
     */
    /*public static TwoTuple<String, String> post(String url, String body, WasabiConfig config) {
        HttpResponse response = null;
        try {
            String signature = RsaUtils.signSHA256(body, config.getPrivateKey());


            Map<String, String> headerMap = new HashMap<>(2);
            headerMap.put(HEADER_API_KEY, config.getApiKey());
            headerMap.put(HEADER_SIGNATURE, signature);

            HttpRequest httpRequest = HttpRequest.post(url).body(body);
            httpRequest.addHeaders(headerMap);

            response = httpRequest.execute();
            log.info("sendPostJson url {}, response: http code {}, result: {}",
                    url, response.getStatus(), response.body());
        } catch (Exception e) {
            log.error("sendPostForm exception", e);
        }
        String signature = response.header(HEADER_SIGNATURE);
        return new TwoTuple();
    }*/

    /**
     * post json
     *
     * @param url
     * @param form
     * @return
     */
    /*public static TwoTuple<String, String> postFile(String url,  Map<String, Object> form, WasabiConfig config) {
        HttpResponse response = null;
        try {
            String signature = RsaUtils.signSHA256(new JSONObject().toJSONString(), config.getPrivateKey());

            Map<String, String> headerMap = new HashMap<>(2);
            headerMap.put(HEADER_API_KEY, config.getApiKey());
            headerMap.put(HEADER_SIGNATURE, signature);

            // 发送 POST 请求并添加 Header
            HttpRequest httpRequest = HttpRequest.post(url)
                    .addHeaders(headerMap)
                    .form(form);

            response = httpRequest.execute();
            log.info("sendPostJson url {}, response: http code {}, result: {}",
                    url, response.getStatus(), response.body());
        } catch (Exception e) {
            log.error("sendPostForm exception", e);
        }
        String signature = response.header(HEADER_SIGNATURE);
        return new TwoTuple<>(signature, response.body());
    }*/



}

