package com.its.safe.config;

import com.zaxxer.hikari.HikariDataSource;
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.transaction.annotation.EnableTransactionManagement;

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

@Slf4j
@ToString
@Configuration
@MapperScan(
        basePackages="com.its.safe.dao.mapper.safe",
        sqlSessionFactoryRef = "safeSqlSessionFactory")
@EnableTransactionManagement
public class DatabaseSafeConfig {

    @Value("${spring.safe-datasource.hikari.safe.mapper-locations:classpath:mybatis/mapper/safe/**/*.xml}")
    String mapperLocations;

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

    @Bean(name="safeDataSource")
    @ConfigurationProperties(prefix="spring.datasource.hikari.safe")
    public DataSource dataSource() {
        //return DataSourceBuilder.create().build();
        return DataSourceBuilder.create().type(HikariDataSource.class).build();
    }

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

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

//    @Bean(name="safeSqlSessionTemplate")
//    public SqlSessionTemplate sqlSessionTemplate(
//            @Autowired @Qualifier("safeSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
//        return new SqlSessionTemplate(sqlSessionFactory);
//    }

}
