package com.viettel.campaign.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.*;
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.acd_full",
        entityManagerFactoryRef = "acdFullEntityManager",
        transactionManagerRef = DataSourceQualify.ACD_FULL
)
public class ACDFullDatasourceConfig {
    @Autowired
    private Environment env;

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

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

        return em2;
    }

    @Bean(name = DataSourceQualify.ACD_FULL)
    public PlatformTransactionManager acdFullTransactionManager() {
        JpaTransactionManager transactionManager
                = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(
                acdFullEntityManager().getObject());
        return transactionManager;
    }

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

