package com.jeesharp.business.wechat.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import com.jeesharp.business.wechat.WeChatService;
import com.jeesharp.business.wechat.config.WeChatConfig;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

/**
 * 微信业务层
 *
 * @author kxh
 * @date 2024/9/25
 */
@Service
public class WeChatServiceImpl implements WeChatService {

    private final WeChatConfig properties;

    public WeChatServiceImpl(WeChatConfig properties) {
        this.properties = properties;
    }

    @Override
    public WxPayService getWxPayService() {
        WxPayConfig wxPayConfig = new WxPayConfig();
        wxPayConfig.setAppId(properties.getPayAppId());
        wxPayConfig.setMchId(properties.getMchId());
        wxPayConfig.setTradeType(WxPayConstants.TradeType.JSAPI);
        String signType = properties.getSignType();
        if (signType != null && !signType.isBlank()) {
            String normalized = signType.toUpperCase().replace("-", "").replace("_", "");
            if (normalized.contains("HMACSHA256")) {
                wxPayConfig.setSignType(WxPayConstants.SignType.HMAC_SHA256);
            } else {
                wxPayConfig.setSignType(WxPayConstants.SignType.MD5);
            }
        } else {
            wxPayConfig.setSignType(WxPayConstants.SignType.MD5);
        }
        //api密钥
        wxPayConfig.setMchKey(properties.getMchKey());
        wxPayConfig.setApiV3Key(properties.getApiKey());
        String certPath = properties.getCertPath();
        if (certPath != null && !certPath.isBlank()) {
            wxPayConfig.setKeyPath(certPath.trim());
            try {
                wxPayConfig.setKeyContent(Files.readAllBytes(Path.of(certPath.trim())));
            } catch (IOException e) {
                throw new IllegalStateException("无法读取微信商户 p12 证书: " + certPath, e);
            }
        } else {
            wxPayConfig.setKeyPath(certPath);
        }
        wxPayConfig.setPrivateKeyString(null);
        wxPayConfig.setPrivateKeyPath(properties.getCertPrivateKeyPath());
        wxPayConfig.setPrivateCertPath(properties.getCertPrivateCertPath());
        //log.info("WxPayProperties.getCertPath()：{}", properties.getCertPath());
        wxPayConfig.setNotifyUrl(properties.getNotifyUrl());
        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(wxPayConfig);
        return wxPayService;
    }

    @Override
    public WxMaService getWxMaService() {
        WxMaDefaultConfigImpl wxMaInMemoryConfig = new WxMaDefaultConfigImpl();
        wxMaInMemoryConfig.setAppid(properties.getMiniappAppId());
        wxMaInMemoryConfig.setSecret(properties.getMiniappSecret());
        WxMaService wxMaService = new WxMaServiceImpl();
        wxMaService.setWxMaConfig(wxMaInMemoryConfig);
        return wxMaService;
    }

    @Override
    public WxMpService getWxMpService() {
        WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
        wxMpDefaultConfig.setAppId(properties.getMpAppId());
        wxMpDefaultConfig.setSecret(properties.getMpSecret());
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpDefaultConfig);
        return wxMpService;
    }
}
