package at.procon.eventhub.yellowfox.config;

import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
@ConditionalOnExpression("T(org.springframework.util.StringUtils).hasText('${eventhub.yellow-fox.datasource.jdbc-url:}')")
public class YellowFoxDataSourceConfig {

    @Bean
    @ConfigurationProperties(prefix = "eventhub.yellow-fox.datasource")
    public YellowFoxDataSourceProperties yellowFoxDataSourceProperties() {
        return new YellowFoxDataSourceProperties();
    }

    @Bean(defaultCandidate = false)
    public DataSource yellowFoxDataSource(YellowFoxDataSourceProperties config) {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(validateJdbcUrl(config));
        dataSource.setUsername(config.getUsername());
        dataSource.setPassword(config.getPassword());
        String driverClassName = trimToNull(config.getDriverClassName());
        if (driverClassName != null) {
            dataSource.setDriverClassName(driverClassName);
        }
        return dataSource;
    }

    @Bean
    public NamedParameterJdbcTemplate yellowFoxNamedParameterJdbcTemplate(
            @Qualifier("yellowFoxDataSource") DataSource yellowFoxDataSource
    ) {
        return new NamedParameterJdbcTemplate(yellowFoxDataSource);
    }

    private String validateJdbcUrl(YellowFoxDataSourceProperties config) {
        String jdbcUrl = trimToNull(config.getJdbcUrl());
        if (jdbcUrl == null) {
            throw new IllegalStateException("eventhub.yellow-fox.datasource.jdbc-url must not be empty");
        }
        return jdbcUrl;
    }

    private String trimToNull(String value) {
        if (value == null) {
            return null;
        }
        String trimmedValue = value.trim();
        return trimmedValue.isEmpty() ? null : trimmedValue;
    }

    public static class YellowFoxDataSourceProperties {
        private String jdbcUrl;
        private String username;
        private String password;
        private String driverClassName;

        public String getJdbcUrl() {
            return jdbcUrl;
        }

        public void setJdbcUrl(String jdbcUrl) {
            this.jdbcUrl = jdbcUrl;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        public String getDriverClassName() {
            return driverClassName;
        }

        public void setDriverClassName(String driverClassName) {
            this.driverClassName = driverClassName;
        }
    }
}
