package tmaestroplayer.navigation;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author Gonzalo
 */
public class Navigation {

    private String initiallySelected;
    private HashMap<String, ButtonNavigation> navigations;

    public Navigation(String navigationFile) {
        System.out.println("Navigatioooooooon");
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(navigationFile);

            XPath xpath = XPathFactory.newInstance().newXPath();

            initiallySelected = (String) xpath.evaluate("/navigation/initiallySelected/@componentName", doc, XPathConstants.STRING);
            navigations = new HashMap<String, ButtonNavigation>();

            NodeList buttons = (NodeList) xpath.evaluate("/navigation/component", doc, XPathConstants.NODESET);
            for (int i = 0; i<buttons.getLength(); i++) {
                Node button = buttons.item(i);
                String buttonName = (String) xpath.evaluate("./@componentName", button, XPathConstants.STRING);
                String left = (String) xpath.evaluate("./left/@componentName", button, XPathConstants.STRING);
                String right = (String) xpath.evaluate("./right/@componentName", button, XPathConstants.STRING);
                String up = (String) xpath.evaluate("./up/@componentName", button, XPathConstants.STRING);
                String down = (String) xpath.evaluate("./down/@componentName", button, XPathConstants.STRING);
                navigations.put(buttonName, new ButtonNavigation(buttonName, left, right, up, down));
            }

        } catch (XPathExpressionException ex) {
            ex.printStackTrace();
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public HashMap<String, ButtonNavigation> getNavigations() {
        return this.navigations;
    }


    /**
     * main method just for testing and debugging
     * @param args
     */
    public static void main(String[] args) {
        String navigationFileURL = "file:/C:\\Documents and Settings\\Gonzalo\\Mis documentos\\Cursos\\Cocina\\alitas_ingredientes_SCO\\TEMPLATE\\Navigation.xml";
        Navigation nav = new Navigation(navigationFileURL);
        for (Entry<String, ButtonNavigation> buttonEntry : nav.getNavigations().entrySet()) {
            System.out.println(buttonEntry.getKey());
            ButtonNavigation bn = buttonEntry.getValue();
            System.out.println("\tleft: "+bn.getLeft());
            System.out.println("\tright: "+bn.getRight());
            System.out.println("\tup: "+bn.getUp());
            System.out.println("\tdown: "+bn.getDown()+"\n\n");
        }
    }

    /**
     * @return the initiallySelected
     */
    public String getInitiallySelected() {
        return initiallySelected;
    }
}
