import java.util.ArrayList;
import javax.swing.JOptionPane;
/**
 * Graphical user interface for decision support application.
 *
 * @author Dr. Jody Paul
 * @version 4 April 2006
 */
public class GraphicalUserInterface extends UserInterface {

    public ArrayList<Choice> getChoices() {
        ArrayList<Choice> alchoice = new ArrayList<Choice>();
        String alternative = JOptionPane.showInputDialog(
            "Please enter one of the alternatives"
            + " that you are considering. (Cancel to quit.)");
        while (alternative != null && !("".equals(alternative))) {
            alchoice.add(new Choice(alternative));
            alternative = JOptionPane.showInputDialog(
                "Please enter another alternative. (Leave blank if all have been entered.)");
        }
        return alchoice;
    }
    
    public ArrayList<Characteristic> getCharacteristics() {
        ArrayList<Characteristic> alchar = new ArrayList<Characteristic>();
        String characteristic = JOptionPane.showInputDialog(
            "Please enter one factor that would influence your choice\n"
            + "among alternatives. (Cancel to quit.)");
        while (characteristic != null && !("".equals(characteristic))) {
            alchar.add(new Characteristic(characteristic));
            characteristic = JOptionPane.showInputDialog(
                "Please enter another attribute. (Leave blank if all have been entered.)");
        }
        return alchar;
    }
    
    public void getCharacteristicRankings(ArrayList<Characteristic> alc, int standard) {
        int lastAttribute = alc.size() - 1;
        for (int i = 0; i < lastAttribute; i++) {
            String importance = JOptionPane.showInputDialog(
                "<HTML>If <B><SIZE=+1>"
                + alc.get(lastAttribute).getName()
                + "</SIZE></B> has an <I>importance</I> of <B>"
                + standard
                + "</B>,<BR>"
                + "how important is <B><SIZE=+1>"
                + alc.get(i).getName()
                + "</SIZE></B>?</HTML>");
             if (importance == null || "".equals(importance)) {
                 alc.get(i).setRank(standard);
             } else {
                 alc.get(i).setRank(new Integer(importance));
             }
        }
        alc.get(lastAttribute).setRank(standard);

    }

    public double[][] getCrossRankings(ArrayList<Choice> choices,
                                      ArrayList<Characteristic> characteristics,
                                      int standard) {
        double[][] crossRankings = new double[choices.size()][characteristics.size()];
        for (int i = 0; i < characteristics.size(); i++) {
            int firstChoice = 0;
            crossRankings[firstChoice][i] = standard;
            for (int j = firstChoice + 1; j < choices.size(); j++) {
                String rank = JOptionPane.showInputDialog(
                    "<HTML>Considering <B><SIZE=+1>"
                    + characteristics.get(i).getName()
                    + "</SIZE></B> only,<BR>"
                    + "if <B><SIZE=+1>"
                    + choices.get(firstChoice).getName()
                    + "</SIZE></B> has a value of <B><SIZE=+1>"
                    + standard
                    + "</SIZE></B> "
                    + ",<BR>"
                    + "what value would you associate with <B><SIZE=+1>"
                    + choices.get(j).getName()
                    + "</SIZE></B><BR>(a higher value is <I>more desirable</I>)?</HTML>");
                if (rank == null || "".equals(rank)) {
                    crossRankings[j][i] = standard;
                } else {
                    crossRankings[j][i] = new Integer(rank);
                }
            }
        }
        return crossRankings;
    }

    public void showResults(ArrayList<Choice> choices) {
        Choice preferredChoice = choices.get(0);
        int max = 0;
        for (Choice c : choices) {
            if (max < c.getFinalScore()) max = c.getFinalScore();
        }
        for (Choice c : choices) {
            if (max == c.getFinalScore()) preferredChoice = c;
        }
        String s = "<HTML>Preferred choice: <B>";
        s += preferredChoice.getName();
        s += "</B>\n-----\n";
        for (Choice c : choices) {
            s += c.getName()
                 + " == "
                 + c.getFinalScore()
                 + "\n";
        }
        JOptionPane.showMessageDialog(null,
                                      s,
                                     "Decider Results",
                                     JOptionPane.INFORMATION_MESSAGE);
    }
}
