package com.czg.third.alipay;

import com.alibaba.fastjson2.JSONObject;
import com.alipay.v3.ApiException;
import com.alipay.v3.api.AlipayTradeApi;
import com.alipay.v3.model.AlipayTradeCreateModel;
import com.alipay.v3.model.AlipayTradeCreateResponseModel;
import com.alipay.v3.model.AlipayTradePayModel;
import com.alipay.v3.model.ExtendParams;
import com.alipay.v3.util.model.CustomizedParams;
import com.czg.PayCst;
import com.czg.exception.CzgException;
import com.czg.pay.*;
import com.czg.third.alipay.dto.config.AlipayConfigDto;
import lombok.extern.slf4j.Slf4j;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yjjie
 * @date 2026/1/9 09:30
 */
@Slf4j
public class AlipayIsvPayManager {

    /**
     * H5支付
     *
     * @param configDto 配置
     * @param paramsDto 参数
     */
    public static Map<String, Object> jsapiPay(AlipayConfigDto configDto, CzgPayBaseReq paramsDto, NativeMerchantDTO merchantDTO) {
        try {
            AlipayClient.setApiClient(configDto);
            AlipayTradeApi api = new AlipayTradeApi();
            AlipayTradeCreateModel model = new AlipayTradeCreateModel();
            model.setOutTradeNo(paramsDto.getMchOrderNo());
            model.setProductCode("JSAPI_PAY");
            model.setOpAppId(merchantDTO.getAlipayMerchantId());
            model.setOpBuyerOpenId(paramsDto.getUserId());
            model.setTotalAmount(getYuanAmountByFen(paramsDto.getAmount()));
            model.setSubject(paramsDto.getSubject());
            model.setBody(paramsDto.getBody());
            model.setNotifyUrl(paramsDto.getNotifyUrl() + "/" + PayCst.Type.ALIPAY);

            model.setExtendParams(new ExtendParams());
            CustomizedParams customizedParams = new CustomizedParams();
            customizedParams.setAppAuthToken(merchantDTO.getAlipayAuthInfo().getAppAuthToken());

            AlipayTradeCreateResponseModel responseModel = api.create(model, customizedParams);
            log.info("支付宝 jsapi 支付结果: {}", responseModel);
            return new HashMap<>();
        } catch (ApiException e) {
            String body = e.getResponseBody();
            log.error("支付宝 H5 api 支付异常: {}", body);
            JSONObject object = JSONObject.parseObject(body);
            throw new CzgException(object.getString(PayCst.ALIPAY_ERROR_MSG_KEY));
        } catch (Exception e) {
            log.error("支付宝 H5支付异常: {}", e.getMessage());
            throw new CzgException(e.getMessage());
        }
    }

    /**
     * 条码支付
     *
     * @param configDto 配置
     * @param paramsDto 参数
     */
    public static Map<String, Object> barPay(AlipayConfigDto configDto, CzgPayBaseReq paramsDto, NativeMerchantDTO merchantDTO) {
        try {
            AlipayClient.setApiClient(configDto);

            // 构造请求参数以调用接口
            AlipayTradeApi api = new AlipayTradeApi();
            AlipayTradePayModel data = new AlipayTradePayModel();

            data.setOutTradeNo(paramsDto.getMchOrderNo());
            data.setTotalAmount(getYuanAmountByFen(paramsDto.getAmount()));
            data.setSubject(paramsDto.getSubject());
            // 设置支付授权码
            data.setAuthCode(merchantDTO.getAlipayAuthInfo().getAppAuthToken());
            // 设置支付场景
            data.setScene("bar_code");
            // 设置产品码
            data.setProductCode("FACE_TO_FACE_PAYMENT");
            data.setAuthCode(paramsDto.getAuthCode());
//            AlipayTradeCreateResponseModel responseModel = api.create(data);

            return new JSONObject();
//        } catch (ApiException e) {
//            String body = e.getResponseBody();
//            log.error("支付宝 条码支付异常: {}", body);
//            JSONObject object = JSONObject.parseObject(body);
//            throw new CzgException(object.getString(PayCst.ALIPAY_ERROR_MSG_KEY));
        } catch (Exception e) {
            log.error("支付宝 条码支付异常: {}", e.getMessage());
            throw new CzgException(e.getMessage());
        }
    }

    public static QueryOrderRespDTO queryOrder(AlipayConfigDto configDto, String orderNo, NativeMerchantDTO merchantDTO) {
        return new QueryOrderRespDTO();
    }

    public static RefundRespDTO refundOrder(AlipayConfigDto configDto, CzgRefundReq paramsDto, String notifyUrl, NativeMerchantDTO merchantDTO) {
        return new RefundRespDTO();
    }

    /**
     * 金额转换
     */
    private static String getYuanAmountByFen(Long amount) {
        BigDecimal yuanAmount = new BigDecimal(amount).divide(new BigDecimal(100));
        return yuanAmount.toString();
    }

}
