package com.ruoyi.cai.pay;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import com.ruoyi.cai.domain.PayConfig;
import com.ruoyi.cai.service.PayConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Slf4j
public class PayConfigManager {
    private static final Map<Long,PayConfig> PAY_CONFIGS_MAP = new ConcurrentHashMap<>();
    @Autowired
    private PayConfigService payConfigService;

    @PostConstruct
    public void init(){
        List<PayConfig> list = payConfigService.list(Wrappers.lambdaQuery(PayConfig.class));
        for (PayConfig payConfig : list) {
            PAY_CONFIGS_MAP.put(payConfig.getId(), payConfig);
        }
    }

    public Collection<PayConfig> getAllPayConfig(){
        return PAY_CONFIGS_MAP.values();
    }

    // 1支付宝2微信
    public void addPayConfig(PayConfig payConfig) {
        PAY_CONFIGS_MAP.put(payConfig.getId(),payConfig);
    }

    public void updatePayConfig(PayConfig payConfig) {
        PAY_CONFIGS_MAP.put(payConfig.getId(),payConfig);
        if(PayTypeEnum.ALI.getCode().equals(payConfig.getPayType())){
            AliPayApiConfigKit.removeApiConfig(payConfig.getAppid());
        }
    }

    public void deletePayConfig(PayConfig payConfig) {
        PAY_CONFIGS_MAP.remove(payConfig.getId());
        if(PayTypeEnum.ALI.getCode().equals(payConfig.getPayType())){
            AliPayApiConfigKit.removeApiConfig(payConfig.getAppid());
        }
    }

    public PayConfig getWxConfig(){
        Collection<PayConfig> values = PAY_CONFIGS_MAP.values();
        for (PayConfig value : values) {
            if(PayTypeEnum.WX.getCode().equals(value.getPayType())){
                return value;
            }
        }
        return null;
    }

    public PayConfig getAliConfig(){
        Collection<PayConfig> values = PAY_CONFIGS_MAP.values();
        for (PayConfig value : values) {
            if(PayTypeEnum.ALI.getCode().equals(value.getPayType())){
                return value;
            }
        }
        return null;
    }


    public boolean initAliPay() {
        PayConfig payConfig = this.getAliConfig();
        if(payConfig == null){
            return false;
        }
        AliPayApiConfig aliPayApiConfig;
        try {
            aliPayApiConfig = AliPayApiConfigKit.getApiConfig(payConfig.getAppid());
        } catch (Exception e) {
            log.error("实例化AliPayApiConfig...");
            // 如果Map中没有当前支付宝的实例就初始化并添加到Map中
            aliPayApiConfig = AliPayApiConfig.builder()
                    .setAppId(payConfig.getAppid())
                    .setAliPayPublicKey(payConfig.getPublicKey())
                    .setCharset("UTF-8")
                    .setPrivateKey(payConfig.getPrivateKey())
                    .setServiceUrl(payConfig.getNotifyUrl())
                    .setSignType("RSA2")
                    .build();
        }
        AliPayApiConfigKit.setThreadLocalAliPayApiConfig(aliPayApiConfig);
        return true;
    }

    public PayConfig getAliConfigByAppId(String appId) {
        Collection<PayConfig> values = PAY_CONFIGS_MAP.values();
        for (PayConfig value : values) {
            if(value.getAppid() != null && value.getAppid().equals(appId)){
                return value;
            }
        }
        return null;
    }

    public PayConfig getWxConfigByMchId(String mchId) {
        Collection<PayConfig> values = PAY_CONFIGS_MAP.values();
        for (PayConfig value : values) {
            if(value.getWxMcid() != null && value.getWxMcid().equals(mchId)){
                return value;
            }
        }
        return null;
    }
}
