package com.xt.hb.data.system;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.alibaba.druid.pool.DruidDataSource;

public class DataSourceFactory implements FactoryBean<DataSource>, InitializingBean {
    Logger logger = LoggerFactory.getLogger(DataSourceFactory.class);
    private Resource[] properties;
    private DataSource ds;
    private String fileEncoding;
    /** 连接池数据源类型 */
    private String dsType = "com.alibaba.druid.pool.DruidDataSource";

    public void close() {
        if (ds instanceof DruidDataSource) {
            ((DruidDataSource) ds).close();
        }
    }

    public void init() {
        if (ds instanceof DruidDataSource) {
            try {
                ((DruidDataSource) ds).init();
            }
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public DataSource getObject() throws Exception {
        return ds;
    }

    @Override
    public Class<?> getObjectType() {
        return DataSource.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    private void loadProperties(Properties props) {
        for (Resource location : this.properties) {
            if (logger.isInfoEnabled()) {
                logger.info("Loading properties file from " + location);
            }
            try {
                PropertiesLoaderUtils.fillProperties(props, new EncodedResource(location, this.fileEncoding));
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        Properties properties = new Properties();
        loadProperties(properties);
        if (dsType.equals("com.alibaba.druid.pool.DruidDataSource")) {
            DruidDataSource druidDataSource = new DruidDataSource();
            druidDataSource.configFromPropety(properties);
            {
                String property = properties.getProperty("druid.url");
                if (property != null && property.length() > 0) {
                    druidDataSource.setUrl(property);
                }
            }
            {
                String property = properties.getProperty("druid.username");
                if (property != null && property.length() > 0) {
                    druidDataSource.setUsername(property);
                }
            }
            {
                String property = properties.getProperty("druid.password");
                if (property != null && property.length() > 0) {
                    druidDataSource.setPassword(property);
                }
            }
            {
                String property = properties.getProperty("druid.initialSize");
                if (property != null && property.length() > 0) {
                    druidDataSource.setInitialSize(Integer.parseInt(property));
                }
            }
            {
                String property = properties.getProperty("druid.minIdle");
                if (property != null && property.length() > 0) {
                    druidDataSource.setMinIdle(Integer.parseInt(property));
                }
            }
            {
                String property = properties.getProperty("druid.maxActive");
                if (property != null && property.length() > 0) {
                    druidDataSource.setMaxActive(Integer.parseInt(property));
                }
            }
            {
                String property = properties.getProperty("druid.timeBetweenEvictionRunsMillis");
                if (property != null && property.length() > 0) {
                    druidDataSource.setTimeBetweenEvictionRunsMillis(Long.parseLong(property));
                }
            }
            {
                String property = properties.getProperty("druid.minEvictableIdleTimeMillis");
                if (property != null && property.length() > 0) {
                    druidDataSource.setMinEvictableIdleTimeMillis(Integer.parseInt(property));
                }
            }
            {
                String property = properties.getProperty("druid.testOnReturn");
                if ("true".equals(property)) {
                    druidDataSource.setTestOnReturn(true);
                }
                else if ("false".equals(property)) {
                    druidDataSource.setTestOnReturn(false);
                }
            }
            {
                String property = properties.getProperty("druid.poolPreparedStatements");
                if ("true".equals(property)) {
                    druidDataSource.setPoolPreparedStatements(true);
                }
                else if ("false".equals(property)) {
                    druidDataSource.setPoolPreparedStatements(false);
                }
            }
            {
                String property = properties.getProperty("druid.maxPoolPreparedStatementPerConnectionSize");
                if (property != null && property.length() > 0) {
                    druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(Integer.parseInt(property));
                }
            }
            ds = druidDataSource;
        }
    }

    public void setDsType(String dsType) {
        this.dsType = dsType;
    }

    public void setProperties(Resource[] properties) {
        this.properties = properties;
    }
}
