package app.PREDICTION;

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 OilConsuptionPredictor extends AbstractPredictor {



    int id = -1;
    static int OilRange = 20;
    public OilConsuptionPredictor(int oilRange) {
       Preprocesor p;
        List<PrepConf> dataX = new ArrayList<>();

        dataX.add(new PrepConf("table9:idOlej", "ID oleje", "datumOdberu", -1 ,-1, 0, 35, 0, 35));
        p=new Preprocesor(dataX);
        double[][] posledniID = p.getData();
        if(posledniID.length>0)
            id = (int) posledniID[0][0];

        dataX.clear();



        dataX.add(new PrepConf("table9:nitrace", "Nitrace", "idOlej", id-oilRange, id+1, 0, 35, 0, 35));
        dataX.add(new PrepConf("table9:sulfatace", "sulfatace", "idOlej", id-oilRange, id+1, 38, 48, 38, 48));
        dataX.add(new PrepConf("table9:oxidace", "oxidace", "idOlej", id-oilRange, id+1, 45, 58, 45,58));
        dataX.add(new PrepConf("table9:tBN", "TBN", "idOlej", id-oilRange, id+1, -20, 50, -20, 50));

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

        List<PrepConf> dataY = new ArrayList<>();
        dataY.add(new PrepConf("table9:dobaOleje", "Probeh olej. naplne", "idOlej", id-oilRange, id+1, 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 rozsah){
        double[] results = new double[1];
        double[] betas = getBetaArray();
        List<PrepConf> dataX = new ArrayList<>();

        Preprocesor p;

        dataX.add(new PrepConf("table9:dobaOleje", "posledni doba oleje", "datumOdberu", -1 ,-1, 0, 35, 0, 35));
        p=new Preprocesor(dataX);
        double[][] posledniVymena = p.getData();
        int dobaOleje = -1;
        if(posledniVymena.length>0)
            dobaOleje = (int) posledniVymena[0][0];



            dataX.clear();


                dataX.add(new PrepConf("table9:nitrace", "ocp_nitrace", "dobaOleje", dobaOleje-rozsah, dobaOleje+rozsah, 0, 35, 0, 35));
                dataX.add(new PrepConf("table9:sulfatace", "ocp_sulfatace", "dobaOleje",  dobaOleje-rozsah, dobaOleje+rozsah, 38, 48,38, 48));
                dataX.add(new PrepConf("table9:oxidace", "ocp_oxidace", "dobaOleje",  dobaOleje-rozsah, dobaOleje+rozsah, 45, 58, 45, 58));
                dataX.add(new PrepConf("table9:tBN", "ocp_TBN", "dobaOleje",  dobaOleje-rozsah, dobaOleje+rozsah, -20, 50, -20, 50));

            p = new Preprocesor(dataX);

        double[][] data = p.getAverageData();

            System.out.println("OCP result b"+(betas.length) );
            if(betas.length==5) {
                results[0] = betas[0] + betas[1] * data[0][0] + betas[2] * data[1][0] + betas[3] * data[2][0] + betas[4] * data[3][0];    //spocitej Y pro dany den
                System.out.println(results[0]);
            }



        return results;
    }

    public int getId() {
        return id;
    }
}
