package vismon.plot;

import vismon.data.Indicator;
import vismon.data.Scenario;
import vismon.data.Session;
import vismon.gui.OverviewPanel;
import vismon.gui.Vismon;
import vismon.util.Util;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class IndicatorOverviewPlot extends HistogramPlot {

    private Indicator indicator;
    private List<Integer> indicatorBinHeights;

    public IndicatorOverviewPlot(Indicator indicator) {
        this.indicator = indicator;
        min = indicator.getMin();
        max = indicator.getMax();

        int dataItemNum = Session.getDataItems().size();
        int simulationNum = Session.getSimulationNum();
        List<Double> indicatorValues = new ArrayList<Double>();
        for (int i = 0; i < simulationNum; ++i)
            for (int j = 0; j < dataItemNum; ++j) {
                double v = Session.getDataItems().get(j).getValue(i, indicator);
                if (!Util.isNaN(v))
                    indicatorValues.add(v);
            }
        indicatorBinHeights = getPDFBinHeights(indicatorValues);

        x = 0;
        y = 0;
        h = OverviewPanel.HIST_PLOT_HEIGHT;
    }

    @Override
    public void paint(Graphics g) {
        setBinHeights();
        super.paint(g);
    }

    private void setBinHeights() {
        Scenario scenario = Session.getSelectedScenario();

        if (Vismon.getVismonInstance().getOverviewPanel().getDensityFunc() == OverviewPanel.PDF)
            binHeights = indicatorBinHeights;
        else
            binHeights = getCDFBinHeights(indicatorBinHeights);

        if (scenario == null) {
            logPlot = false;
            onTopBinHeights = null;
        } else {
            logPlot = true;
            onTopColor = scenario.getColor();

            double[] values = scenario.getValues(indicator);
            List<Double> scenarioValues = new ArrayList<Double>();
            //for (int i = 0; i < Session.getSimulationNum(); ++i)
            //  scenarioValues.add(scenario.getValue(i, indicator));
            for (double v : values)
                scenarioValues.add(v);

            if (Vismon.getVismonInstance().getOverviewPanel().getDensityFunc() == OverviewPanel.PDF)
                onTopBinHeights = getPDFBinHeights(scenarioValues);
            else
                onTopBinHeights = getCDFBinHeights(getPDFBinHeights(scenarioValues));
        }
    }

    public static boolean getLogPlot() {
        return logPlot;
    }
}