
package usda.weru.util.tree;

import de.schlichtherle.truezip.file.TFile;
import java.io.IOException;
import java.nio.file.Path;
import javax.swing.ProgressMonitor;
import usda.weru.remoteDataAccess.remoteFiles.RemoteFile;
import usda.weru.remoteDataAccess.remoteFiles.RemoteFile.RemoteFileType;
import usda.weru.remoteDataAccess.remoteFiles.dbFile.soil.SoilFile;
import usda.weru.util.WepsMessageLog;

/**
 *
 * @author mhaas
 */
public class WepsTreeNodeRemote extends WepsTreeNode {

    private static final long serialVersionUID = 1L;

    protected String name;
    protected String path;
    protected RemoteFile fileObj;

    protected boolean temporarilyClosed;

    public WepsTreeNodeRemote() {
        fileObj = new RemoteFile("WepsTreeNodeRda default", RemoteFileType.INET);
        path = null;
        name = null;
        temporarilyClosed = false;

        // calling this checks AND sets / intialize the flag variable
        getAllowsChildren();
    }

    public WepsTreeNodeRemote(RemoteFile fileobj) {
        this.name = fileobj.getName();
        this.path = fileobj.getParent();
        //System.out.println("Remote File Path: " + path);
        fileObj = fileobj;
//MEH-04-14        c_childrenLoaded=!fileobj.isFile();

        // calling this checks AND sets / intialize the flag variable
        getAllowsChildren();
    }

    public void setDefaultDest(Path defaultDestPath) {
        if (fileObj != null) {
            fileObj.setDestinationPath(defaultDestPath);
        }
    }

    public void setTreeNodePath(String path) {
        this.path = path;
    }

    public String getTreeNodePath() {
        return path;
    }

    public String getTreeNodeName() {
        return name;
    }

    public void reInitialize() {
        fileObj.reInitialize();
    }

    @Override
    public TFile getNodeData() throws IOException {
        //System.out.println("This is the Remote Soil NoArg");
        //System.out.println("fileObj: " + fileObj.getAbsolutePath());
        TFile ret = new TFile((isLeaf()) ? fileObj.downloadFile() : fileObj);
        //System.out.println("Node File Name: " + ret.getName());
        return ret;
    }

    @Override
    public TFile getNodeData(ProgressMonitor progress) throws IOException {
        TFile ret = new TFile((isLeaf()) ? fileObj.downloadFile(progress) : fileObj);
        return ret;
    }

    public WepsMessageLog getNodeDataLog() {
        if (fileObj instanceof SoilFile) {
            return ((SoilFile) fileObj).getDownloadFileLog();
        } else {
            return null;
        }
    }

    @Override
    public Object getUserObject() {
        return fileObj;
    }

    public RemoteFile getRemoteFileObj() {
        if (fileObj instanceof RemoteFile) {
            return fileObj;
        } else {
            return null;
        }
    }

    @Override
    public boolean isLeaf() {
        if (temporarilyClosed) {
            return true;
        }
        if (fileObj != null) {
            return fileObj.isFile();
        }
        return false;
    }

    @Override
    public String toString() {
        if (displayName != null) {
            return displayName;
        } else if (fileObj != null) {
            return fileObj.getName();
        } else {
            return super.toString();
        }
    }

    @Override
    public boolean updateSubtree() {
        return false;
    }

    @Override
    public boolean getAllowsChildren() {
        allowsChildren = !this.isLeaf();
        return allowsChildren;
    }

    @Override
    protected WepsTreeNode[] createChildren() {
        return super.createChildren();
    }

    public void setTemporarilyClosed(boolean newVal) {
        temporarilyClosed = newVal;
    }
}
