
package usda.weru.remoteDataAccess.jdbc.soil;

import java.io.File;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.ArrayList;
import javax.swing.ProgressMonitor;
import usda.weru.remoteDataAccess.RdaInterface;
import usda.weru.remoteDataAccess.exceptions.RdaConnectException;
import usda.weru.soil.JdbcSoilDatabase;
import usda.weru.util.WepsMessageLog;

/**
 *
 * @author mhaas
 */
public abstract class JdbcSoilInterface extends RdaInterface {
    
    public enum soilElementType {
        database,
        legend,
        mapunit,
        component
    };
    
    public static class SoilCatParms {
        
        String name;
        // for legend types
        String c_lkey;
        String c_areaname;
        String c_areasymbol;
        // for map unit types
        String c_mukey;
        String c_muname;
        String c_musym;
        // for component types
        String c_cokey;
        String c_compname;
        String c_localphase;
        String c_comppct_r;
        
        protected SoilCatParms (String name) {
            this.name = name;
        }
        
        protected SoilCatParms setLegendVals (String c_lkey, String c_areaname, String c_areasymbol) {
            this.c_lkey = c_lkey;
            this.c_areaname = c_areaname;
            this.c_areasymbol = c_areasymbol;
            return this;
        }
        protected SoilCatParms setMapunitVals (String c_lkey, String c_mukey, String c_muname, String c_musym) {
            this.c_lkey = c_lkey;
            this.c_mukey = c_mukey;
            this.c_compname = c_muname;
            this.c_localphase = c_musym;
            return this;
        }
        protected SoilCatParms setComponentVals (String c_cokey, String c_mukey, String c_compname, String c_localphase, String c_comppct_r) {
            this.c_cokey = c_cokey;
            this.c_mukey = c_mukey;
            this.c_compname = c_compname;
            this.c_localphase = c_localphase;
            this.c_comppct_r = c_comppct_r;
            return this;
        }
    }
     
    SoilCatParms parms;
    protected Connection soilsDbConnection;
    protected JdbcSoilDatabase soilsDatabase = null;
    protected String soilsDbHostName;

    protected soilElementType ifType = soilElementType.database;

    
    public JdbcSoilInterface () {
        super();
        this.parms = new SoilCatParms ("");
    }
    public JdbcSoilInterface (SoilCatParms parms) {
        this();
        this.parms = parms;
    }
    public JdbcSoilInterface (JdbcSoilInterface parent, SoilCatParms parms) {
        this();
        if (parms != null) {
            this.parms = parms;
        } else {
            this.parms = parent.parms;
        }
        
        this.soilsDatabase = parent.soilsDatabase;
        this.soilsDbConnection = parent.soilsDbConnection;
        this.soilsDbHostName = parent.soilsDbHostName;

        this.interfaceSetDestinationPath(parent.getDestinationPath());
    }
   
    public void initialize () throws RdaConnectException {
        throw new UnsupportedOperationException("Generic initialize() not overridden in:" + this.getClass());        
    }
    
    
//    @Override
//     protected void interfaceSetDestinationPath (Path destPath) {
//        super.interfaceSetDestinationPath(destPath);
//    }
    
    // For use in SoilInterfaceComponent (overridden there)
    protected WepsMessageLog getRecordLog () {
        return null;
    }
        
    protected ArrayList<SoilCatParms> callSoilCatalog () throws RdaConnectException {
        // Not needed.  A component is always a file
        throw new UnsupportedOperationException("Invalid to seek catalog of: " + this.getClass());
    }   
    
    protected File callSoilGetRecord(String name, String parentPath) throws RdaConnectException {
        // Mneeds a specific implementation / override still
        throw new UnsupportedOperationException("Invalid to seek record of: " + this.getClass());
    }
    
    // default, unless overridden
    protected File callSoilGetRecord(String name, String parentPath, ProgressMonitor progress) throws RdaConnectException {
        return callSoilGetRecord(name, parentPath);
    }
    
    
    // support methods
    
    // copied from JdbcSoilDatabase.java
    protected static boolean checkForWarning(SQLWarning warn) throws SQLException {
        boolean rc = false;

        // If a SQLWarning object was given, display the
        // warning messages.  Note that there could be
        // multiple warnings chained together
        if (warn != null) {
            while (warn != null) {
                int code = warn.getErrorCode();
                switch (code) {
                    case 0:
                        warn = warn.getNextWarning();
                        continue;
                    case 1: //Readonly
                        warn = warn.getNextWarning();
                        continue;
                    case 63: //Can't read a registry setting
                        warn = warn.getNextWarning();
                        continue;
                    default:
                }
                if (!rc) {
                    //System.err.println("*** JDBC ODBC WARNINGS ***");
                }
                rc = true;
                //System.err.println("SQLState: " +
                //warn.getSQLState());
                //System.err.println("Message:  " +
                //warn.getMessage());
                //System.err.println("Error:   " +
                //warn.getErrorCode());
                //System.err.println("");
                //warn = warn.getNextWarning();
            }
            if (rc) {
                //System.err.println("*** *** *** *** *** ***");
            }
        }
        return rc;
    }

    public String getString(ResultSet rs, String column) {
        try {
            if (rs == null || column == null) {
                return null;
            }
            Object temp = rs.getString(column);
            if (temp != null) {
                return temp.toString();
            }
        } catch (SQLException se) {
            return null;
        }
        return null;
    }
}
