package com.jay.monitor.data.server.config;

import com.alipay.remoting.rpc.protocol.UserProcessor;
import com.jay.monitor.data.core.enums.MonitorDataConstants;
import com.jay.monitor.data.server.MonitorDataServer;
import com.jay.monitor.data.server.config.props.AlertProperties;
import com.jay.monitor.data.server.config.props.MonitorProperties;
import com.jay.monitor.data.server.factory.DataIdFactory;
import com.jay.monitor.data.server.factory.IdGenerateService;
import com.jay.monitor.data.server.factory.callback.IdChangeCallback;
import com.jay.monitor.data.server.store.AbstractSplitDataProcess;
import com.jay.monitor.data.server.store.ConfigStoreManager;
import com.jay.monitor.data.server.store.DefaultLocalConfigManager;
import com.jay.monitor.data.server.store.SplitDataManager;
import com.jay.monitor.data.server.store.mysql.MysqlConfigStoreManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * Spring定义
 * 普通的Bean的定义
 *
 * @author Liuhx
 * @version V1.0
 * @create 2018/6/14 13:40
 * @email liuhx@elab-plus.com
 **/
@Configuration
//@ComponentScan(basePackages = {"com.jay.monitor.data.server"}, excludeFilters = {
//        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class, ControllerAdvice.class})
//})
@EnableConfigurationProperties({MonitorProperties.class, AlertProperties.class})
public class BeanConfig {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Bean
    public MonitorDataServer boltServer(List<UserProcessor<?>> userProcessorList) {
        MonitorDataServer monitorDataServer = new MonitorDataServer(MonitorDataConstants.DEFAULT_PORT);
        monitorDataServer.registerUserProcessor(userProcessorList);
        if (monitorDataServer.start()) {
            logger.info(" 监控数据传输器启动完成 : " + monitorDataServer.getPort());
        }
        return monitorDataServer;
    }

    @Bean
    @ConditionalOnBean({IdGenerateService.class})
    public DataIdFactory dataIdFactory(List<IdGenerateService> idGenerateServices, IdChangeCallback idChangeCallback) {
        return new DataIdFactory(idGenerateServices, idChangeCallback);
    }

    @Bean
    @ConditionalOnBean({AbstractSplitDataProcess.class})
    public SplitDataManager splitDataManager(List<AbstractSplitDataProcess> splitDataProcessList) {
        return new SplitDataManager(splitDataProcessList);
    }

    @Bean
    @ConditionalOnProperty(prefix = "spring.alert", value = "storeType", havingValue = "LOCAL", matchIfMissing = true)
    public ConfigStoreManager localConfigManager() {
        return new DefaultLocalConfigManager();
    }

    @Bean
    @ConditionalOnProperty(prefix = "spring.alert", value = "storeType", havingValue = "MYSQL")
    public ConfigStoreManager mysqlConfigStoreManager() {
        return new MysqlConfigStoreManager();
    }

}
