
package usda.weru.soil;

import java.util.LinkedList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import usda.weru.remoteDataAccess.remoteFiles.dbFile.soil.SoilFileComponent;
import usda.weru.remoteDataAccess.remoteFiles.dbFile.soil.SoilFileLegend;
import usda.weru.remoteDataAccess.remoteFiles.dbFile.soil.SoilFileMapunit;
import usda.weru.util.tree.WepsTreeComboBox;
import usda.weru.util.tree.WepsTreeModelSoil;

/**
 * MassSoilTreeModel.java
 *
 * Created on April 19, 2007, 12:27 PM
 * 
 * @author Joseph Levin
 */


public class MassSoilTreeModel extends WepsTreeModelSoil {

    private static final long serialVersionUID = 1L;
    
    List<DefaultMutableTreeNode> selected;
    private int legend = 0;
    private int mapUnit = 0;
    private int component = 0;
    
    private final int legendLimit = 1;
    private final int mapUnitLimit = 10;
    private final int componentLimit = 50;

    /**
     *
     * @param input - include project
     */
    public MassSoilTreeModel(WepsTreeComboBox input) {
        super(input, false, false, true);
        selected = new LinkedList<>();
    }
    
    public int getNumLegend() { 
        return legend; 
    }
    
    public int getNumMapUnit() { 
        return mapUnit; 
    }
    
    public int getNumComponent() { 
        return component; 
    }

    public boolean pathSelected(TreePath path) {
        Object temp = path.getLastPathComponent();
        DefaultMutableTreeNode res;

        try {
            res = (DefaultMutableTreeNode) temp;
        } catch (ClassCastException cce) {
            return false;
        }

        return selected.contains(res);
    }
    
    public void selectPath(TreePath path) {
        Object temp = path.getLastPathComponent();
        DefaultMutableTreeNode res;
        try {
            res = (DefaultMutableTreeNode) temp;
        } catch (ClassCastException cce) {
            return;
        }
        try {
            temp = res.getUserObject();
        } catch (UnsupportedOperationException | NullPointerException uoe) {
            return;
        }
        if (temp instanceof SoilFileLegend) {
            if (selected.contains(res)) {
                selected.remove(res);
                legend--;
            } else {
                if (legend == legendLimit) {
                    JOptionPane.showMessageDialog(null, "Only one legend node is "
                            + "allowed to be selected at a time.\n"
                            + "Please Download the currently selected node first "
                            + "or unselect the current node.", "Large Selection Size", JOptionPane.WARNING_MESSAGE);
                } else {
                    selected.add(res);
                    legend++;
                }
            }
        } else if (temp instanceof SoilFileMapunit) {
            if (selected.contains(res)) {
                selected.remove(res);
                mapUnit--;
            } else {
                if (mapUnit == mapUnitLimit) {
                    JOptionPane.showMessageDialog(null, "Only ten map unit nodes are "
                            + "allowed to be selected at a time.\n"
                            + "Please Download the currently selected nodes first "
                            + "or unselect a node.", "Large Selection Size", JOptionPane.WARNING_MESSAGE);
                } else {
                    selected.add(res);
                    mapUnit++;
                }
            }
        } else if (temp instanceof SoilFileComponent) {
            if (selected.contains(res)) {
                selected.remove(res);
                component--;
            } else {
                if (component == componentLimit) {
                    JOptionPane.showMessageDialog(null, "Only fifty component nodes are "
                            + "allowed to be selected at a time.\n"
                            + "Please Download the currently selected nodes first "
                            + "or unselect a node.", "Large Selection Size", JOptionPane.WARNING_MESSAGE);
                } else {
                    selected.add(res);
                    component++;
                }
            }
        }
    }
    
    public boolean downloadReady() {
        return !selected.isEmpty();
    }
}
