package vut.msa.managedBeans;

import java.io.Serializable;
import java.util.List;
import javax.faces.model.SelectItem;
import org.primefaces.model.DualListModel;
import vut.msa.enums.BackupSchedule;
import vut.msa.gui.components.ClientsTree;
import vut.msa.gui.components.DisksPickList;
import vut.msa.gui.components.ServerInfoDashboard;
import vut.msa.gui.components.UsersTree;
import vut.msa.helpers.BackupDiskHelper;
import vut.msa.helpers.UsersHelper;
import vut.msa.interfaces.SelectItemable;
import vut.msa.monitor.system.ServerInformation;
import vut.msa.utilities.LogReader;
import vut.msa.utilities.SelectsCreator;

/**
 * Trieda sluzi na ukladanie uzivatelskych dat pre jednotlive relacie. Poskytuje
 * aj zakladne funkcie pre navigaciu a GUI.
 */
public class UserSession implements Serializable{

    private String locale;
    private UsersHelper helper;
    private String actualPage;
    private LogReader reader;

    private ServerInformation information;

    //GUI
    private UsersTree usersTree;
    private ClientsTree clientsTree;
    private ServerInfoDashboard serverDashboard;

    private SelectsCreator creator;
    private List<SelectItem> backupTypes;
    private List<SelectItem> connectedDisks;
    private List<SelectItem> backupDisks;
    private DualListModel<String> disks;

    public UserSession () {
        this.locale = "sk";
        this.actualPage = "/administration/usersAdmin.jsp";
        this.helper = new UsersHelper();

        information = ServerInformation.getInstance();

        this.usersTree = new UsersTree();
        this.clientsTree = new ClientsTree();
        this.serverDashboard = new ServerInfoDashboard();

        reader = new LogReader();

        creator = new SelectsCreator();
        this.backupTypes = creator.convertEnumToSelectList(BackupSchedule.DAILY, BackupSchedule.MOUNTHLY);
        this.connectedDisks = creator.getConnectedDisks();
        this.disks = new DisksPickList().getDisks();
    }

    //PROPERTY: helper (READ-ONLY)
    public UsersHelper getHelper() { return this.helper; }

    //PROPERTY: information (READ-ONLY)
    public ServerInformation getInformation() { return information; }

    //PROPERTY: locale (READ-ONLY)
    public String getLocale() { return this.locale; }

    //PROPERTY: usersTree
    public UsersTree getUsersTree() { return usersTree; }
    public void setUsersTree(UsersTree tree) { this.usersTree = tree; }

    //PROPERTY: clientsTree
    public ClientsTree getClientsTree() { return clientsTree; }
    public void setClientsTree(ClientsTree clientsTree) { this.clientsTree = clientsTree; }

    //PROPERTY: serverDashboard
    public ServerInfoDashboard getServerDashboard() { return serverDashboard; }
    public void setServerDashboard(ServerInfoDashboard serverDashboard) { this.serverDashboard = serverDashboard; }

    //SelectList: backupTypes
    public List<SelectItem> getBackupTypes() { return backupTypes; }
    public void setBackupTypes(List<SelectItem> backupTypes) { this.backupTypes = backupTypes; }

    //PROPERTY: connectedDisks
    public List<SelectItem> getConnectedDisks() { return connectedDisks; }
    public void setConnectedDisks(List<SelectItem> connectedDisks) { this.connectedDisks = connectedDisks; }

    //PROPERTY: backupDisks
    public List<SelectItem> getBackupDisks() {
        BackupDiskHelper bdHelper = new BackupDiskHelper();
        List<?> tempList = bdHelper.getBackupDisksList();
        backupDisks = creator.getSelectList((List<SelectItemable>) tempList);
        return backupDisks;
    }
    public void setBackupDisks(List<SelectItem> backupDisks) { this.backupDisks = backupDisks; }

    //PROPERTY: disks
    public DualListModel<String> getDisks() { return disks; }
    public void setDisks(DualListModel<String> disks) { this.disks = disks; }

    public String getLastLogEntry() {
        if (reader != null)
            return reader.getLastLine();
        else
            return "";
    }

    /* Aktualna stranka v strednom okne a navigacne metody */
    public String getActualPage() { return actualPage; }

    /* Navigation functions */
    public String showMonitoring() {
        actualPage = "/monitor_report/monitor.jsp";
        return "";
    }

    public String showReporting() {
        actualPage = "/monitor_report/report.jsp";
        return "";
    }

    public String showServerStatus() {
        actualPage = "/monitor_report/serverstat.jsp";
        return "";
    }

    public String showMediaAdmin() {
        actualPage = "/media/management.jsp";
        return "";
    }

    public String showUserManagement() {
        actualPage = "/administration/usersAdmin.jsp";
        return "";
    }

    public String showClientsManagement() {
        actualPage = "/administration/clientsAdmin.jsp";
        return "";
    }

    public String showSystemManagement() {
        actualPage = "/administration/systemAdmin.jsp";
        return "";
    }
}
