package vismon.data;

import vismon.gui.TradeoffsPanel;
import vismon.gui.Vismon;
import vismon.util.Computations;
import vismon.util.Constants;
import vismon.util.Util;

import java.awt.Color;
import java.util.*;
import java.util.prefs.Preferences;

public class Scenario extends DataItem {
    
    public static int scenarioCounter;
    public static int numOfVisibleScenarios = 0;

    private String name;
    private Color color;
    private boolean visible;



    static {
        scenarioCounter = 1;
    }

    public Scenario(MngAction action) {
        super(action);

        name = "Scn" + scenarioCounter;
        color = Constants.getScenarioColor();
        visible = true;

        checkIfFiltered();
    }

    public void checkIfFiltered() {
        //TODO
    }

    public static void incrementIndicatorCounter() {
        if (scenarioCounter == 1)
            Vismon.getVismonInstance().setTradeoffsWinMenuItemSelected();
        ++scenarioCounter;
        ++numOfVisibleScenarios;
    }

    public static void resetScenarioNum() {
        scenarioCounter = 1;
        numOfVisibleScenarios = 0;
    }

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        if (this.visible == visible)
            return;

        this.visible = visible;
        if (visible)
            ++numOfVisibleScenarios;
        else
            --numOfVisibleScenarios;
    }

    public double getAverageOrMedianValue(Indicator indicator, int avgMedian) {
        if (avgMedian == 0)
            return getAverageValue(indicator);
        return getMedianValue(indicator);
    }

    public void computeSimValues() {
        MngOption xMngOption = Session.getMngOption(0);
        MngOption yMngOption = Session.getMngOption(1);

        for (Indicator ind : Session.getIndicators()) {
            List<Double> simValues = Computations.getSimValuesOfPoint(xMngOption, yMngOption, ind,
                    mngAction.getMngValue(xMngOption), mngAction.getMngValue(yMngOption));
            for (int i = 0; i < simValues.size(); ++i)
                addValue(i, ind, simValues.get(i));
        }
    }

    public boolean lessThan(Scenario scenario, Indicator indicator) {
        return (getAverageValue(indicator) <= scenario.getAverageValue(indicator));
    }

    @Override
    public boolean equals(Object o) {
        Scenario oScenario = (Scenario)o;
        return  name.equals(oScenario.getName()) &&
                color.equals(oScenario.getColor()) &&
                Util.equals(mngAction.getMngValue(Session.getMngOption(0)), (oScenario.getMngAction().getMngValue(Session.getMngOption(0)))) &&
                Util.equals(mngAction.getMngValue(Session.getMngOption(1)), (oScenario.getMngAction().getMngValue(Session.getMngOption(1))));
    }

    @Override
    public String toString() {
        return name + ": \t@(" + mngAction.getMngValue(Session.getMngOption(0)) +
                mngAction.getMngValue(Session.getMngOption(1)) + ")" +
                "\n\t" + color;
    }

    public String getInfo(Indicator ind) {
        switch (TradeoffsPanel.uncertainty) {
            case TradeoffsPanel.NO_UNCERTAINTY:
                return "Avg = " + Util.format(getAverageValue(ind), 2);
            case TradeoffsPanel.ERROR_BAR:
                double[] errorBars = getErrorBar(ind, Session.getErrorBarPercentage());
                return "Lower Quan = " + Util.format(errorBars[0], 2) + ", " +
                       "Avg = " + Util.format(getAverageValue(ind), 2) + ", " +
                       "Upper Quan = " + Util.format(errorBars[1], 2);
            case TradeoffsPanel.BOX_PLOT:
                errorBars = getErrorBar(ind, 50);
                return "Min = " + Util.format(getMinValue(ind), 2) + ", " +
                       "Lower Quan = " + Util.format(errorBars[0], 2) + ", " +
                       "Avg = " + Util.format(getAverageValue(ind), 2) + ", " +
                       "Median = " + Util.format(getMedianValue(ind), 2) + ", " +
                       "Upper Quan = " + Util.format(errorBars[1], 2) + ", " +
                        "Max = " + Util.format(getMaxValue(ind), 2);
            case TradeoffsPanel.DISTRIBUTION:
                return "";
        }
        return "";
    }
}
