package com.viettel.campaign.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;

/**
 * @author anhvd_itsol
 */

@Configuration
@PropertySource({ "classpath:application.yml" })
@EnableJpaRepositories(
        basePackages = "com.viettel.campaign.repository.ccms_full",
        entityManagerFactoryRef = "ccmsFullEntityManager",
        transactionManagerRef = DataSourceQualify.CCMS_FULL
)
public class CCMSFullDatasourceConfig {
    @Autowired
    private Environment env;

    @Bean("datasource1.datasource")
    public DataSource dataSource(){
        OracleDataSource r = new OracleDataSource();
        r.setDriverClassName(env.getProperty("spring.datasource.driver-class-name"));
        r.setPoolName("app.datasource1");
        r.setJdbcUrl(env.getProperty("spring.datasource.url"));
        r.setMaximumPoolSize(env.getProperty("spring.datasource.max_pool_size", Integer.class));
        r.setPassword(env.getProperty("spring.datasource.password"));
        r.setUsername(env.getProperty("spring.datasource.username"));
        return r;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean ccmsFullEntityManager() {
        LocalContainerEntityManagerFactoryBean em
                = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(
                new String[] { "com.viettel.campaign.model.ccms_full" });
        em.setPersistenceUnitName(DataSourceQualify.JPA_UNIT_NAME_CCMS_FULL); // Important !!
        HibernateJpaVendorAdapter vendorAdapter
                = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        HashMap<String, Object> properties = new HashMap<>();
        properties.put("hibernate.dialect", env.getProperty("spring.jpa.database-platform"));
        em.setJpaPropertyMap(properties);

        return em;
    }

    @Bean(name = DataSourceQualify.CCMS_FULL)
    public PlatformTransactionManager ccmsFullTransactionManager() {
        JpaTransactionManager transactionManager
                = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(
                ccmsFullEntityManager().getObject());
        return transactionManager;
    }

    @Bean(name = DataSourceQualify.NAMED_JDBC_PARAMETER_TEMPLATE_CCMS_FULL)
    @DependsOn("datasource1.datasource")
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate(@Qualifier("datasource1.datasource") DataSource abcDataSource) {
        return new NamedParameterJdbcTemplate(abcDataSource);
    }
}
