package com.ruoyi.bst.channel.service.impl;

import com.ruoyi.common.auth.ali.AliConfig;
import com.ruoyi.common.constants.ConfigKeys;
import com.ruoyi.common.pay.tm.config.TmPayConfig;
import com.ruoyi.common.pay.wx.config.WxPayConfig;
import com.ruoyi.bst.app.domain.AppConfig;
import com.ruoyi.bst.app.domain.AppVO;
import com.ruoyi.bst.app.service.AppService;
import com.ruoyi.bst.channel.domain.ChannelConfig;
import com.ruoyi.bst.channel.domain.ChannelVO;
import com.ruoyi.bst.channel.domain.enums.ChannelApiType;
import com.ruoyi.bst.channel.service.ChannelConverter;
import com.ruoyi.bst.channel.service.ChannelService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.config.service.ConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author wjh
 * 2025/1/7
 */
@Service
@Slf4j
public class ChannelConverterImpl implements ChannelConverter {

    @Autowired
    private ChannelService channelService;

    @Autowired
    private AppService appService;

    @Autowired
    private ConfigService configService;

    @Value("${pay.notifyHost}")
    private String notifyHost;

    @Override
    public Object toConfig(ChannelVO channel, AppVO app) {
        if (channel == null || channel.getChannelConfig() == null) {
            return null;
        }
        if (app == null) {
            app = new AppVO();
        }

        // 微信
        if (ChannelApiType.WECHAT.getType().equals(channel.getApiType())) {
            return toWxPayConfig(channel, app);
        }
        // 支付宝
        else if (ChannelApiType.ALI.getType().equals(channel.getApiType())) {
            return toAliConfig(app);
        }
        // 太米
        else if (ChannelApiType.TM_WX.getType().equals(channel.getApiType())) {
            return toTmPayConfig(channel);
        }

        return null;
    }


    private TmPayConfig toTmPayConfig(ChannelVO channel) {

        String host = notifyHost;
        TmPayConfig config = new TmPayConfig();
        if (StringUtils.hasText(host) && channel != null) {
            config.setNotifyUrl(host + "/pay/notify/tm/" + channel.getChannelId());
        }
        if (channel != null && channel.getChannelConfig() != null) {
            ChannelConfig channelConfig = channel.getChannelConfig();
            config.setDeveloperId(channelConfig.getDeveloperId());
            config.setShopId(channelConfig.getShopId());
            config.setSignKey(channelConfig.getSignKey());
            config.setHttpUrl(channelConfig.getHttpUrl());
            config.setSn(channelConfig.getSn());
        }
        return config;
    }

    private AliConfig toAliConfig(AppVO app) {
        String host = notifyHost;
        AliConfig config = new AliConfig();

        if (StringUtils.hasText(host) && app != null) {
            config.setNotifyUrl(host + "/pay/notify/ali/" + app.getId());
            config.setTransferNotifyUrl(host + "/pay/notify/ali/transfer" + app.getId());
            if (app.getConfig() != null) {
                AppConfig appConfig =  app.getConfig();
                config.setAppId(appConfig.getAppId());
                config.setPrivateKey(appConfig.getPrivateKey());
                config.setAesPrivateKey(appConfig.getAesPrivateKey());
                config.setAppCertPath(appConfig.getAppCertPath());
                config.setAlipayCertPath(appConfig.getAlipayCertPath());
                config.setAlipayRootCertPath(appConfig.getAlipayRootCertPath());
            }
        }
        return config;
    }

    @Override
    public WxPayConfig toWxPayConfig(Long channelId, Long appId) {
        ChannelVO channel = channelService.selectChannelByChannelId(channelId);
        if (channel == null) {
            log.error("ID为{}的支付渠道不存在", channelId);
            return null;
        }

        AppVO app = appService.selectAppById(appId);
        if (app == null) {
            log.error("ID为{}的APP不存在", appId);
            return null;
        }

        return this.toWxPayConfig(channel, app);
    }

    @Override
    public WxPayConfig toWxPayConfig(ChannelVO channel, AppVO app) {
        String host = notifyHost;

        WxPayConfig config = new WxPayConfig();

        if (StringUtils.hasText(host) && channel != null && app != null) {
            config.setNotifyUrl(host + "/pay/notify/wx/" + channel.getChannelId() + "/" + app.getId());
            config.setRefundNotifyUrl(host + "/pay/notify/wx/refund/" + channel.getChannelId() + "/" + app.getId());
        }
        if (channel != null && channel.getChannelConfig() != null) {
            ChannelConfig channelConfig = channel.getChannelConfig();
            config.setMerchantId(channelConfig.getMerchantId());
            config.setApiV3Key(channelConfig.getApiV3Key());
            config.setPrivateKeyPath(channelConfig.getPrivateKeyPath());
            config.setMerchantSerialNumber(channelConfig.getMerchantSerialNumber());
        }
        if (app != null && app.getConfig() != null) {
            AppConfig appConfig = app.getConfig();
            config.setAppId(appConfig.getAppId());
        }

        return config;
    }
}
