package org.firebirdsql.gds.impl.jni;

import org.firebirdsql.gds.GDS;
import org.firebirdsql.gds.GDSException;
import org.firebirdsql.gds.impl.GDSFactoryPlugin;
import org.firebirdsql.jdbc.FBConnection;

public class EmbeddedGDSFactoryPlugin implements GDSFactoryPlugin {

    private static final String[] TYPE_ALIASES = new String[0];

    private static final String[] JDBC_PROTOCOLS = new String[] { 
        "jdbc:firebirdsql:embedded:"
    };

    private static GDS gds;
    
    public String getPluginName() {
        return "GDS implementation for embedded server.";
    }

    public String getTypeName() {
        return EmbeddedGDSImpl.EMBEDDED_TYPE_NAME;
    }

    public String[] getTypeAliases() {
        return TYPE_ALIASES;
    }

    public Class getConnectionClass() {
        return FBConnection.class;
    }

    public String[] getSupportedProtocols() {
        return JDBC_PROTOCOLS;
    }

    public synchronized GDS getGDS() {
        if (gds == null)
            gds = applySyncPolicy(new EmbeddedGDSImpl());
        
        return gds;
    }

    /**
     * Apply the synchronization policy if the current platform is not Windows.
     * @param tempGds instance if {@link GDS} to which policy should be applied.
     */
    public static GDS applySyncPolicy(GDS tempGds) {
        GDSSynchronizationPolicy.AbstractSynchronizationPolicy syncPolicy = null;

        String osName = System.getProperty("os.name");
        if (osName != null && osName.indexOf("Windows") == -1)
            syncPolicy = new GDSSynchronizationPolicy.ClientLibrarySyncPolicy(tempGds);

        if (syncPolicy != null)
            return GDSSynchronizationPolicy.applySyncronizationPolicy(tempGds, syncPolicy);
        else
            return tempGds;
    }

    
    public String getDatabasePath(String server, Integer port, String path)
            throws GDSException {
        return path;
    }

    public String getDatabasePath(String jdbcUrl) throws GDSException {
        String[] protocols = getSupportedProtocols();
        for (int i = 0; i < protocols.length; i++) {
            if (jdbcUrl.startsWith(protocols[i]))
                return jdbcUrl.substring(protocols[i].length());
        }

        throw new IllegalArgumentException("Incorrect JDBC protocol handling: "
                + jdbcUrl);
    }

    public String getDefaultProtocol() {
        return getSupportedProtocols()[0];
    }
    
    public int hashCode() {
        return getTypeName().hashCode();
    }
    
    public boolean equals(Object obj) {
        if (obj == this) 
            return true;
        
        if (!(obj instanceof EmbeddedGDSFactoryPlugin))
            return false;
        
        return true;
    }   
}
