package vut.msa.gui.components;

import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.event.DashboardReorderEvent;
import org.primefaces.model.DashboardColumn;
import org.primefaces.model.DashboardModel;
import org.primefaces.model.DefaultDashboardColumn;
import org.primefaces.model.DefaultDashboardModel;

/**
 * Trieda vytvára dashboard pre zobrazenie informácií o serveri.
 *
 */
public class ServerInfoDashboard implements Serializable {

    private DashboardModel model;

    /**
     * Konštruktor triedy
     */
    public ServerInfoDashboard() {
        model = new DefaultDashboardModel();
        DashboardColumn column1 = new DefaultDashboardColumn();
        DashboardColumn column2 = new DefaultDashboardColumn();

        column1.addWidget("memory");
        column1.addWidget("cpu");
        column1.addWidget("state");
        
        column2.addWidget("network");


        model.addColumn(column1);
        model.addColumn(column2);

    }

    /**
     * Metóda slúži na spracovanie udalosti pri zmene rozlozenia jednotlivych
     * poli dashboardu.
     *
     * @param event udalost, vyvolana pri zmene
     */
    public void handleReorder(DashboardReorderEvent event) {
        FacesMessage message = new FacesMessage();
        message.setSeverity(FacesMessage.SEVERITY_INFO);
        message.setSummary("Zmene poradie: " + event.getWidgetId());
        message.setDetail("Index polozky: " + event.getItemIndex()
                            + ", Intex stlpca: " + event.getColumnIndex()
                            + ", Index odosielatela: " + event.getSenderColumnIndex());

        addMessage(message);
    }

    /**
     * Metoda zobrazuje spravu v kontexte JSF. Metoda sluzi pre DashBoard.
     * @param message
     */
    private void addMessage(FacesMessage message) {
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    /**
     * Metoda vracia objekt DashboardModel, ktory sluzi pre zobrazenie dashboardu
     * na stranke JSP.
     *
     * @return model pre dashboard
     */
    public DashboardModel getModel() {
        return model;
    }

}
