package com.kiisoo.ic.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Maps;
import com.kiisoo.ic.base.entity.BasicConfigDO;
import com.kiisoo.ic.base.mapper.BasicConfigDOMapper;
import lombok.val;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import me.chanjar.weixin.cp.message.WxCpMessageRouter;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Binary Wang(https://github.com/binarywang)
 */
@Configuration
@EnableConfigurationProperties(WxCpProperties.class)
@Service
public class WxCpConfiguration {

    private static Map<Integer, WxCpService> cpServices = Maps.newHashMap();

    public static Integer APPLICATIONID = 1000004;

    @Autowired
    private BasicConfigDOMapper basicConfigDOMapper;

    public static WxCpService getCpService(Integer agentId) {
        return cpServices.get(agentId);
    }

    @PostConstruct
    public void initServices() {
        QueryWrapper<BasicConfigDO> baseParams = new QueryWrapper<>();
        baseParams.eq("code","HT_CP_CORPID");
        BasicConfigDO basicConfigDO = basicConfigDOMapper.selectOne(baseParams);

        QueryWrapper<BasicConfigDO> confParams = new QueryWrapper<>();
        confParams.likeRight("code","HT_CP_");
        confParams.ne("code","HT_CP_CORPID");
        List<BasicConfigDO> confConfigDO = basicConfigDOMapper.selectList(confParams);
        Map<String,Map<String,String>> confMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(confConfigDO)){
            confConfigDO.forEach(conf -> {
                Map<String, String> oneConf = confMap.get(conf.getCode());
                if (oneConf == null){
                    oneConf = new HashMap<>();
                    oneConf.put(conf.getConfKey(),conf.getConfValue());
                    confMap.put(conf.getCode(),oneConf);
                }else{
                    oneConf.put(conf.getConfKey(),conf.getConfValue());
                }
            });
        }


        cpServices = confMap.keySet().stream().map(a -> {
            val configStorage = new WxCpDefaultConfigImpl();
            configStorage.setCorpId(basicConfigDO.getConfValue());
            configStorage.setAgentId(Integer.valueOf(confMap.get(a).get("agentId")));
            configStorage.setCorpSecret(confMap.get(a).get("secret"));
            configStorage.setToken(confMap.get(a).get("token"));
            configStorage.setAesKey(confMap.get(a).get("aesKey"));
            val service = new WxCpServiceImpl();
            service.setWxCpConfigStorage(configStorage);
            return service;
        }).collect(Collectors.toMap(service -> service.getWxCpConfigStorage().getAgentId(), a -> a));
    }
}
