package cn.quant.baa.pay.config;

import cn.quant.baa.pay.acquirer.AcquirerConfiguration;
import cn.quant.baa.pay.acquirer.AcquirerConfigurer;
import cn.quant.baa.pay.acquirer.AcquirerProperties;
import cn.quant.baa.pay.acquirer.MerchantAcquirer;
import cn.quant.baa.pay.acquirer.weixin.AlipayMerchantAcquirer;
import cn.quant.baa.pay.acquirer.weixin.WeiXinMerchantAcquirer;
import cn.quant.baa.pay.jpa.entity.*;
import cn.quant.baa.pay.jpa.repository.*;
import cn.quant.baa.pay.model.DictionaryItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.crypto.NoSuchPaddingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2021/8/22 0022.
 */
@Configuration
public class ServerConfiguration {

    @Autowired
    private PayAccountRepository payAccountRepository;

    @Autowired
    private PayAppRepository payAppRepository;

    @Autowired
    private PayFeatureRepository payFeatureRepository;

    @Autowired
    private MchChannelRepository mchChannelRepository;

    @Autowired
    private DictionaryRepository dictionaryRepository;

    public AcquirerConfigurer configurer() {
        List<PayAccountEntity> accounts = payAccountRepository.findAll();
        List<PayFeatureEntity> features = payFeatureRepository.findAll();
        List<PayAppEntity> apps = payAppRepository.findAll();

        AcquirerConfigurer configurer = new AcquirerConfigurer();
        configurer.accounts(accounts)
                .features(features)
                .apps(apps);

        accounts.clear();
        features.clear();
        apps.clear();
        return configurer;
    }

    @Bean
    public DictionaryViewer dictionaryViewer() {
        DictionaryViewer dictionary = new DictionaryViewer();
        List<DictionaryEntity> entities = dictionaryRepository.findAll();
        for (DictionaryEntity entity : entities) {
            DictionaryItem item = new DictionaryItem();
            item.setName(entity.getName());
            item.setCode(entity.getCode());
            item.setType(entity.getType());
            item.setImageUrl(entity.getImageUrl());
            dictionary.add(item);
        }
        return dictionary;
    }

    @Bean
    public AcquirerConfiguration acquirerConfiguration() {
        AcquirerConfigurer configurer = configurer();
        List<MchChannelEntity> entities = mchChannelRepository.findAll();
        AcquirerConfiguration configuration = configurer.configure(entities);
        configurer.release();
        entities.clear();
        return configuration;
    }

    @Bean
    public MerchantAcquirer acquirer(AcquirerConfiguration acquirerConfiguration) throws Exception {
        MerchantAcquirer merchantAcquirer = MerchantAcquirer.getInstance();
        Collection<AcquirerProperties> properties = acquirerConfiguration.getAll();
        for (AcquirerProperties property : properties) {
            if("WXP".equals(property.getPayChanCode())){
                WeiXinMerchantAcquirer acquirer = new WeiXinMerchantAcquirer();
                acquirer.init(property);
                merchantAcquirer.register(acquirer);
            } else if ("ALIP".equals(property.getPayChanCode())) {
                AlipayMerchantAcquirer acquirer = new AlipayMerchantAcquirer();
                acquirer.init(property);
                merchantAcquirer.register(acquirer);
            }
        }
        return merchantAcquirer;
    }
}
