package vut.msa.utilities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;
import javax.faces.model.SelectItem;
import org.apache.log4j.Logger;
import org.hyperic.sigar.FileSystem;
import vut.msa.enums.BackupSchedule;
import vut.msa.interfaces.SelectItemable;
import vut.msa.interfaces.SelectPojo;
import vut.msa.monitor.system.ServerInformation;

/**
 * Trieda pre metody na vytvaranie <select> zoznamov na HTML stránkach.
 *
 * @author Martin Starosta(QEX)
 */
public class SelectsCreator implements Serializable {

    private static final transient Logger log = Logger.getLogger(SelectsCreator.class);

    //Objects lists

    //Helpers


    public SelectsCreator() {
    }

    /**
     * Metoda sluzi na vytvorenie zoznamu <select> pre HTML stranky.
     *
     * @return zoznam ako SelectItem
     */
    public List<SelectItem> getSelectFromPojoList(List<SelectPojo> list) {

        List selectItems = new ArrayList<SelectItem>();

        try {
            
            Iterator<SelectPojo> iterator = list.iterator();
            while (iterator.hasNext()) {
                SelectPojo item = iterator.next();
                SelectItem tempItem =
                        new SelectItem(item.getId(), item.getName());
                selectItems.add(tempItem);
            }
        } catch (Exception ex) {
            log.error("Chyba pri vytvarani zoznamu pre element <select>." + ex.getMessage());
        }

        return selectItems;
    }

    /**
     * Metoda sluzi na vytvorenie zoznamu <select> pre HTML stranky z objektov
     * z balika pojos.
     *
     * @return zoznam ako SelectItem
     */
    public List<SelectItem> getSelectList(List<SelectItemable> list) {

        List selectItems = new ArrayList<SelectItem>();

        try {

            Iterator<SelectItemable> iterator = list.iterator();
            while (iterator.hasNext()) {
                SelectItemable item = iterator.next();

                SelectItem tempItem =
                        new SelectItem(item.getId(), item.getName());
                selectItems.add(tempItem);
            }
        } catch (Exception ex) {
            log.error("Chyba pri vytvarani zoznamu pre element <select>." + ex.getMessage());
        }

        return selectItems;
    }

    /**
     * Metoda vytvori zoznam pripojenych diskov pre HTML stranku.
     *
     * @return zoznam s objektami SelectItem pre JSF stranku
     */
    public List<SelectItem> getConnectedDisks() {

        List selectItems = new ArrayList<SelectItem>();

        try {
            ServerInformation info =ServerInformation.getInstance();
            List<FileSystem> fsList = info.getFsList();
            Iterator<FileSystem> iterator = fsList.iterator();
            while (iterator.hasNext()) {

                FileSystem item = iterator.next();

                SelectItem tempItem =
                        new SelectItem(item.getDevName(), item.getDirName());
                selectItems.add(tempItem);
            }
        } catch (Exception ex) {
            log.error("Problem pri vytvarani zoznamu pripojenych diskov. Dovod: " + ex.getMessage());
        }

        return selectItems;
    }

    /**
     * Metoda sluzi na konvertovanie enumeratora na zoznam <select> pre HTML stranky.
     * Metoda vie konvertovat enum. typu Title a UserType. Ako parametre volim rozsah
     * prvkov z enumeratora.
     *
     * @param first zaciatocny prvok zo zvoleneho rozsahu enumeratora
     * @param last konecny prvok zo zvoleneho rozsahu enumeratora
     * @return zoznam objektov typu SelectItem
     */
    public List<SelectItem> convertEnumToSelectList(Enum first, Enum last)  {

        if (first instanceof BackupSchedule) {
            EnumSet<BackupSchedule> enumSet = EnumSet.range(first, last);
            return convertEnum(enumSet);
        } else {
            log.error("Metoda convertEnumToSelectList nemoze konvertovat enumerator.");
            return null;
        }
    }

    /**
     * Metoda sluzi na vytvorenie <select> zoznamu z enumeratora. Enumerator musi
     * implementovat rozhranie SelectIteamble, aby bolo mozne ho konvertovat na
     * zoznam objektov typu SelectItem. Metoda je privatna a sluzi len pre tuto
     * triedu.
     *
     * @param enumSet enumerator, z ktoreho sa vytvori zoznam
     * @return zoznam prvkov typu SelectItem
     */
    private List<SelectItem> convertEnum(EnumSet<?> enumSet) {

        List<SelectItem> selectList = new ArrayList<SelectItem>();

        for (Enum enumType : enumSet) {
            SelectItem item = new SelectItem();

            if (enumType instanceof SelectItemable) {
                SelectItemable sEnum = (SelectItemable) enumType;
                item.setLabel(sEnum.getName());
                item.setValue(enumType);
            } else {
                item.setLabel(enumType.name());
                item.setValue(enumType);
            }
            selectList.add(item);
        }

        return selectList;
    }
}
