package de.games.nanoleaf.panelconfig;

import java.io.Serializable;
import java.util.Iterator;

import org.eclnt.editor.annotations.CCGenClass;
import org.eclnt.jsfserver.elements.componentnodes.COLDISTANCENode;
import org.eclnt.jsfserver.elements.componentnodes.LABELNode;
import org.eclnt.jsfserver.elements.componentnodes.PANENode;
import org.eclnt.jsfserver.elements.componentnodes.ROWNode;
import org.eclnt.jsfserver.elements.impl.DYNAMICCONTENTBinding;
import org.eclnt.jsfserver.elements.impl.ROWDYNAMICCONTENTBinding;
import org.eclnt.jsfserver.pagebean.PageBean;

@CCGenClass (expressionBase="#{d.PanelConfigBean}")

public class PanelConfigBean extends PageBean implements Serializable {
    ROWDYNAMICCONTENTBinding m_dynNanoleafLayout = new ROWDYNAMICCONTENTBinding();
    public ROWDYNAMICCONTENTBinding getDynNanoleafLayout() { return m_dynNanoleafLayout; }

    public void onPanelsOff(org.eclnt.jsfserver.base.faces.event.ActionEvent event) {}

    public void onPanelsOn(org.eclnt.jsfserver.base.faces.event.ActionEvent event) {}

    
	String m_nanoleafToken = "LAG28BQWrXK2p79NbHMQsvcYYCN1TcD4";
    public String getNanoleafToken() { return m_nanoleafToken; }
    public void setNanoleafToken(String value) { this.m_nanoleafToken = value; }

    int m_port = 16021;
    public int getPort() { return m_port; }
    public void setPort(int value) { this.m_port = value; }

    String m_ipAdresse = "192.168.178.141";
    public String getIpAdresse() { return m_ipAdresse; }
    public void setIpAdresse(String value) { this.m_ipAdresse = value; }

    String m_hostname = "Canvas-72FA.fritz.box";
    public String getHostname() { return m_hostname; }
    public void setHostname(String value) { this.m_hostname = value; }

	
    DYNAMICCONTENTBinding m_dynPanelList = new DYNAMICCONTENTBinding();
    public DYNAMICCONTENTBinding getDynPanelList() { return m_dynPanelList; }

    public void onPanelsCreate(org.eclnt.jsfserver.base.faces.event.ActionEvent event) {
    	createPanelsList(getAnzahlPanels());
    }

    public void onCheckAndCreate(org.eclnt.jsfserver.base.faces.event.ActionEvent event) {
    	
    	createNanoleafLayoutList(getMatrixX(), getMatrixY());
    }

    int m_matrixY = 3;
    public int getMatrixY() { return m_matrixY; }
    public void setMatrixY(int value) { this.m_matrixY = value; }

    int m_matrixX = 3;
    public int getMatrixX() { return m_matrixX; }
    public void setMatrixX(int value) { this.m_matrixX = value; }

    int m_anzahlPanels = 1;
    public int getAnzahlPanels() { return m_anzahlPanels; }
    public void setAnzahlPanels(int value) { this.m_anzahlPanels = value; }
    
    int m_anzahlPanelsMax = 0;
    public int getAnzahlPanelsMax() { return m_anzahlPanelsMax; }
    public void setAnzahlPanelsMax(int value) { this.m_anzahlPanelsMax = value; }

	
    // ------------------------------------------------------------------------
    // inner classes
    // ------------------------------------------------------------------------
    
    /* Listener to the user of the page bean. */
    public interface IListener extends Serializable {
    	
    }
    
    // ------------------------------------------------------------------------
    // members
    // ------------------------------------------------------------------------
    
    private IListener m_listener;
    
    // ------------------------------------------------------------------------
    // constructors & initialization
    // ------------------------------------------------------------------------

    public PanelConfigBean() {
    	createPanelsList(1);
    }

    private void createPanelsList(int countPanels) {
		
    	ROWNode row = new ROWNode();
    	COLDISTANCENode coldistanceNode = null;
    	PANENode paneNode = null;
    	PanelInfo panelInfo = null;
    	LABELNode labelNode = null;
    	
    	for (int p = 0; p < countPanels; p++) {
    		panelInfo = new PanelInfo(56253 + p);
    		paneNode = new PANENode();
    		paneNode.setHeight(60);
    		paneNode.setWidth(60);
    		paneNode.setBorder("left:2;right:2;top:2;bottom:2;color:#ff0000");
    		paneNode.setDragsend("Nanoleaf:" + panelInfo.getPanelID());
    		ROWNode rowInt = new ROWNode();
    		paneNode.addSubNode(rowInt);
    		coldistanceNode = new COLDISTANCENode();
    		coldistanceNode.setWidth("50%");
    		rowInt.addSubNode(coldistanceNode);
    		labelNode = new LABELNode();
    		labelNode.setText(panelInfo.getPanelID() + "");
    		rowInt.addSubNode(labelNode);
    		
    		row.addSubNode(paneNode);
    		coldistanceNode = new COLDISTANCENode();
    		coldistanceNode.setWidth(20);
    		row.addSubNode(coldistanceNode);
    	}
    	
    	getDynPanelList().setContentNode(row);
		
	}
    
    
    private void createNanoleafLayoutList(int xPanels, int yPanels) {
		
    	ROWNode row = new ROWNode();
    	COLDISTANCENode coldistanceNode = null;
    	PANENode paneNode = null;
    	PanelInfo panelInfo = null;
    	PANENode paneNodeRoot = new PANENode();
    	
    	for (int x = 0; x < xPanels; x++) {
    		row = new ROWNode();
    		for (int y = 0; y < yPanels; y++) {
    			paneNode = new PANENode();
        		paneNode.setHeight(60);
        		paneNode.setWidth(60);
        		paneNode.setBorder("left:2;right:2;top:2;bottom:2;color:#ff0000");
        		paneNode.setDragsend("Nanoleaf:");
        		
        		row.addSubNode(paneNode);
        		coldistanceNode = new COLDISTANCENode();
        		coldistanceNode.setWidth(5);
        		row.addSubNode(coldistanceNode);
    		}
    		paneNodeRoot.addSubNode(row);
    	}
    	
    	getDynNanoleafLayout().setContentNode(paneNodeRoot);
    	
//    	for (int p = 0; p < countPanels; p++) {
//    		panelInfo = new PanelInfo(p);
//    		paneNode = new PANENode();
//    		paneNode.setHeight(60);
//    		paneNode.setWidth(60);
//    		paneNode.setBorder("left:2;right:2;top:2;bottom:2;color:#ff0000");
//    		paneNode.setDragsend("Nanoleaf:");
//    		
//    		row.addSubNode(paneNode);
//    		coldistanceNode = new COLDISTANCENode();
//    		coldistanceNode.setWidth(20);
//    		row.addSubNode(coldistanceNode);
//    	}
    	
    	getDynPanelList().setContentNode(row);
		
	}
    
	public String getPageName() { return "/games/nanoleaf/panelconfig/panelconfig.xml"; }
    public String getRootExpressionUsedInPage() { return "#{d.PanelConfigBean}"; }

    // ------------------------------------------------------------------------
    // public usage
    // ------------------------------------------------------------------------

    /* Initialization of the bean. Add any parameter that is required within your scenario. */
    public void prepare(IListener listener)
    {
        m_listener = listener;
    }

    // ------------------------------------------------------------------------
    // private usage
    // ------------------------------------------------------------------------
}
