package com.uvigo.tMaestro.SCOCreator.tool;

import com.uvigo.mhp.Page;
import com.uvigo.mhp.ui.HScrollBar;
import java.awt.Color;
import java.io.File;
import java.util.Hashtable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.*;
import javax.xml.xpath.*;
import org.w3c.dom.*;

/**
 *
 * @author mvicente
 */
public class Template extends Page {

    private String sNombre;
    private Hashtable<String, com.uvigo.mhp.ui.HScrollBar> hscrollbars = new Hashtable<String, HScrollBar>();
    
    public Template(String sNombre) {
        this.sNombre = sNombre;
        this.setLayout(null);
        try {
            String expresion;
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            DocumentBuilder db = dbf.newDocumentBuilder();
            //TODO le añado MainFrame.oMainFrame.sDir a la ruta
            //Document doc = db.parse("TEMPLATES/"+ sNombre+ "/TemplateComponents.xml");
            System.out.println(MainFrame.oMainFrame.sDir+"/"+"TEMPLATES/"+ sNombre+ "/TemplateComponents.xml");
            Document doc = db.parse(new File(MainFrame.oMainFrame.sDir+"/"+"TEMPLATES/"+ sNombre+ "/TemplateComponents.xml"));
            XPath xpath = XPathFactory.newInstance().newXPath();

            expresion = "//component[@type='" + sNombre + "']";
            Node nplantilla = (Node) xpath.evaluate(expresion, doc, XPathConstants.NODE);
            NamedNodeMap attplantilla = nplantilla.getAttributes();
            this.setName(attplantilla.getNamedItem("componentName").getTextContent());
            //this.setBounds(0, 0, new Integer(attplantilla.getNamedItem("w").getTextContent()).intValue(), new Integer(attplantilla.getNamedItem("h").getTextContent()).intValue());
            this.setBounds(0, 0, 0, 0);

            expresion = "/templateComponents/component/component[@type]";
            NodeList nl = (NodeList) xpath.evaluate(expresion, doc, XPathConstants.NODESET);
            for (int i = 0; i < nl.getLength(); i++) {
                addComponents(nl.item(i), this);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //Funcion recursiva que 'rellena' la pseudoplantilla
    private void addComponents(Node n, java.awt.Container cPadre) {
        XPath xpath = XPathFactory.newInstance().newXPath();
        String expresion;

        NamedNodeMap att = n.getAttributes();
        java.awt.Component oComponent = newComponent(att.getNamedItem("type").getTextContent(), att.getNamedItem("componentName").getTextContent());
//        System.out.println("nodo-'type': " + n.getAttributes().getNamedItem("type").getTextContent());
//        System.out.println("nodo-'x': " + new Integer(att.getNamedItem("x").getTextContent()).intValue());
//        System.out.println("nodo-'y': " + new Integer(att.getNamedItem("y").getTextContent()).intValue());
//        System.out.println("nodo-'w': " + new Integer(att.getNamedItem("w").getTextContent()).intValue());
//        System.out.println("nodo-'h': " + new Integer(att.getNamedItem("h").getTextContent()).intValue());
        if(!(cPadre instanceof com.uvigo.mhp.ui.HScrollPane)) oComponent.setBounds(new Integer(att.getNamedItem("x").getTextContent()).intValue(),
                new Integer(att.getNamedItem("y").getTextContent()).intValue(),
                new Integer(att.getNamedItem("w").getTextContent()).intValue(),
                new Integer(att.getNamedItem("h").getTextContent()).intValue());
        oComponent.setName(att.getNamedItem("componentName").getTextContent());
//        System.out.println("---------- " + oComponent.getName());

        oComponent.setBackground(new java.awt.Color(102, 102, 255));

        if (cPadre instanceof com.uvigo.mhp.ui.HMultiPanel) {
            cPadre.add(oComponent, att.getNamedItem("z").getTextContent());
        } else if(cPadre instanceof com.uvigo.mhp.ui.HScrollPane){
           ((com.uvigo.mhp.ui.HScrollPane)cPadre).setViewportView(oComponent);
            //System.out.println("Template- viewport "+((com.uvigo.mhp.ui.HScrollPane)cPadre).getViewport().getView().getName());
        } else {
            cPadre.setComponentZOrder(oComponent, new Integer(att.getNamedItem("z").getTextContent()).intValue());
            cPadre.add(oComponent);
        }
        if (n.hasChildNodes()) {
            if (!(oComponent instanceof com.uvigo.mhp.ui.HMultiPanel)) {
                ((java.awt.Container) oComponent).setLayout(null);
            }
            try {
                java.awt.Container oContainer = (java.awt.Container) oComponent;
                expresion = "./component[@type]";
                NodeList nl = (NodeList) xpath.evaluate(expresion, n, XPathConstants.NODESET);
                for (int i = 0; i < nl.getLength(); i++) {
                    addComponents(nl.item(i), oContainer);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //Funcion que obtiene el objeto del tipo component indicado
    private java.awt.Component newComponent(String sComponentType, String sComponentName) {
        java.awt.Component oC = null;
        if (sComponentType.equalsIgnoreCase("HLabel")) oC = new com.uvigo.mhp.ui.HLabel();
        else if (sComponentType.equalsIgnoreCase("HImage")) oC = new com.uvigo.mhp.ui.HImage();
        else if (sComponentType.equalsIgnoreCase("HButton")) oC = new com.uvigo.mhp.ui.HButton();
        else if (sComponentType.equalsIgnoreCase("MediaPlayer")) {
            oC = new com.uvigo.mhp.mediaPlayer.MediaPlayer();
           ((com.uvigo.mhp.mediaPlayer.MediaPlayer) oC).setContextManager(new com.uvigo.mhp.ContextManager());
        }
        else if (sComponentType.equalsIgnoreCase("TextPanel")) oC = new com.uvigo.mhp.textPanel.TextPanel();
        else if (sComponentType.equalsIgnoreCase("HAnimated")) oC = new com.uvigo.mhp.ui.HAnimated();
        else if (sComponentType.equalsIgnoreCase("HPanel")) oC = new com.uvigo.mhp.ui.HPanel();
        else if (sComponentType.equalsIgnoreCase("HSwitch")) oC = new com.uvigo.mhp.ui.HSwitch();
        else if (sComponentType.equalsIgnoreCase("ActiveImage")) {
            oC = new com.uvigo.mhp.activeImage.ActiveImage();
             ((com.uvigo.mhp.activeImage.ActiveImage) oC).setContextManager(new com.uvigo.mhp.ContextManager());
        }
        else if (sComponentType.equalsIgnoreCase("HMultiPanel")) {
            oC = new com.uvigo.mhp.ui.HMultiPanel();
             ((com.uvigo.mhp.ui.HMultiPanel) oC).setContextManager(new com.uvigo.mhp.ContextManager());
        }
        else if (sComponentType.equalsIgnoreCase("HScrollBar")) {
            oC = new com.uvigo.mhp.ui.HScrollBar();
            hscrollbars.put(sComponentName, (com.uvigo.mhp.ui.HScrollBar) oC);
        } else if (sComponentType.equalsIgnoreCase("HInternalFrame")) oC = new com.uvigo.mhp.ui.HInternalFrame();
        else if (sComponentType.equalsIgnoreCase("HScrollPane")) {
            try {
                oC = new com.uvigo.mhp.ui.HScrollPane();

                String expresion;
                DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

                DocumentBuilder db = dbf.newDocumentBuilder();
                //TODO le añado MainFrame.oMainFrame.sDir a la ruta
                //Document doc = db.parse("TEMPLATES/"+ sNombre + "/ScrollPanelBar.xml");
                Document doc = db.parse(new File(MainFrame.oMainFrame.sDir+"/"+"TEMPLATES/"+ sNombre + "/ScrollPanelBar.xml"));

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

                expresion = "//scrollPanel[@componentName='" + sComponentName + "']";
                Node nsp = (Node) xpath.evaluate(expresion, doc, XPathConstants.NODE);
                expresion = "./horizontal";
                Node nh = (Node) xpath.evaluate(expresion, nsp, XPathConstants.NODE);
                if (nh != null)
                    ((com.uvigo.mhp.ui.HScrollPane) oC).setHorizontalScrollControl(hscrollbars.get(nh.getAttributes().getNamedItem("componentName").getTextContent()));
                expresion = "./vertical";
                Node nv = (Node) xpath.evaluate(expresion, nsp, XPathConstants.NODE);
                if (nv != null)
                    ((com.uvigo.mhp.ui.HScrollPane) oC).setVerticalScrollControl(hscrollbars.get(nv.getAttributes().getNamedItem("componentName").getTextContent()));
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        } else if (sComponentType.equalsIgnoreCase("HMenu")) {
            oC = new com.uvigo.mhp.ui.HMenu();
             ((com.uvigo.mhp.ui.HMenu) oC).setContextManager(new com.uvigo.mhp.ContextManager());
        }

        return oC;
    }
}
