package usda.weru.remoteDataAccess.csip.polygon.soil;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import usda.weru.remoteDataAccess.RdaHierarchicalItem;
import static usda.weru.remoteDataAccess.csip.CsipInputControllerCachingSupport.PROP_CSIP_BGCATALOG_COMPLETE;
import usda.weru.remoteDataAccess.csip.CsipLmodUtil;
import usda.weru.remoteDataAccess.csip.CsipInterface;
import usda.weru.remoteDataAccess.csip.polygon.CsipInputContPolygonBgCache;
import usda.weru.remoteDataAccess.exceptions.RdaConnectException;
import usda.weru.util.ConfigData;
import static usda.weru.weps.gui.ConfigPanel_n.CSIP_SOIL_POLYGONSIZE_PROP;

/**
 *
 * @author mhaas
 */
public class CsipInputContSoilBgCache extends CsipInputContPolygonBgCache {
    
    public CsipInputContSoilBgCache () {
        this (false);
    }
    
    public CsipInputContSoilBgCache (boolean fromSubClass) {
        super(fromSubClass);
        
        if (ConfigData.checkParmValue("CD-WS-csip-Soil-DoLoadCache","1")) {
            doCacheBackgroundLoad = true;
        }
        
        setPolygonPropName(CSIP_SOIL_POLYGONSIZE_PROP);
        
        cachesupport = new CsipInputContSoilCachingSupport(this);
        cachesupport.setBaseCacheDir(new File (cachesupport.getBaseCacheDir(), CsipLmodUtil.WepsCacheDirNameSoil) );
        // Date vals to trigger clearing soils cache: anything before this is cleared
        cachesupport.checkAndClearCacheDir(2021, 2, 4);
        cacheloader = new CsipInputContSoilCacheLoader (this, cachesupport);
        changes.addPropertyChangeListener(PROP_CSIP_BGCATALOG_COMPLETE,this);
        
        ((CsipInterfaceSoil)csipIf).setCacheSupport((CsipInputContSoilCachingSupport)cachesupport);
    }
    
    
    @Override
    public CsipInputContSoilCachingSupport getCachingSupport() {
        return (CsipInputContSoilCachingSupport)cachesupport;
    }
    
    @Override
    protected CsipInterface getNewCsipInterface() {
        return new CsipInterfaceSoil ();
    }
    
    @Override
    protected CsipInputContPolygonBgCache getNewCsipInputContPolygonBgCache() {
        return new CsipInputContSoilBgCache ();
    }
    
//    @Override
//    public void propertyChange(PropertyChangeEvent evt) {
//        if (evt.getPropertyName().contentEquals(PROP_CSIP_BGFILLCACHE_COMPLETE_INTERNAL)) {
//            // MEH location
//            // for soils
//            // Need to update serviceResults var
//            // it still has val from original csip catalog call
////            updateServiceResults();
//        }
//        super.propertyChange(evt);
//    }
//    
//    protected void updateServiceResults () {
//        ((CsipInputContSoilCachingSupport)cachesupport).loadSoilCacheProperties();
//        for (int i = 0; i < serviceResults.size(); i++) {
//            String s = serviceResults.get(i);
//            s = s.replace('\\', '/');
//            String s2 = ((CsipInputContSoilCachingSupport)cachesupport).updateSurveyName(new File (s)).getPath();
//            String s3 = new File (s2).getName();
//            s3 = ((CsipInputContSoilCachingSupport)cachesupport).updateName(s3);
//            String s4 = new File (new File (s2).getParentFile(),s3).getPath();
//            serviceResults.set(i, s4);
////            serviceResults.set(i, s2);
//        }
//    }
    
    @Override
    public File getFile(RdaHierarchicalItem item) throws RdaConnectException {
        File cacheFile = cachesupport.getFileCaching(item, csipIf);
        
        String destFileName = cacheFile.getName();
        destFileName = ((CsipInputContSoilCachingSupport)cachesupport).updateName(destFileName);
        File destFile = new File (getInterface().getDestinationPath().toString(), destFileName);
        
        try {
            FileUtils.copyFile(cacheFile, destFile, true);
        } catch (IOException ex) {
            throw (new RdaConnectException("Could not copy soil file to project", ex, "Csip Soil Bg Cache"));
        }
        return destFile;
    }
}
