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.UsersHelper;
import vut.msa.pojos.User;

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

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

    private TreeNode root;
    private TreeNode users;

    private String nodeTitle;
    private User selectedUser;

    private UsersHelper helper;

    /**
     * Konštruktor triedy.
     */
    public UsersTree() {

        helper = new UsersHelper();
        selectedUser = new User();

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

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

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

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

        ArrayList<User> usersList = (ArrayList<User>) helper.getUsersList();
        Iterator<User> userIterator = usersList.iterator();

        while (userIterator.hasNext()) {

            User user = userIterator.next();
            TreeNodeImpl userNode = new TreeNodeImpl();
            userNode.setData(user.getUsername());
            users.addChild(user, userNode);
        }
        String label = "Aktívny užívatelia (" + usersList.size() + ")";
        users.setData(label);
        root.addChild("Uzivatelia", users);
    }

    /**
     * Metoda nacita udaje do stromu uzivatelov a potom vrati tento strom.
     *
     * @return strom uzivatelov
     */
    public TreeNode getTree() {

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

    /**
     * Metoda sluzi na spracovanie udalosti vyberu uzivatela v strome. Vybrany
     * uzivatel je ulozeny do objektu selectedUser tejto triedy.
     *
     * @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) {
                selectedUser = helper.getUserByUsername((String)currentNode.getData());
                if (selectedUser == null) {
                    selectedUser = new User();
                }

                log.info("Vybrany uzivatel: " + selectedUser.getUsername());
            }
        }
    }

    /**
     * Metoda zmaze vybraneho uzivatela z databazy. V pripade, ze sa zmazanie
     * z databazy podari, uzivatel je odstraneny aj zo stromu uzivatelov.
     *
     * @param event udalost stlacenie tlacidla pre zmazanie uzivatela
     */
    public void removeSelectedUser(ActionEvent event) {

        if (selectedUser != null) {
            boolean success = helper.removeUser(selectedUser);

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