package vismon.io;

import vismon.data.*;
import vismon.util.Constants;
import vismon.util.Util;

import java.io.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class FileHandler {


    /*private static void setProgressBarValue(int linesRead, long fileSize, ProgressBar progressBar) {
        progressBar.setProgressBarValue((int) (100.0 * linesRead / fileSize + 0.5));
    }*/

    public static void readDataFile(File f) {
        long fileSize = f.length();
        int linesRead = 0;

        //ProgressBar progressBar = new ProgressBar();
        //progressBar.run();

        try {
            BufferedReader input = new BufferedReader(new FileReader(f));
            
            String inputString = input.readLine();
            Session.setDataSetName(inputString);
            linesRead += inputString.length() + 2;
            /*if (linesRead % 1000 == 0)
                setProgressBarValue(linesRead, fileSize, progressBar);*/

            inputString = input.readLine();
            int mngOptionNum = Integer.parseInt(inputString);
            linesRead += inputString.length() + 2;
            /*if (linesRead % 1000 == 0)
                setProgressBarValue(linesRead, fileSize, progressBar);*/
            Session.setMngOptionNum(mngOptionNum);

            //CREATING MANAGEMENT OPTIONS AND SETTING THEIR PROPERTIES
            for (int i = 0; i < mngOptionNum; ++i) {
                MngOption mngOpt = new MngOption();

                inputString = input.readLine();
                mngOpt.setName(inputString);
                linesRead += inputString.length() + 2;
                /*if (linesRead % 1000 == 0)
                    setProgressBarValue(linesRead, fileSize, progressBar);*/

                inputString = input.readLine();
                mngOpt.setValueNum(Integer.parseInt(inputString));
                linesRead += inputString.length() + 2;
                /*if (linesRead % 1000 == 0)
                    setProgressBarValue(linesRead, fileSize, progressBar);*/

                Session.addMngOption(mngOpt);
            }

            inputString = input.readLine();
            int indicatorNum = Integer.parseInt(inputString);
            linesRead += inputString.length() + 2;
            /*if (linesRead % 1000 == 0)
                setProgressBarValue(linesRead, fileSize, progressBar);*/
            Session.setIndicatorNum(indicatorNum);

            inputString = input.readLine();
            Session.setIndicatorRowNum(Integer.parseInt(inputString));
            linesRead += inputString.length() + 2;
            /*if (linesRead % 1000 == 0)
                setProgressBarValue(linesRead, fileSize, progressBar);*/

            inputString = input.readLine();
            Session.setIndicatorColNum(Integer.parseInt(inputString));
            linesRead += inputString.length() + 2;
            /*if (linesRead % 1000 == 0)
                setProgressBarValue(linesRead, fileSize, progressBar);*/

            //CREATING INDICATORS AND SETTING THEIR PROPERTIES
            for (int i = 0; i < indicatorNum; ++i) {
                Indicator ind = new Indicator();

                inputString = input.readLine();
                ind.setName(inputString);
                linesRead += inputString.length() + 2;
                /*if (linesRead % 1000 == 0)
                    setProgressBarValue(linesRead, fileSize, progressBar);*/

                inputString = input.readLine();
                ind.setShortName(inputString);
                linesRead += inputString.length() + 2;
                /*if (linesRead % 1000 == 0)
                    setProgressBarValue(linesRead, fileSize, progressBar);*/

                inputString = input.readLine();
                ind.setUnit(inputString);
                linesRead += inputString.length() + 2;
                /*if (linesRead % 1000 == 0)
                    setProgressBarValue(linesRead, fileSize, progressBar);*/

                String minMaxBest = input.readLine();
                linesRead += minMaxBest.length() + 2;
                /*if (linesRead % 1000 == 0)
                    setProgressBarValue(linesRead, fileSize, progressBar);*/
                if (minMaxBest.equals("min") || minMaxBest.equals("Min"))
                    ind.setBestValue(Indicator.MIN_BEST);
                else if (minMaxBest.equals("max") || minMaxBest.equals("Max"))
                    ind.setBestValue(Indicator.MAX_BEST);
                else
                    System.out.println("error!");

                Session.addIndicator(ind);
            }

            inputString = input.readLine();
            Session.setSimulationNum(Integer.parseInt(inputString));
            linesRead += inputString.length() + 2;

            //READING MANAGEMENT OPTION VALUES
            for(MngOption mngOption : Session.getMngOptions()) {
                for (int i = 0; i < mngOption.getValueNum(); ++i) {
                    inputString = input.readLine();
                    mngOption.addValue(Double.parseDouble(inputString));
                    linesRead += inputString.length() + 2;
                }
            }

            //List<double[]> permutations = Util.getPermutations(Session.getMngOptions());
            List<double[]> permutations = Util.getPermutations(Session.getMngOption(0), Session.getMngOption(1));

            //CREATING MANAGEMENT ACTIONS
            for (int i = 0; i < permutations.size(); ++i) {
                MngAction action = new MngAction();
                double[] perm = permutations.get(i);
                for (int j = 0; j < perm.length; ++j)
                    action.addMngValue(Session.getMngOption(j), perm[j]);

                DataItem dataItem = new DataItem(action);
                Session.addDataItem(dataItem);
            }

            //READING INDICATOR VALUES
            for(Indicator depDim : Session.getIndicators()) {
                for (double[] perm : permutations) {
                    MngAction action = new MngAction();
                    for (int j = 0; j < perm.length; ++j)
                        action.addMngValue(Session.getMngOption(j), perm[j]);

                    DataItem dataItem = Session.getDataItem(action);

                    for (int j = 0; j < Session.getSimulationNum(); ++j) {
                        inputString = input.readLine();
                        String str = inputString;
                        linesRead += inputString.length() + 2;

                        if (str.equals("NA"))
                            dataItem.addValue(j, depDim, Constants.NA);
                        else
                            dataItem.addValue(j, depDim, Double.parseDouble(str) + Math.random() * 1e-5);
                    }
                }
            }

            for (MngOption mngOption : Session.getMngOptions())
                mngOption.doComputations();
            for (Indicator indicator : Session.getIndicators())
                indicator.doComputations();
            for (DataItem dataItem : Session.getDataItems())
                dataItem.doComputations();
            for (Indicator indicator : Session.getIndicators())
                Session.calcIndicatorContourStep(indicator);
            Session.setIndicatorDefaultRowColPositions();

            input.close();
            //progressBar.close();
        } catch (IOException ex) {
            Logger.getLogger(Session.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void readProjectFile(File f) {

    }

    public void saveProject(String path) {

    }

    public void saveDiagrams(String path) {

    }

    public void exportScenarios(String path) {

    }

}
