package app.GUI;

import app.DAO.DashboardPanelWidget;
import app.HibernateUtil;
import app.PREPROCESSING.PrepConf;
import app.PREPROCESSING.Preprocesor;
import org.hibernate.Session;
import org.primefaces.event.CloseEvent;
import org.primefaces.event.DashboardReorderEvent;
import org.primefaces.event.ToggleEvent;
import org.primefaces.model.DashboardModel;
import org.primefaces.model.DefaultDashboardColumn;
import org.primefaces.model.DefaultDashboardModel;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
 * Created by Petr Flégl on 26.1.2017.
 */

@ManagedBean
@ViewScoped
public class DashboardView implements Serializable {

    private DashboardModel model;
    private List<DefaultDashboardColumn> cols = new ArrayList<>();
    @PostConstruct
    public void init() {
        model = new DefaultDashboardModel();
        cols.add(new DefaultDashboardColumn());
        cols.add(new DefaultDashboardColumn());
        cols.add(new DefaultDashboardColumn());
        cols.add(new DefaultDashboardColumn());
        cols.add(new DefaultDashboardColumn());

        Session s= HibernateUtil.getSessionFactory().openSession();
        s.beginTransaction();
        for(int col = 0;col<cols.size();col++){
            List<DashboardPanelWidget> panelWidgets = s.createQuery("FROM DashboardPanelWidget WHERE col = "+col+" ORDER BY row ASC ").list();
            for (Iterator iterator = panelWidgets.iterator(); iterator.hasNext();){
                DashboardPanelWidget widget = (DashboardPanelWidget) iterator.next();
                cols.get(col).addWidget(widget.getRow(),widget.getNameID());

            }
            model.addColumn(cols.get(col));
        }
        s.close();
    }

    public void handleReorder(DashboardReorderEvent event) {
        FacesMessage message = new FacesMessage();
        message.setSeverity(FacesMessage.SEVERITY_WARN);
        message.setSummary("Reordered: " + event.getWidgetId());
        message.setDetail("Item index: " + event.getItemIndex() + ", Column index: " + event.getColumnIndex() + ", Sender index: " + event.getSenderColumnIndex());

        addMessage(message);

        Session s= HibernateUtil.getSessionFactory().openSession();
        s.beginTransaction();
        s.saveOrUpdate(new DashboardPanelWidget(event.getWidgetId(),event.getItemIndex(),event.getColumnIndex()));
        //s.get(DashboardPanelWidget.class,event.getWidgetId());
        s.getTransaction().commit();
        s.close();
        message.setDetail("Done");
        addMessage(message);
    }

    public void handleClose(CloseEvent event) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Panel Closed", "Closed panel id:'" + event.getComponent().getId() + "'");

        addMessage(message);
    }

    public void handleToggle(ToggleEvent event) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, event.getComponent().getId() + " toggled", "Status:" + event.getVisibility().name());

        addMessage(message);
    }

    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public DashboardModel getModel() {
        return model;
    }

    public double getValueFor(String viewID){
        List<PrepConf> c = new ArrayList<>();
        if(viewID.equals("vykonO")){
            c.add(new PrepConf("table8:vykonSvorky", "Svorkova vyroba h", "idParametryElektro", lastID, lastID+1, 0, 100, 0, 100));
        }else if(viewID.equals("vykonD")){
            c.add(new PrepConf("table10:vykon", "Vykon denní", "datum", lastID, lastID+1, 0, 100, 0, 100));
        }else if(viewID.equals("mthCO")) {
            c.add(new PrepConf("oilT:celkem", "Celkovy provoz", "IdO", lastID, lastID + 1, 0, 100, 0, 100));
        }

        else if(viewID.equals("mthOO")) {
            c.add(new PrepConf("oilT:najeto", "Najeto na oleji", "IdO", lastID, lastID + 1, 0, 100, 0, 100));
        }else if(viewID.equals("mthDVO")) {
            c.add(new PrepConf("oilT:zbyva", "zbyva do vymeny", "IdO", lastID, lastID + 1, 0, 100, 0, 100));
        }else if(viewID.equals("teplotaOO")) {
            c.add(new PrepConf("oilT:teplota", "teplota oleje", "IdO", lastID, lastID + 1, 0, 100, 0, 100));
        }

        else if(viewID.equals("vykonOO")) {
            c.add(new PrepConf("elektroT:svorkOK", "Okamzity vykon", "IdElT", lastID, lastID + 1, 0, 100, 0, 100));
        } else if(viewID.equals("vykonDO")) {
            c.add(new PrepConf("elektroT:svorkD", "Svorkova vyroby denni", "IdElT", lastID, lastID + 1, 0, 100, 0, 100));
        } else if(viewID.equals("vykonDpO")) {
            c.add(new PrepConf("elektroT:prodejD", "Denni prodej", "IdElT", lastID, lastID + 1, 0, 100, 0, 100));
        } else if(viewID.equals("spotrebaADO")) {
            c.add(new PrepConf("elektroT:spotAD", "Spotreba Areaulu", "IdElT", lastID, lastID + 1, 0, 100, 0, 100));
        } else if(viewID.equals("spotrebaTDO")) {
            c.add(new PrepConf("elektroT:spotBPSD", "Spotreba BPS", "IdElT", lastID, lastID + 1, 0, 100, 0, 100));
        } else if(viewID.equals("vykonMO")) {
            c.add(new PrepConf("elektroT:svorkM", "Svorky mesicni", "IdElT", lastID, lastID + 1, 0, 100, 0, 100));
        } else if(viewID.equals("vykonMPO")) {
            c.add(new PrepConf("elektroT:prodejM", "Mesicni prodej", "IdElT", lastID, lastID + 1, 0, 100, 0, 100));
        } else if(viewID.equals("vykonMBO")) {
            c.add(new PrepConf("elektroT:bonusM", "Mesicni zeleny bonus", "IdElT", lastID, lastID + 1, 0, 100, 0, 100));
        }


        Preprocesor p = new Preprocesor(c);
        double out[][] = p.getData();
        if(out.length>0 && out[0].length>0)
            return out[0][0];
        else
            return 0;
    }
    private int lastID = 0;
    public void increment(){
        lastID++;
    }

}