
package usda.weru.remoteDataAccess.jdbc.soil;

import java.io.File;
import javax.swing.ProgressMonitor;
import usda.weru.remoteDataAccess.exceptions.RdaConnectException;
import usda.weru.soil.IFC;
import usda.weru.soil.NASIS;
import usda.weru.soil.OrganicSoilException;
import usda.weru.util.ConfigData;
import usda.weru.util.WepsMessageLog;
import usda.weru.util.wepsFileChooser2.WepsFileTypes2;
import usda.weru.weps.Weps;

/**
 *
 * @author mhaas
 */
public class JdbcSoilInterfaceComponent extends JdbcSoilInterface {
       
    IFC currentIfc;
    
    public JdbcSoilInterfaceComponent (SoilCatParms parms) {
        super(parms);
        currentIfc = null;
    }
    public JdbcSoilInterfaceComponent (JdbcSoilInterface parent, SoilCatParms parms) {
        super(parent, parms);
        currentIfc = null;
    }
    
    @Override
    protected File callSoilGetRecord(String name, String parentPath) throws RdaConnectException {
        return callSoilGetRecord(name, parentPath, null);
    }
    
    
    @Override
    protected File callSoilGetRecord(String name, String parentPath, ProgressMonitor progress) throws RdaConnectException {
                    
        NASIS nasis = new NASIS(soilsDbConnection, progress);
        //System.out.println("Is this what gets called?");
        boolean valid = false;
        try {
            valid = nasis.loadSQL(parms.c_cokey);
        } catch(OrganicSoilException e) {
            if(e.getMessage().equals("Default")) {
                String fullPath = ConfigData.getDefault().getDataParsed(ConfigData.SoilOrganicFile);
                String fileName = "Organic Soil.ifc";
                //System.out.println("File Name: " + fileName);
                //System.out.println("Destination Path: " + destinationPath.toString());
                File newFile = new File(destinationPath.toString(), fileName);
                //currentIfc.writeNewIfc(newFile.getAbsolutePath());
                Weps.getInstance().getAuxPanel().runOrganicReplacement();
                return newFile;
            }
        }
        if (!valid) {
            throw (new RdaConnectException("Unable to retrieve soil component data", null, this.interfaceName));
        }
        
        currentIfc = new IFC(nasis);
        currentIfc.sendErrorsToInternalVariable();
        currentIfc.getLog().mergeLog(nasis.getLog());
        
        String fileName = currentIfc.getFileName();
        if (!fileName.endsWith(WepsFileTypes2.Soil.getExtension())) {
            fileName += WepsFileTypes2.Soil.getExtension();
        }
        //System.out.println("Required File Name Format: " + fileName);
//        File newFile = new File (getProperty("project.directory"),fileName);
        File newFile = new File (destinationPath.toString(),fileName);
        try {
            currentIfc.writeNewIfc(newFile.getAbsolutePath());
        } catch(ArithmeticException ae) {
            throw new RdaConnectException();
        }
        return newFile;
    }
    
    @Override
    protected WepsMessageLog getRecordLog () {
        if (currentIfc != null) {
            return currentIfc.getLog();
        } else {
            return null;
        }
    }
    
}
