package usda.weru.remoteDataAccess;

import java.beans.PropertyChangeSupport;
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.ProgressMonitor;
import usda.weru.remoteDataAccess.exceptions.RdaConnectException;

/**
 *
 * @author mhaas
 */
public abstract class RdaInputController {

    protected String interfaceName;
    protected RdaHierarchicalItem inputDataTree;

    protected PropertyChangeSupport changes;

    public RdaInputController() {
        inputDataTree = null;
        changes = new PropertyChangeSupport(this);
    }

    public RdaHierarchicalItem getInputDataTree() {
        return inputDataTree;
    }

    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    public String getInterfaceName() {
        return interfaceName;
    }

    public RdaHierarchicalItem init(String name) throws RdaConnectException {
        RdaHierarchicalItem item = new RdaHierarchicalItem(name, this);
        init(item);
        return inputDataTree;
    }

    public void init(RdaHierarchicalItem treeRoot) throws RdaConnectException {
        inputDataTree = treeRoot;
        inputDataTree = initializeData(inputDataTree);
    }

    public PropertyChangeSupport getChangeObj() {
        return changes;
    }

    public abstract RdaInterface getInterface();

    public abstract void controllerSetDestinationPath(Path destPath);

    protected abstract RdaHierarchicalItem initializeData(RdaHierarchicalItem treeRoot) throws RdaConnectException;

    public abstract File getFile(RdaHierarchicalItem item) throws RdaConnectException;

    // obviously: need to override to activate...
    public File getFile(RdaHierarchicalItem item, ProgressMonitor progress) throws RdaConnectException {
        return getFile(item);
    }

    protected abstract void loadChildren(RdaHierarchicalItem rootItem) throws RdaConnectException;

    public RdaHierarchicalItem getRootItem() {
        return inputDataTree;
    }

    public ArrayList<String> getPathItems(String path) {
        RdaHierarchicalItem item = inputDataTree;
        if (!path.isEmpty()) {
            item = inputDataTree.getBottomPathItem(path);
        }
        if (item != null) {
            return getPathItems(item);
        }
        return new ArrayList<>();
    }

    public ArrayList<String> getPathItems(Object item) {
        if (item instanceof RdaHierarchicalItem) {
            // check if children need to be loaded from service before they are listed.
            if (!((RdaHierarchicalItem) item).getChildrenLoaded()) {
                try {
                    loadChildren((RdaHierarchicalItem) item);
                } catch (RdaConnectException ex) {
                    JOptionPane.showMessageDialog(null, "Cannot connect to remote service:" + ex.getServiceName(), "Weps Remote Data Access", JOptionPane.WARNING_MESSAGE);
                }
            }
            return ((RdaHierarchicalItem) item).getChildrenNames();
        }
        return new ArrayList<>();
    }

    public RdaHierarchicalItem getPathObject(String path) {
        RdaHierarchicalItem item = inputDataTree;
        if (!path.isEmpty()) {
            item = inputDataTree.getBottomPathItem(path);
        }
        return item;
    }

    public RdaHierarchicalItem getNewItem(String name) {
        return new RdaHierarchicalItem(name, this);
    }
}
