package com.its.web.config;

import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
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.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.io.IOException;

@Profile("itcs")
@Slf4j
@ToString
@Configuration
@MapperScan(
        basePackages="com.its.web.mapper.itcs",
        sqlSessionFactoryRef = "itcsSqlSessionFactory")
@EnableTransactionManagement
public class DatabaseItcsConfig {
    @Value("${spring.itcs-datasource.hikari.mapper-locations:classpath:mybatis/mapper/itcs/**/*.xml}")
    String mapperLocations;

    @PostConstruct
    private void init() {
        if (this.mapperLocations.trim().equals("")) {
            this.mapperLocations = "classpath:mybatis/mapper/itcs/**/*.xml";
        }
        log.info("mapperLocations: {}", this.mapperLocations);
        log.info("{}", this);
    }

    @Bean(name="itcsDataSource")
    @ConfigurationProperties(prefix="spring.itcs-datasource.hikari")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name="itcsSqlSessionFactory")
    public SqlSessionFactory sqlSessionFactoryBean(
            @Autowired @Qualifier("itcsDataSource") DataSource dataSource, ApplicationContext applicationContext) {
        log.info("dataSource : {}", dataSource);
        log.info("mapperLocations: {}", this.mapperLocations);
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource);
        try {
            factoryBean.setMapperLocations(applicationContext.getResources(this.mapperLocations));
            factoryBean.setConfigLocation(applicationContext.getResource("classpath:mybatis/mapper-config.xml"));
            return factoryBean.getObject();
        } catch (IOException ie) {
            log.error("sqlSessionFactoryBean: IOException");
        } catch (Exception e) {
            log.error("sqlSessionFactoryBean: Exception");
        }
        return null;
    }

    @Bean(name="itcsSqlSession")
    public SqlSessionTemplate sqlSession(
            @Autowired @Qualifier("itcsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean(name="itcsSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(
            @Autowired @Qualifier("itcsSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
