package usda.weru.util.tree;

import de.schlichtherle.truezip.file.TFile;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileFilter;
import static usda.weru.remoteDataAccess.csip.CsipInputControllerCachingSupport.PROP_CSIP_BGCATALOG_COMPLETE;
import usda.weru.remoteDataAccess.csip.CsipLmodUtil;
import usda.weru.remoteDataAccess.csip.CsipLmodUtil.lmodType;
import usda.weru.util.ConfigData;
import usda.weru.util.About;
import usda.weru.util.wepsFileChooser2.WepsFileTypes2;

/**
 *
 * @author mhaas
 */
public class WepsTreeModelCrop extends WepsTreeModelCrLmod implements PropertyChangeListener, FileFilter {

    private static final long serialVersionUID = 1L;

    WepsTreeNodeFile cropsDbDir;
    WepsTreeNodeFile upgmDbDir;

    public WepsTreeModelCrop(WepsTreeComboBox treeCombo, boolean useWaitCursor) {
        super(treeCombo, useWaitCursor);
    }    

    @Override
    protected void build() {
        super.build();

        if(ConfigData.checkParmValue(ConfigData.locCropCheck, "1")) {
            TFile locRop = new TFile(ConfigData.getDefault().getDataParsed(ConfigData.LocalCropDB));
            if(locRop.exists() && locRop.isDirectory()) {
                WepsTreeNodeFile localCropDbDir = new WepsTreeNodeFile(locRop, this);
                localCropDbDir.setDisplayName("Local Crop and Residue Records");
                addNode(localCropDbDir);
            }
        }
        
        if(ConfigData.checkParmValue(ConfigData.shaCropCheck, "1")) {
            TFile shareCrop = new TFile(ConfigData.getDefault().getDataParsed(ConfigData.localResidueRecords));
            if(shareCrop.exists() && shareCrop.isDirectory()) {
                WepsTreeNodeFile sharedCropDbDir = new WepsTreeNodeFile(shareCrop, this);
                sharedCropDbDir.setDisplayName("Shared Crop and Residue Records");
                addNode(sharedCropDbDir);
            }
            
        }
        
        if(ConfigData.checkParmValue("CD-system_cropres_cb","1")) {
            //System.out.println("Crop Check is 1");
            TFile cyCrop = new TFile(ConfigData.getDefault().getDataParsed(ConfigData.SystemCropDB));
            if(cyCrop.exists() && cyCrop.isDirectory()) {
                cropsDbDir = new WepsTreeNodeFile (cyCrop, this);
                cropsDbDir.setDisplayName("System Crop and Residue Records");
                addNode(cropsDbDir);
            }
            
            // -ihaas- Temporary fix for the upgm db.
            if (ConfigData.checkParmValue("CD-upgm-manx", "1")) {
                upgmDbDir = new WepsTreeNodeFile (new TFile(ConfigData.getDefault().getDataParsed(ConfigData.SystemUPGMDB)), this);
                upgmDbDir.setDisplayName("System UPGM Records");
                addNode(upgmDbDir);
            }
            
        } 

        if(ConfigData.checkParmValue(ConfigData.crlmodCropCheck,"1")) {
            crlmodNode = new WepsTreeNodeCrLmodCropResidue();
            crlmodNode.refreshChildren();
            addNode(crlmodNode);
            crlmodNode.getBackgroundPropertyChangeObject()
                        .addPropertyChangeListener(PROP_CSIP_BGCATALOG_COMPLETE, this);
        }
        // Residues are now already under crops in WepsTreeNodeCrLmodCropResidue
        // For cached files.  Remove after dev? MEH.
        if (ConfigData.checkParmValue("CD-WS-csip-crlmod-Crop-ShowCacheItem","1")) {
            addFileNode (new TFile(About.getWepsCacheDir(), CsipLmodUtil.WepsCacheDirNameCrop), 
                         "Cached CR" + CsipLmodUtil.getCrLmodName(lmodType.lmodCrop));
        }
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        super.propertyChange(evt);
        if (null != evt.getPropertyName()) {
            switch (evt.getPropertyName()) {
                case ConfigData.SystemCropDB: {
                    String path = ConfigData.getDefault().getDataParsed(ConfigData.SystemCropDB);
                    if (path != null) {
                        cropsDbDir.setFile(new TFile(path));
                    } else {
                        cropsDbDir.setFile(null);
                    }
                    nodeStructureChanged(cropsDbDir);
                    break;
                }
            }
        }
    }

    @Override
    public boolean accept(java.io.File file) {
        return ( file.isDirectory() || file.getName().toLowerCase().trim().endsWith(WepsFileTypes2.Crop.getExtension())
                        || file.getName().toLowerCase().trim().endsWith(WepsFileTypes2.Upgm.getExtension()) );
    }
    
    public void refresh() {
        build();
    }

}
