package com.njcn.msgpush.module.push.client;

import com.njcn.msgpush.module.push.client.factory.MessageProviderFactory;
import com.njcn.msgpush.module.push.client.factory.impl.AliyunProviderFactory;
import com.njcn.msgpush.module.push.constant.MsgPushConstant;
import com.njcn.msgpush.module.push.dal.dataobject.channel.ChannelProviderConfigDO;
import com.njcn.msgpush.module.push.service.channel.ChannelProviderConfigService;
import com.njcn.msgpush.module.push.util.RestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caozehui
 * @data 2026-02-10
 */
@Configuration
public class ClientConfiguration {

    @Autowired
    private RestTemplateUtil restTemplateUtil;


    @Autowired
    private ChannelProviderConfigService channelProviderConfigService;

    @Bean
    public Map<String, MessageProviderFactory> messageProviderFactoryMap() throws Exception {
        List<ChannelProviderConfigDO> activeProviders = channelProviderConfigService.getActiveProviders();
        Map<String, MessageProviderFactory> messageProviderFactoryMap = new HashMap<>();

        for (ChannelProviderConfigDO config : activeProviders) {
            switch (config.getProviderType()) {
                case MsgPushConstant.PROVIDER_TYPE_ALI_YUN: {
                    MessageProviderFactory orDefault = messageProviderFactoryMap.getOrDefault(MsgPushConstant.PROVIDER_TYPE_ALI_YUN, new AliyunProviderFactory());
                    messageProviderFactoryMap.put(MsgPushConstant.PROVIDER_TYPE_ALI_YUN, orDefault);
                }
                break;
                case MsgPushConstant.PROVIDER_TYPE_TELECOM: {
                    MessageProviderFactory orDefault = messageProviderFactoryMap.getOrDefault(MsgPushConstant.PROVIDER_TYPE_TELECOM, new AliyunProviderFactory());
                    messageProviderFactoryMap.put(MsgPushConstant.PROVIDER_TYPE_TELECOM, orDefault);
                }
                break;
                case MsgPushConstant.PROVIDER_TYPE_UNI_PUSH: {
                    MessageProviderFactory orDefault = messageProviderFactoryMap.getOrDefault(MsgPushConstant.PROVIDER_TYPE_UNI_PUSH, new AliyunProviderFactory());
                    messageProviderFactoryMap.put(MsgPushConstant.PROVIDER_TYPE_UNI_PUSH, orDefault);
                }
                break;
                default:
                    throw new IllegalArgumentException("该" + config.getProviderType() + "服务商暂不支持");
            }
        }

        if (messageProviderFactoryMap.isEmpty()) {
            throw new Exception("当前没有激活的渠道服务商!");
        }
        return messageProviderFactoryMap;
    }
}
