/*
 * UpgmTableModel.java
 *
 * Created on July 20, 2004, 2:18 PM
 *
 * This is the model for delivering data to the main operations table view.
 * This handles loading the operations_defn.xml file and the
 * operations_lang.xml file.
 *
 * The operations_defn.xml file contains the following information that is stored:
 *    <paramname> - name of the parameter
 *    <paramtype> - type of the parameter, float, int, etc
 *    <paramunit> - default units for the value
 *
 * The operations_lang.xml file contains the following information that is stored:
 *    <paramprompt> - long description of parameter, what is displayed as a column heading
 *    <paramaltunit> - an alternate unit (English) that the parameter can be displayed in
 *    <factor> - what to multiple the value by to display the value in alternate units
 *    <paramchoice> - list of values and text for the choice lists that a parameter may have
 *
 * Jim Frankenberger
 * USDA-ARS, West Lafayette IN
 * jrf@purdue.edu
 *
 */
package ex1;

import javax.swing.JOptionPane;


/**
 * This the model for delivering data to the main operations table view. This handles loading the
 * operations_defn.xml file and the operations_lang.xml file.
 *
 * The operations_defn.xml file contains the following information that is stored:
 * <paramname> - name of the parameter
 * <paramtype> - type of the parameter, float, int, etc
 * <paramunit> - default units for the value
 *
 * @author jrf
 *
 */
public class UpgmTableModel extends OprnTableModel {
    private static final long serialVersionUID = 1L;

    /**
     * Create the table model for the main part of operations
     *
     * @param cfgDir directory where mcrew config files are located
     * @param dbMainDir directory where data files are located
     */
    public UpgmTableModel(String cfgDir, String dbMainDir) {
        super(cfgDir, dbMainDir, false);
        parms = new DefnFileParser(cfgDir + "/UPGM_defn_db.xml", false);
        new LangFileParser(parms, cfgDir + "/UPGM_lang_db.xml", false);
        pData = new DataFileParser(parms, cfgDir, false);
    }

    /**
     * Get the name of the column of the main operations screen, very generic.
     *
     * @param col the column to get the name for
     * @return the name used for the column
     */
    @Override
    public String getColumnName(int col) {
        //return parms.getParmName(col);
        if (col == 0) {
            return "Num";
        }
        if (col == 1) {
            return "Name";
        }
        if (col == 2) {
            return "Subdirectory";
        } else {
            return "Phase " + String.valueOf(col - 2);
        }
    }

        /**
     * Delete process from a specific row.
     *
     * @param row row number that delete will occur on
     * @param procNum index of process within row that will be deleted.
     * @return true if the process could be deleted.
     */
    @Override
    public boolean deleteProcess(int row, int procNum) {
        boolean rc = false;
        if(procNum == 0)
        {
            JOptionPane.showMessageDialog(null, "All UPGM records must have at exactly"
                    + " one P 100 process.  Cannot delete.", "UPGM "
                    + "Process", JOptionPane.INFORMATION_MESSAGE);
            return false;
        }

        WepsDBFile wf = xmlFiles.get(row);
        if (wf != null) {
            return wf.deleteProcess(procNum);
        }
        return rc;
    }

    /**
     * Changes the process type within a row to new process
     *
     * @param row row that process belongs to
     * @param procNum index of process that is to be changed
     * @param newProc index of new process in processes array
     * @return
     */
    @Override
    public boolean changeProcess(int row, int procNum, int newProc) {
        boolean rc = false;
        OpAction a = parms.getAction(newProc);
        if((procNum == 0) ^ (a.code == 'P' && a.id == 100))
        {
            JOptionPane.showMessageDialog(null, "All UPGM records must have exactly"
                    + " one P 100 process.  Cannot change.", "UPGM "
                    + "Process", JOptionPane.INFORMATION_MESSAGE);
            return false;
        }

        WepsDBFile wf = xmlFiles.get(row);
        if (wf != null) {
            ActionValue av = findSameAction(a);

            return wf.changeProcess(procNum, newProc, av);
        }
        return rc;
    }

    /**
     * Searches all files for one that has the same type of process.
     *
     * @param a action/process value to search for
     * @return an action structure with parameters set to reasonable values
     */
    private ActionValue findSameAction(OpAction a) {

        for (WepsDBFile wf : xmlFiles) {
            ActionValue av = wf.find(a);
            if (av != null) {
                return av;
            }
        }

        return null;
    }

    /**
     * Insert a new process int the operation.
     *
     * @param row row that process will be added to
     * @param procNum index of process to be changed
     * @param newProc type of new process to be added
     * @param before true if new process is before existing, or false if after
     * @return true if new process could be added
     */
    @Override
    public boolean insertProcess(int row, int procNum, int newProc, boolean before) {
        boolean rc = false;
        OpAction a = parms.getAction(newProc);
        if(((procNum == 0) || before) && (a.code == 'P' && a.id == 100))
        {
            JOptionPane.showMessageDialog(null, "All UPGM records must have exactly"
                    + " one P 100 process.  Cannot change.", "UPGM "
                    + "Process", JOptionPane.INFORMATION_MESSAGE);
            return false;
        }

        WepsDBFile wf = xmlFiles.get(row);
        if (wf != null) {
            ActionValue av = findSameAction(a);
            return wf.insertProcess(procNum, newProc, before, av);
        }
        return rc;
    }

    /**
     * Insert a new process int the operation.
     *
     * @param row row that process will be added to
     * @param procNum index of process to be changed
     * @param newProc type of new process to be added
     * @param before true if new process is before existing, or false if after
     * @return true if new process could be added
     */
    @Override
    public boolean insertFirstProcess(int row, int procNum, int newProc, boolean before) {
        boolean rc = false;
        OpAction a = parms.getAction(newProc);
        WepsDBFile wf = xmlFiles.get(row);
        if(wf.getActionCount() > 0)
        {
            JOptionPane.showMessageDialog(null, "All UPGM records must have exactly"
                    + " one P 100 process.  Cannot change.", "UPGM "
                    + "Process", JOptionPane.INFORMATION_MESSAGE);
            return false;
        }
        if (wf != null) {
            ActionValue av = findSameAction(a);
            return wf.insertProcess(procNum, newProc, before, av);
        }
        return rc;
    }

    /**
     * This will shift/move an existing process in the process order
     *
     * @param row row that process belongs to
     * @param col column of process to shift
     * @param isRight true if data should be shofted right, false for left
     */
    @Override
    public void moveProcess(int row, int col, boolean isRight) {
        WepsDBFile wf = xmlFiles.get(row);
        if(((col == 0) && isRight) || ((col == 1) && !isRight))
        {
            JOptionPane.showMessageDialog(null, "All UPGM records must have exactly"
                    + " one P 100 process.  Cannot move.", "UPGM "
                    + "Process", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        if (wf != null) {
            if (isRight) {
                wf.moveRight(col);
            } else {
                wf.moveLeft(col);
            }
        }
    }

    @Override
    public WepsTableModel.TableEnum getType() {
        return WepsTableModel.TableEnum.UPGM;
    }
    
    @Override
    public String getTypeString() {
        return "upgm";
    }
    
    @Override
    public String getTypeFileExtension() {
        return ".upgm";
    }

}
