package vut.msa.gui.components;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import javax.faces.event.ActionEvent;
import org.apache.log4j.Logger;
import org.richfaces.component.html.HtmlTree;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.TreeNode;
import org.richfaces.model.TreeNodeImpl;
import vut.msa.helpers.ClientHelper;
import vut.msa.pojos.Client;

/*
 * Trieda predstavuje strom uzivatelov. Sluzi pre GUI komponent <rich:tree>.
 * 
 */
public class ClientsTree implements Serializable {

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

    private TreeNode root;
    private TreeNode clients;

    private String nodeTitle;
    private Client selectedClient;

    private ClientHelper helper;

    public ClientsTree() {

        helper = new ClientHelper();
        selectedClient = new Client();

        root = new TreeNodeImpl();
        clients = new TreeNodeImpl();
    }

    //PROPERTY: root
    public TreeNode getRoot() { return root; }
    public void setRoot(TreeNode root) { this.root = root;}

    //PROPERTY: selectedNode
    public Client getSelectedNode() { return selectedClient; }
    public void setSelectedNode(Client selectedClient) { this.selectedClient = selectedClient; }

    /**
     * Metoda naplni strom udajmi. Strom nema podurovne, vsetci uzivatelia
     * patria do rovnakej urovne.
     */
    private void loadDataToTree() {

        ArrayList<Client> clientsList = (ArrayList<Client>) helper.getClientsList();
        Iterator<Client> clientIterator = clientsList.iterator();

        while (clientIterator.hasNext()) {

            Client client = clientIterator.next();
            TreeNodeImpl clientNode = new TreeNodeImpl();
            clientNode.setData(client.getClientName());
            clients.addChild(client, clientNode);
        }
        String label = "Zálohované pobočky (" + clientsList.size() + ")";
        clients.setData(label);
        root.addChild("Pobočka", clients);
    }

    /**
     * Getter pre objekt Tree (strom klientov)
     * @return strom klientov pre GUI
     */
    public TreeNode getTree() {

        //Nacitanie dat do stromu
        loadDataToTree();
        return root;
    }

    /**
     * Metoda sluzi na spracovanie udalosti vyberu polozky v strome klientov.
     * Vybraneho klienta v strome ulozi do objektu selectedClient.
     *
     * @param event udalost vyberu polozky v strome
     */
    public void processSelection(NodeSelectedEvent event) {

        HtmlTree tree = (HtmlTree) event.getComponent();
        nodeTitle = (String) tree.getRowData();
        TreeNode currentNode = tree.getModelTreeNode(tree.getRowKey());
        if (currentNode.isLeaf()) {
            if (currentNode.getData() instanceof String) {
                selectedClient = helper.getClientByName((String) currentNode.getData());
                if (selectedClient == null) {
                    selectedClient = new Client();
                    log.error("Problem s nacitanim klienta pri vybere.");
                }

                log.info("Vybrany klient: " + selectedClient.getClientName());
            }
        }
    }

    /**
     * Metoda sluzi na zmazanie vybraneho klienta v strome klientov.
     *
     * @param event udalost, vyvolana stlacenim tlacidla pre zmazanie
     */
    public void removeSelectedClient(ActionEvent event) {

        if (selectedClient != null) {
            boolean success = helper.removeClient(selectedClient);

            if (success) {
                clients.removeChild(selectedClient);
                log.info("Uzivatel bol zmazany.");
            } else {
                log.error("Zmazanie sa nepodarilo.");
            }
        }
    }
}
