package vut.msa.managedBeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;
import vut.msa.helpers.ClientHelper;
import vut.msa.interfaces.SelectPojo;
import vut.msa.pojos.Backup;
import vut.msa.pojos.BackupDisk;
import vut.msa.pojos.Client;
import vut.msa.utilities.SelectsCreator;

/**
 * Trieda typu JavaBean, ktora poskytuje rozhranie pre pracu s objektom Client
 * (zalohovaci klient) pomocou JSF stranky.
 */
public class ClientsBean implements Serializable {

    private static Logger log = Logger.getLogger(ClientsBean.class);

    private ClientHelper helper;
    private Client client;
    private List<Backup> selectedClientBackups = new ArrayList<Backup>(0);
    private String selectedDisk;
    private int selectedClient;

    private boolean active;
    
    public ClientsBean() {
        helper = new ClientHelper();
        client = new Client();
        //TODO: overit ci je aktivny alebo ne, vlastnost Client triedy.
        active = true;
    }

    //PROPERTY: clients
    public List<Client> getClients() { return helper.getClientsList(); }

    //PROPERTY: active
    public boolean isActive() { return active; }

    //PROPERTY: client
    public Client getClient() { return client; }
    public void setClient(Client client) { this.client = client; }

    //PROPERTY: selectedItem
    public int getSelectedClient() { return selectedClient; }
    public void setSelectedClient(int selectedClient) { this.selectedClient = selectedClient; }

    //PROPERTY: selectedDisk
    public String getSelectedDisk() { return selectedDisk; }
    public void setSelectedDisk(String selectedDisk) {
        this.selectedDisk = selectedDisk;
    }

    //PROPERTY: selectedClientBackups
    public List<Backup> getSelectedClientBackups() { return selectedClientBackups; }

    public List<SelectItem> getClientsToSelectList() {
        List<?> clients = this.getClients();
        return new SelectsCreator().getSelectFromPojoList((List<SelectPojo>) clients);
    }

    /**
     * Metoda vytvori noveho zalohovacieho klienta a ulozi do databazy.
     * Informacie su nacitane z formulara. Tato metoda je zavolana po stlaceni
     * tlacidla pre vytvorenie klienta.
     *
     * @param event udalost stlacenia tlacidla pre vytvorenie klienta na JSF stranke
     */
    public void addClient(ActionEvent event) {

        FacesMessage message = null;
        try {
            if (client == null) {
                throw new Exception("Uzivatel nie je inicializovany.");
            }

            if (helper.getClientByName(client.getClientName()) != null)
                throw new Exception("Klient uz existuje.");

            BackupDisk bd = new BackupDisk(selectedDisk);
            client.setBackupDisk(bd);

            helper.addClient(client);
            message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                    "Klient uspesne vytvoreny.",
                    "Klient uspesne vytvoreny.");
            FacesContext.getCurrentInstance().addMessage(null, message);

            log.info("Klient: " + client.getClientName() + " uspesne vytvoreny.");
            client = new Client();
        } catch (Exception ex) {
            message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
                    ex.getMessage(), ex.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }

    public void selectListener(ValueChangeEvent event) {

        int id =(Integer)event.getNewValue();
        client = helper.getClient(id);
        selectedClientBackups = new ArrayList<Backup>(client.getBackups());
        log.info("Mam zaloh: " + client.getBackupsCount());
        
    }

}
