package com.lingniu.ingest.tdenginehistory.config;

import com.lingniu.ingest.tdenginehistory.TdengineHistorySchema;
import com.lingniu.ingest.tdenginehistory.TdengineHistoryStatements;
import com.lingniu.ingest.tdenginehistory.TdengineHistoryReader;
import com.lingniu.ingest.tdenginehistory.TdengineHistoryWriter;
import com.lingniu.ingest.tdenginehistory.TdengineJdbcHistoryReader;
import com.lingniu.ingest.tdenginehistory.TdengineJdbcHistoryWriter;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

@AutoConfiguration
@EnableConfigurationProperties(TdengineHistoryProperties.class)
public class TdengineHistoryAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(
            prefix = "lingniu.ingest.tdengine-history",
            name = "enabled",
            havingValue = "true")
    public TdengineHistorySchema tdengineHistorySchema(TdengineHistoryProperties properties) {
        return new TdengineHistorySchema(properties.getDatabase());
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(
            prefix = "lingniu.ingest.tdengine-history",
            name = "enabled",
            havingValue = "true")
    public TdengineHistoryStatements tdengineHistoryStatements(TdengineHistorySchema schema) {
        return new TdengineHistoryStatements(schema);
    }

    @Bean
    @ConditionalOnMissingBean(DataSource.class)
    @ConditionalOnProperty(
            prefix = "lingniu.ingest.tdengine-history",
            name = "enabled",
            havingValue = "true")
    public DataSource tdengineDataSource(TdengineHistoryProperties properties) {
        HikariConfig config = new HikariConfig();
        config.setPoolName("tdengine-history");
        config.setDriverClassName(properties.getDriverClassName());
        config.setJdbcUrl(properties.getJdbcUrl());
        config.setUsername(properties.getUsername());
        config.setPassword(properties.getPassword());
        config.setMaximumPoolSize(Math.max(1, properties.getMaximumPoolSize()));
        config.setMinimumIdle(Math.max(0, Math.min(properties.getMinimumIdle(), properties.getMaximumPoolSize())));
        config.setConnectionTimeout(Math.max(250, properties.getConnectionTimeoutMillis()));
        config.setInitializationFailTimeout(properties.getInitializationFailTimeoutMillis());
        return new HikariDataSource(config);
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean(DataSource.class)
    @ConditionalOnProperty(
            prefix = "lingniu.ingest.tdengine-history",
            name = "enabled",
            havingValue = "true")
    public TdengineHistoryWriter tdengineHistoryWriter(DataSource dataSource, TdengineHistorySchema schema) {
        return new TdengineJdbcHistoryWriter(dataSource, schema);
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnBean(DataSource.class)
    @ConditionalOnProperty(
            prefix = "lingniu.ingest.tdengine-history",
            name = "enabled",
            havingValue = "true")
    public TdengineHistoryReader tdengineHistoryReader(DataSource dataSource, TdengineHistorySchema schema) {
        return new TdengineJdbcHistoryReader(dataSource, schema);
    }
}
