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

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.liquidnet.commons.lang.util.MD5Utils;
import com.liquidnet.service.goblin.config.properties.ShouqianbaProperties;
import com.liquidnet.service.goblin.param.shouqianba.callback.CallbackParams;
import com.liquidnet.service.goblin.param.shouqianba.request.*;
import com.liquidnet.service.goblin.param.shouqianba.response.*;
import com.liquidnet.service.goblin.param.shouqianba.response.data.*;
import com.liquidnet.service.goblin.service.IGoblinShouQianBaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.List;

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


    private final ShouqianbaProperties shouqianbaProperties;

    public GoblinShouQianBaServiceImpl(ShouqianbaProperties properties) {
        this.shouqianbaProperties = properties;
        if (this.shouqianbaProperties == null) {
            throw new IllegalArgumentException("找不到应用配置");
        }
    }

    private static final ObjectMapper objectMapper;
    private static final RestTemplate restTemplate;


    static {
        // 1. 初始化 RestTemplate 并强制设置 String 转换器为 UTF-8
        restTemplate = new RestTemplate();
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));

        // 2. 初始化紧凑型 ObjectMapper (签名专用：无空格、无换行、不含空字段)
        objectMapper = new ObjectMapper();
        objectMapper.configure(SerializationFeature.INDENT_OUTPUT, false);
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    /**
     * 创建结算明细
     *
     * @param request 创建结算明细请求参数
     * @return 收钱吧响应结果；调用失败时返回 null
     */
    @Override
    public SettlementCreateData createSettlement(SettlementCreateRequest request) {
        return executeShouQianBaRequest("/optimus/core/mall/createCheckoutItems", "创建结算明细", request, SettlementCreateResponse.class);
    }

    /**
     * 创建订单
     *
     * @param request
     * @return
     */
    @Override
    public OrderCreateData createOrder(OrderCreateRequest request) {
        return executeShouQianBaRequest("/optimus/core/order/placeOrder", "创建订单", request, OrderCreateResponse.class);
    }

    /**
     * 创建收单
     * 如果上一步创建收单失败 可以使用这个接口重新创建收单
     *
     * @return
     */
    @Override
    public AcquiringCreateData createAcquiring(AcquiringCreateRequest request) {
        return executeShouQianBaRequest("/optimus/core/order/createAcquiring", "创建收单环节", request, AcquiringCreateResponse.class);
    }

    /**
     * 查询收银台
     *
     * @param request
     * @return
     */
    @Override
    public CashierQueryData queryCashier(CashierQueryRequest request) {
        return executeShouQianBaRequest("/optimus/core/cashier/queryCashierPage", "查询收银台", request, CashierQueryResponse.class);
    }

    /**
     * 创建微信预支付订单
     *
     * @param request
     * @return
     */
    @Override
    public CreateWechatPrepayOrderData createWechatPrepayOrder(CreateWechatPrepayOrderRequest request) {
        return executeShouQianBaRequest("/optimus/core/cashier/proxyPreCreatedPay", "创建微信预支付订单", request, CreateWechatPrepayOrderResponse.class);
    }

    /**
     * 查询券码
     *
     * @param request
     * @return
     */
    @Override
    public CouponQueryData queryCoupon(CouponQueryRequest request) {
        return executeShouQianBaRequest("/optimus/core/voucher/queryOrderVoucherList", "查询优惠券", request, CouponQueryResponse.class);
    }

    /**
     * 券码状态同步
     *
     * @param request
     * @return
     */
    @Override
    public boolean syncCouponStatus(CouponStatusSyncRequest request) {
        CouponStatusSyncResponse response = executeRequestAndGetResponse("/optimus/core/voucher/modifyOrderVoucherList", "同步优惠券状态", request, CouponStatusSyncResponse.class);
        return response != null && Boolean.TRUE.equals(response.getSuccess());
    }

    /**
     * 券退款
     *
     * @param request
     * @return
     */
    @Override
    public CouponRefundData refundCoupon(CouponRefundRequest request) {
        return executeShouQianBaRequest("/optimus/core/aftersale/applyRefund", "退款优惠券", request, CouponRefundResponse.class);
    }

    /**
     * 商城列表接口
     *
     * @param request
     * @return
     */
    @Override
    public List<MallListQueryData> queryMallList(MallListQueryRequest request) {
        return executeShouQianBaRequest("/optimus/core/open/mall/queryMallList", "查询门店列表", request, MallListQueryResponse.class);
    }

    /**
     * 商城商品接口
     *
     * @param request
     * @return
     */
    @Override
    public List<MallProductsQueryData> queryMallProducts(MallProductsQueryRequest request) {
        return executeShouQianBaRequest("/optimus/core/open/mall/queryMallProducts", "查询门店商品", request, MallProductsQueryResponse.class);
    }

    /**
     * 发送HTTP请求并返回Response对象
     */
    private <T extends BaseResponse<?>> T executeRequestAndGetResponse(
            String apiPath, String businessName, Object request, Class<T> responseClass) {
        final String url = shouqianbaProperties.getBaseApi() + apiPath;
        log.info("[收钱吧] {}, 请求URL: {}, 请求参数: {}", businessName, url, request);

        try {
            // body进行序列化成TreeMap以后 最后转成json（为了排序稳定）
//            TreeMap<String, Object> map = objectMapper.convertValue(
//                    request,
//                    new TypeReference<TreeMap<String, Object>>() {
//                    }
//            );
            String requestBody = objectMapper.writeValueAsString(request);
            // 去除可能存在的空格和换行符，以保证签名通过
            log.info("request body: {}", requestBody);

            // 构建请求头（添加签名参数）
            final String sign = getSign(requestBody);
            log.info("request header: {}", sign);

            MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
            headers.add("Authorization", sign);
            headers.add("Content-Type", "application/json");
            headers.add("X-env-flag", "41508");

            // 4. 发送请求
            // 注意：传入的是 String jsonBody，接收的也是 String.class，确保不被二次处理
            HttpEntity<String> entity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);

            String responseStr = responseEntity.getBody();
            log.info("[收钱吧] {}, 响应体: {}", businessName, responseStr);

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

    /**
     * 封装收钱吧接口调用，提取公共逻辑并发起请求，默认返回Data
     */
    private <D, T extends BaseResponse<D>> D executeShouQianBaRequest(
            String apiPath, String businessName, Object request, Class<T> responseClass) {
        T response = executeRequestAndGetResponse(apiPath, businessName, request, responseClass);
        if (response != null && Boolean.TRUE.equals(response.getSuccess())) {
            return response.getData();
        }
        return null;
    }

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

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

    /**
     * 收钱吧回调推送验签
     *
     * @param callbackParams@return 验签是否通过
     */
    public boolean verifySignature(CallbackParams callbackParams) {
        // 签名原串 = eventId + timestamp + nonce + content
        String plaintext = callbackParams.getEventId() + callbackParams.getTimestamp() + callbackParams.getNonce() + callbackParams.getContent();
        return verifySignatureSHA256WithRSA(plaintext, callbackParams.getSignature(), shouqianbaProperties.getPublicKey());
    }

    public boolean verifySignatureSHA256WithRSA(String plaintext, String inputSignature, String pubKey) {
        return verifySignatureSHA256WithRSA(plaintext.getBytes(StandardCharsets.UTF_8), inputSignature, pubKey);
    }

    public boolean verifySignatureSHA256WithRSA(byte[] plaintextByte, String inputSignature, String pubKey) {
        try {
            X509EncodedKeySpec bobPubKeySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(pubKey));
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey publicKey = keyFactory.generatePublic(bobPubKeySpec);
            byte[] signed = Base64.getDecoder().decode(inputSignature);
            Signature signature = Signature.getInstance("SHA256WithRSA");
            signature.initVerify(publicKey);
            signature.update(plaintextByte);
            return signature.verify(signed);
        } catch (Exception e) {
            log.error("[收钱吧] 验签异常, signature={}, pubKey={}", inputSignature, pubKey, e);
            return false;
        }
    }
}
