package kr.co.uplus.ez.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import com.zaxxer.hikari.HikariDataSource;

@Configuration
public class MultiDatasourceConfig {

	@Value("${mybatis.mapper-locations}")
    private String mapperLocations;

    @Value("${mybatis.config-location}")
    private String configPath;

    // DB1 Configuration Start
	@Bean
	@Qualifier("dataSourceDb1")
	@ConfigurationProperties(prefix = "spring.datasource-db1")
	public DataSource dataSourceDb1() {
		return DataSourceBuilder.create().type(HikariDataSource.class).build();
	}

	@Bean
	@Primary
	public SqlSessionFactory db1SessionFactory() throws Exception {
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		Resource mybatisConfig = resolver.getResource(configPath);
		Resource[] resource = resolver.getResources(mapperLocations);

		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSourceDb1());
		factoryBean.setVfs(SpringBootVFS.class);
		factoryBean.setConfigLocation(mybatisConfig);
		factoryBean.setMapperLocations(resource);
		return factoryBean.getObject();
	}

	@Bean
	public PlatformTransactionManager db1TransactionManager() {
		return new DataSourceTransactionManager(dataSourceDb1());
	}

	@Bean
	@Qualifier("sqlSessionTemplateDb1")
	public SqlSessionTemplate sqlSessionTemplate() throws Exception {
		SqlSessionTemplate template = new SqlSessionTemplate(db1SessionFactory());
		return template;
	}
	// DB1 Configuration End


	// DB2 Configuration Start
	@Bean
	@Qualifier("dataSourceDb2")
	@ConfigurationProperties(prefix = "spring.datasource-db2")
	public DataSource dataSourceDb2() {
		return DataSourceBuilder.create().type(HikariDataSource.class).build();
	}

	@Bean
	public SqlSessionFactory db2SessionFactory() throws Exception {
		ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		Resource mybatisConfig = resolver.getResource(configPath);
		Resource[] resource = resolver.getResources(mapperLocations);

		SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
		factoryBean.setDataSource(dataSourceDb2());
		factoryBean.setVfs(SpringBootVFS.class);
		factoryBean.setConfigLocation(mybatisConfig);
		factoryBean.setMapperLocations(resource);
		return factoryBean.getObject();
	}

	@Bean
	public PlatformTransactionManager db2TransactionManager() {
		return new DataSourceTransactionManager(dataSourceDb2());
	}

	@Bean
	@Qualifier("sqlSessionTemplateDb2")
	public SqlSessionTemplate sqlSessionTemplateDb2() throws Exception {
		SqlSessionTemplate template = new SqlSessionTemplate(db2SessionFactory());
		return template;
	}
	// DB2 Configuration End

}
