package app.PREDICTION;

import app.DataUtil;
import app.PREPROCESSING.PrepConf;
import app.PREPROCESSING.Preprocesor;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Petr Flégl on 26.3.2017.
 */
public class CH4ConsumptionPredictor extends AbstractPredictor{
    private int timestampBetaFrom = 0;
    private int timestampBetaTo = 0;
    private int timestampXyears = 0;

    int daysToScale = 1;

    public CH4ConsumptionPredictor(int bfrom, int bto, int xyears) {
        timestampBetaFrom=bfrom;
        timestampBetaTo=bto;
        timestampXyears=xyears;
        daysToScale = (timestampBetaTo-timestampBetaFrom)/60/60/24;

        List<PrepConf> dataX = new ArrayList<>();
        dataX.add(new PrepConf("table5:cO2", "CO2", "datumPlyn", timestampBetaFrom, timestampBetaTo, 0, 35, 0, 35));
        dataX.add(new PrepConf("table5:kyslik", "O2", "datumPlyn", timestampBetaFrom, timestampBetaTo, 38, 48, 38, 48));
        dataX.add(new PrepConf("table5:datumPlyn", "Datum", "datumPlyn",timestampBetaFrom, timestampBetaTo, timestampBetaFrom, timestampBetaTo,0, daysToScale));

        Preprocesor p = new Preprocesor(dataX);
        X = new Matrix(p.getData(), Matrix.matrixType.XY);

        List<PrepConf> dataY = new ArrayList<>();
        dataY.add(new PrepConf("table5:metan", "Metan", "datumPlyn", timestampBetaFrom, timestampBetaTo, 0, 1500, 0, 1500));
        p = new Preprocesor(dataY);
        Y = new Matrix(p.getData(), Matrix.matrixType.XY);
        ml = new MultiLinearRegression(X,Y,true);
        try {
            beta = ml.calculate();
        } catch (NoSquareException e) {
            e.printStackTrace();
        }
    }


    public double[] getResults(int numOfDays){
        double[] results = new double[numOfDays];
        double[] betas = getBetaArray();
        List<PrepConf> dataX = new ArrayList<>();

        Preprocesor p;
        for(int day = 1;day<=numOfDays;day++) {
            dataX.clear();
            int ts = DataUtil.getTimestampForDay(-day);
            for (int year = 1 ; year <=timestampXyears; year++) {
                int secs = ts-DataUtil.secForYears(year+1);
                int d = 60*60*24;
                dataX.add(new PrepConf("table5:cO2", "CO2", "datumPlyn", secs, secs+d, 0, 35, 0, 35));
                dataX.add(new PrepConf("table5:kyslik", "O2", "datumPlyn", secs, secs+d, 38, 48,38, 48));
                dataX.add(new PrepConf("table5:datumPlyn", "Datum", "datumPlyn",secs, secs+d, secs, secs+d,0, 1 ));

            }
            p = new Preprocesor(dataX);
            double[][] data = p.getData();

            Matrix m = new Matrix(data, Matrix.matrixType.XY);
            double[] dX = new double[dataX.size()/timestampXyears];
            for(int year = 1;year <=timestampXyears; year++){
                for(int col = 0;col<(dataX.size()/timestampXyears);col++) {
                    dX[col] += data[((year - 1) * (dataX.size() / timestampXyears)) + col][0];
                }
            }
            for(int col = 0;col<(dataX.size()/timestampXyears);col++) {
                dX[col] = dX[col] / timestampXyears;
            }
            if(betas.length==4 & dX.length==3) {
                results[day - 1] = betas[0] + betas[1] * dX[0] + betas[2] * dX[1] + betas[3] * dX[2];    //spocitej Y pro dany den
            }
        }


        return results;
    }


    public int getTimestampBetaFrom() {
        return timestampBetaFrom;
    }

    public void setTimestampBetaFrom(int timestampBetaFrom) {
        this.timestampBetaFrom = timestampBetaFrom;
    }

    public int getTimestampBetaTo() {
        return timestampBetaTo;
    }

    public void setTimestampBetaTo(int timestampBetaTo) {
        this.timestampBetaTo = timestampBetaTo;
    }

    public int getTimestampXyears() {
        return timestampXyears;
    }

    public void setTimestampXyears(int timestampXyears) {
        this.timestampXyears = timestampXyears;
    }
}

