package cz.hsrs.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Logger;

import cz.hsrs.db.pool.SQLExecutor;

class ConnectionManager {

	private static Connection conn = null;
	private static Connection connFeeder = null;
	private static Properties prop;	
	
	private static String UnitsPositions_table;
	private static String UnitsTracks_table;
	private static String UnitsLastPositions_table;
		
	
	public static Logger logger = Logger.getLogger(SQLExecutor.LOGGER_ID);
	
	@Deprecated
	private ConnectionManager() { }

	public static void setProperties(Properties proper) throws SQLException {
			prop = proper;
			UnitsPositions_table = prop.getProperty("UnitsPositions_table");
			UnitsTracks_table = prop.getProperty("UnitsTracks_table");
			UnitsLastPositions_table = prop.getProperty("UnitsLastPositions_table");
	}

	public static String getUnitsPositions_table() {
		return UnitsPositions_table;
	}

	public static String getUnitsTracks_table() {
		return UnitsTracks_table;
	}

	public static String getUnitsLastPositions_table() {
		return UnitsLastPositions_table;
	}
	
  
	public static synchronized Connection getConnectionnn() throws SQLException  {
		if (conn != null && !conn.isClosed()) {
			return conn;
		}

		try {
			Class.forName("org.postgresql.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			throw new SQLException(e);
		}

		try {
			String address = (String) prop.get("Address");
			String username = (String) prop.get("Username");
			String password = (String) prop.get("Password");
			conn = DriverManager.getConnection(address, username, password);
			return conn;
		} catch (NullPointerException e) {
			throw new NullPointerException(
					"You have to setProperties before getting Connection");
		}
	}
  
	
	public static synchronized Connection getConnectionFeeder() throws SQLException  {
		if (connFeeder != null && !connFeeder.isClosed()) {
			return connFeeder;
		}

		try {
			Class.forName("org.postgresql.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			throw new SQLException(e);
		}

		try {
			String address = (String) prop.get("Address");
			String username = (String) prop.get("Username");
			String password = (String) prop.get("Password");
			connFeeder = DriverManager.getConnection(address, username, password);
			return connFeeder;
		} catch (NullPointerException e) {
			throw new NullPointerException("You have to setProperties before getting Connection");
		}
	}
	
	public static void closeConnection() throws SQLException {
		conn.close();		
	}
	
	public static void closeConnectionFeeder() throws SQLException {
		conn.close();		
	}
}
