package daruma.global_switch;

import daruma.util.PropertyReader;
import daruma.util.FatalException;

public class ImplementationSwitches
{
    //
    // geometry conversion
    //
    private static final String ENABLE_GEOMETRY_CONVERSION_PROPERTY
				= "daruma.geometry.enable_conversion";
    private static final boolean ENABLE_GEOMETRY_CONVERSION_DEFAULT
				 = true;
    private boolean enableGeometryConversion;


    private static final String ALLOW_CONVERSION_TO_LOWER_DIMENSION_PROPERTY
		= "daruma.geometry.allow_conversion_to_lower_dimension";
    private static final String ALLOW_CONVERSION_TO_UPPER_DIMENSION_PROPERTY
		= "daruma.geometry.allow_conversion_to_upper_dimension";
    private static final boolean ALLOW_CONVERSION_TO_LOWER_DIMENSION_DEFAULT
				 = true;
    private static final boolean ALLOW_CONVERSION_TO_UPPER_DIMENSION_DEFAULT
				 = true;
    private boolean allowConversionToLowerDimension;
    private boolean allowConversionToUpperDimension;

    //
    // db index
    //
    private static final String CREATE_GEOMETRY_INDEX_PROPERTY
		= "daruma.geometry.create_geometry_index";
    private static final boolean CREATE_GEOMETRY_INDEX_DEFAULT = false;
    private boolean createGeometryIndex = false;



    //
    // backend database type
    //
    public enum BackendDBType { PostGIS, MySQL };
    private String BACKEND_DB_TYPE_PROPERTY = "daruma.db.backend";
    private static final String BACKEND_DB_TYPE_DEFAULT = "MySQL";
    private BackendDBType backendDBType;

    //
    // MySQL host & port
    //
    private String MYSQL_HOST_PROPERTY = "daruma.mysql.host";
    private String MYSQL_PORT_PROPERTY = "daruma.mysql.port";
    private static final String MYSQL_HOST_DEFAULT = "localhost";
    private static final int MYSQL_PORT_DEFAULT = 3306;
    private static String mysqlHost;
    private static int mysqlPort;

    public ImplementationSwitches() throws FatalException
    {
	this.enableGeometryConversion
	    = PropertyReader.getProperty
	      ( ENABLE_GEOMETRY_CONVERSION_PROPERTY,
		ENABLE_GEOMETRY_CONVERSION_DEFAULT );

	this.allowConversionToLowerDimension
	    = PropertyReader.getProperty
	      ( ALLOW_CONVERSION_TO_LOWER_DIMENSION_PROPERTY,
		ALLOW_CONVERSION_TO_LOWER_DIMENSION_DEFAULT );

	this.allowConversionToUpperDimension
	    = PropertyReader.getProperty
	      ( ALLOW_CONVERSION_TO_UPPER_DIMENSION_PROPERTY,
		ALLOW_CONVERSION_TO_UPPER_DIMENSION_DEFAULT );

	this.createGeometryIndex
	    = PropertyReader.getProperty
	      ( CREATE_GEOMETRY_INDEX_PROPERTY,
		CREATE_GEOMETRY_INDEX_DEFAULT );

	String backendDBTypeString 
	    = PropertyReader.getProperty
	      ( BACKEND_DB_TYPE_PROPERTY,
		BACKEND_DB_TYPE_DEFAULT );

	if ( backendDBTypeString.equals( "PostGIS" ) )
	{
	    this.backendDBType = BackendDBType.PostGIS;
	}
	else if ( backendDBTypeString.equals( "MySQL" ) )
	{
	    this.backendDBType = BackendDBType.MySQL;
	}
	else
	{
	    throw new FatalException
		( "invalid property value of ["
		  + BACKEND_DB_TYPE_PROPERTY + "],"
		  + " value = [" + this.backendDBType + "],"
		  + " expected was [PostGIS] or [MySQL]." );
	}

	this.mysqlHost
	    = PropertyReader.getProperty
	      ( MYSQL_HOST_PROPERTY,
		MYSQL_HOST_DEFAULT );

	this.mysqlPort
	    = PropertyReader.getProperty
	      ( MYSQL_PORT_PROPERTY,
		MYSQL_PORT_DEFAULT );
    }

    public boolean getEnableGeometryConversion()
    {
	return enableGeometryConversion;
    }

    public boolean getAllowConversionToLowerDimension()
    {
	return allowConversionToLowerDimension;
    }

    public boolean getAllowConversionToUpperDimension()
    {
	return allowConversionToUpperDimension;
    }

    public boolean getCreateGeometryIndex()
    {
	return createGeometryIndex;
    }

    public BackendDBType getBackendDBType()
    {
	return backendDBType;
    }

    public boolean isPostGISBackend()
    {
	return backendDBType == BackendDBType.PostGIS;
    }

    public boolean isMySQLBackend()
    {
	return backendDBType == BackendDBType.MySQL;
    }

    public String getMySQLHost()
    {
	return mysqlHost;
    }

    public int getMySQLPort()
    {
	return mysqlPort;
    }



    //
    // singleton
    //
    private static ImplementationSwitches obj = null;

    public static void createInstance() throws FatalException
    {
	if ( obj == null )
	{
	    obj = new ImplementationSwitches();
	}
    }

    public static ImplementationSwitches instance()
    {
	return obj;
    }
}
