package com.munjaon.client.config;

import com.zaxxer.hikari.HikariDataSource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Slf4j
@Configuration
@RequiredArgsConstructor
@EnableTransactionManagement
@MapperScan(basePackages= "com.munjaon.client.**.mapper")
public class DataSourceConfig {
    //public class DataSourceConfig implements BeanDefinitionRegistryPostProcessor {
    @Autowired
    private ApplicationContext applicationContext;
    private final ServerConfig serverConfig;

    @Primary
    @Bean(name = "datasource")
    public DataSource dataSource() throws ConfigurationException {
        String dbms = serverConfig.getString("DB.DBMS");
        System.out.println("MARIADB.DRIVER : " + serverConfig.getString(dbms + ".DRIVER"));
        System.out.println("MARIADB.URL : " + serverConfig.getString(dbms + ".URL"));
        System.out.println("MARIADB.USER : " + serverConfig.getString(dbms + ".USER"));
        System.out.println("MARIADB.PASSWORD : " + serverConfig.getString(dbms + ".PASSWORD"));
        return DataSourceBuilder.create().type(HikariDataSource.class)
                .driverClassName(serverConfig.getString(dbms + ".DRIVER"))
                .url(serverConfig.getString(dbms + ".URL"))
                .username(serverConfig.getString(dbms + ".USER"))
                .password(serverConfig.getString(dbms + ".PASSWORD"))
                .build();
    }

    @Primary
    @Bean(name = "factory")
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        String dbms = serverConfig.getString("DB.DBMS");
        System.out.println("dbms : " + dbms);
        SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
        sqlSessionFactory.setVfs(SpringBootVFS.class);
        sqlSessionFactory.setDataSource(dataSource);
        sqlSessionFactory.setTypeAliasesPackage("com.munjaon.client.**.dto");
        sqlSessionFactory.setConfigLocation(applicationContext.getResource("classpath:mybatis-config.xml"));
        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/sqlmap/**/*.xml"));
//        sqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/sqlmap/" + dbms.toLowerCase() + "/*.xml"));
        return sqlSessionFactory.getObject();
    }

    @Primary
    @Bean(name = "sqlSession")
    public SqlSessionTemplate sqlSession(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

//    @Override
//    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
//        String dbms = null;
//        try {
//            dbms = serverConfig.getString("DB.DBMS");
//        } catch (ConfigurationException e) {
//            throw new RuntimeException(e);
//        }
//        AbstractBeanDefinition mapperScannerConfigurer = BeanDefinitionBuilder
//                .genericBeanDefinition(MapperScannerConfigurer.class)
//                .addPropertyValue("sqlSessionFactoryBeanName", "factory")
//                .addPropertyValue("basePackage", "com.munjaon.client.**.dto")
//                .addPropertyValue("annotationClass", MapperType.value(dbms).getClass())
//                .getBeanDefinition();
//
//        registry.registerBeanDefinition("MapperScanner", mapperScannerConfigurer);
//    }
}
