package com.liquidnet.service.goblin.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liquidnet.commons.lang.util.HttpUtil;
import com.liquidnet.commons.lang.util.MD5Utils;
import com.liquidnet.service.goblin.param.shouqianba.request.CashierQueryRequest;
import com.liquidnet.service.goblin.param.shouqianba.request.OrderCreateRequest;
import com.liquidnet.service.goblin.param.shouqianba.request.SettlementCreateRequest;
import com.liquidnet.service.goblin.param.shouqianba.response.OrderCreateResponse;
import com.liquidnet.service.goblin.param.shouqianba.response.SettlementCreateResponse;
import com.liquidnet.service.goblin.param.shouqianba.response.data.CashierQueryData;
import com.liquidnet.service.goblin.param.shouqianba.response.data.OrderCreateData;
import com.liquidnet.service.goblin.param.shouqianba.response.data.SettlementCreateData;
import com.liquidnet.service.goblin.service.IGoblinShouQianBaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

import java.util.TreeMap;

/**
 * 收钱吧对接实现类
 *
 * @author liquidnet
 * @since 2026-03-16
 */
@Slf4j
@Service
public class GoblinShouQianBaServiceImpl implements IGoblinShouQianBaService {

    private static final ObjectMapper objectMapper = new ObjectMapper();

    // TODO: 先写固定常量，后期可配置文件化
    public static final String BASE_API = "https://api.shouqianba.com";
    private static final String APPID = "test_appid";
    private static final String APPKEY = "test_appkey";

    /**
     * 创建结算明细
     *
     * @param request 创建结算明细请求参数
     * @return 收钱吧响应结果；调用失败时返回 null
     */
    @Override
    public SettlementCreateData createSettlement(SettlementCreateRequest request) {
        final String url = BASE_API + "/optimus/core/mall/createPreOrder";
        log.info("[收钱吧] 创建结算明细, 请求URL: {}, 请求参数: {}", url, request);

        try {
            // body进行序列化成TreeMap以后 最后转成json（为了排序稳定）
            TreeMap<String, Object> map = objectMapper.convertValue(
                    request,
                    new TypeReference<TreeMap<String, Object>>() {
                    }
            );
            String requestBody = objectMapper.writeValueAsString(map);

            // 构建请求头（添加签名参数）
            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            headers.add("Authorization", getSign(requestBody));

            String responseStr = HttpUtil.postJson(url, requestBody, headers);
            log.info("[收钱吧] 创建结算明细, 响应体: {}", responseStr);

            // 转换响应报文
            SettlementCreateResponse response = objectMapper.readValue(responseStr, SettlementCreateResponse.class);
            if (response != null && Boolean.TRUE.equals(response.getSuccess())) {
                return response.getData();
            } else {
                log.error("[收钱吧] 创建结算明细业务失败, code: {}, msg: {}",
                        response != null ? response.getCode() : "null",
                        response != null ? response.getMsg() : "null");
                return null;
            }
        } catch (Exception e) {
            log.error("[收钱吧] 创建结算明细发生异常, 请求参数: {}", request, e);
            return null;
        }
    }

    @Override
    public OrderCreateData createOrder(OrderCreateRequest request) {
        final String url = BASE_API + "/optimus/core/mall/createPreOrder";
        log.info("[收钱吧] 创建订单, 请求URL: {}, 请求参数: {}", url, request);

        try {
            // body进行序列化成TreeMap以后 最后转成json（为了排序稳定）
            TreeMap<String, Object> map = objectMapper.convertValue(
                    request,
                    new TypeReference<TreeMap<String, Object>>() {
                    }
            );
            String requestBody = objectMapper.writeValueAsString(map);

            // 构建请求头（添加签名参数）
            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            headers.add("Authorization", getSign(requestBody));

            String responseStr = HttpUtil.postJson(url, requestBody, headers);
            log.info("[收钱吧] 创建订单, 响应体: {}", responseStr);

            // 转换响应报文
            OrderCreateResponse response = objectMapper.readValue(responseStr, OrderCreateResponse.class);
            if (response != null && Boolean.TRUE.equals(response.getSuccess())) {
                return response.getData();
            } else {
                log.error("[收钱吧] 创建订单失败, code: {}, msg: {}",
                        response != null ? response.getCode() : "null",
                        response != null ? response.getMsg() : "null");
                return null;
            }
        } catch (Exception e) {
            log.error("[收钱吧] 创建订单发生异常, 请求参数: {}", request, e);
            return null;
        }
    }

    @Override
    public CashierQueryData queryCashier(CashierQueryRequest request) {

        return null;
    }

    private String getSign(String bodyJsonStr) {
        try {
            // 签名算法: sign = MD5( CONCAT( body + appkey ) )
            String rawStr = bodyJsonStr + APPKEY;
            String sign = MD5Utils.md5(rawStr);

            // 签名首部: Authorization: appid + " " + sign
            return APPID + " " + sign;
        } catch (Exception e) {
            log.error("[收钱吧] 生成签名发生异常", e);
            return "";
        }
    }
}
