package de.examples.components;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

import org.eclnt.editor.annotations.CCGenClass;
import org.eclnt.jsfserver.defaultscreens.Statusbar;
import org.eclnt.jsfserver.elements.events.BaseActionEventDrop;
import org.eclnt.jsfserver.elements.events.BaseActionEventInvoke;
import org.eclnt.jsfserver.pagebean.PageBean;

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

public class DragDropPaneBean
    extends PageBean 
    implements Serializable
{
	public void onDropSendPaneTarget(org.eclnt.jsfserver.base.faces.event.ActionEvent event) {
    	if (event instanceof BaseActionEventDrop)
        {
        	
            Statusbar.outputSuccess("Drop-Event was processed: " + event.getClass().getName());
            BaseActionEventDrop baed = (BaseActionEventDrop)event;
            String dragInfo = baed.getDragInfo();
             
            System.out.println(baed.getSourceReference());
            
            if (dragInfo.startsWith("NANOLEAF:"))
            {
            	String nanoID = dragInfo.substring("NANOLEAF:".length());
            	System.out.println("NanoLeaf ID: " + nanoID);
            	int paneID = 0;
            	try {
//                	
                	paneID = getPaneIDSourceMap().get(nanoID);
//                	
				} catch (Exception e) {
					e.printStackTrace();
				}
            	
            	System.out.println("Pane ID " + paneID);
//            	
            	PaneInfoTest paneInfoTest = getPanelSourceList().get(paneID);
            	
            	int posX = 1;
            	int posY = 2;
            	
            	getPaneRepeatList().get(posX).getPaneInfoList().set(posY, paneInfoTest);
            	
            	System.out.println("Ziel: ");
                System.out.println("Drop: " + dragInfo);
            }
        }
    }
    
    HashMap<String, Integer> m_paneIDSourceMap = null;

	public HashMap<String, Integer> getPaneIDSourceMap() {
		if (m_paneIDSourceMap == null) {
			m_paneIDSourceMap = new HashMap<String, Integer>();
		}
		return m_paneIDSourceMap;
	}

	int m_yMatrix = 7;
    public int getYMatrix() { return m_yMatrix; }
    public void setYMatrix(int value) { this.m_yMatrix = value; }

    int m_xMatrix = 7;
    public int getXMatrix() { return m_xMatrix; }
    public void setXMatrix(int value) { this.m_xMatrix = value; }

    int m_countPanels = 15;
    public int getCountPanels() { return m_countPanels; }
    public void setCountPanels(int value) { this.m_countPanels = value; }


    List<PaneRepeat> m_paneRepeatList = new ArrayList<PaneRepeat>();
    public List<PaneRepeat> getPaneRepeatList() { return m_paneRepeatList; }

    List<PaneInfoTest> m_panelSourceList = new ArrayList<PaneInfoTest>();
    public List<PaneInfoTest> getPanelSourceList() { return m_panelSourceList; }

    
    
	


    // ------------------------------------------------------------------------
    // inner classes
    // ------------------------------------------------------------------------
    
    public class PaneRepeat {
    	private int i_paneRepeatID = 0;
    	private List<PaneInfoTest> i_paneInfoList = null;
		public int getPaneRepeatID() {
			return i_paneRepeatID;
		}
		public void setPaneRepeatID(int paneRepeatID) {
			i_paneRepeatID = paneRepeatID;
		}
		public List<PaneInfoTest> getPaneInfoList() {
			return i_paneInfoList;
		}
		public void setPaneInfoList(List<PaneInfoTest> paneInfoList) {
			i_paneInfoList = paneInfoList;
		}
    	
    	
    	
    }
    
	public class PaneInfoTest {    
        
		private int i_paneID = 0;
		private String i_color = null;
		private String i_border = null;
		private String i_reference = null;
		Integer i_nanoleafID;
		String i_drangsendSource;
		
		
		public int getPaneID() { return i_paneID; }
		public void setPaneID(int paneID) { i_paneID = paneID; }

		public String getColor() { return i_color; }
		public void setColor(String color) { 
			i_color = color; 
			createBorder();
		}

		public String getBorder() { return i_border; }
		public void setBorder(String border) { i_border = border; }
		
		public String getReference() { return i_reference; }
		public void setReference(String reference) { i_reference = reference; }
		
		public Integer getNanoleafID() { return i_nanoleafID; }
		public void setNanoleafID(Integer value) { this.i_nanoleafID = value; }
		
	    public String getDrangsendSource() { return i_drangsendSource; }
	    public void setDrangsendSource(String value) { this.i_drangsendSource = value; }


		public void createBorder() {
			String border = "left:1;right:1;top:1;bottom:1;color:" + getColor();
			
			setBorder(border);
		}

		public PaneInfoTest(int paneID) {
			this.i_paneID = paneID;
		}
		

	}


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

    public DragDropPaneBean() {
    	
    	createSourcePanels(getCountPanels());
    	try {
    		createDynTargetPanels(getXMatrix(), getYMatrix());
		} catch (Exception e) {
			e.printStackTrace();
		}
    	
    }

    
	private void createDynTargetPanels(int matrixX, int matrixY) throws Exception {
		System.out.println("createDynTargetPanels: " + matrixX + "-" + matrixY);

		List<PaneInfoTest> paneInfoList = null;
		PaneRepeat paneRepeat = new PaneRepeat();
    	int panelID = 0;

    	for (int x = 0; x < matrixX; x++) {

    		paneInfoList = new ArrayList<PaneInfoTest>();
    		paneRepeat = new PaneRepeat();
    		
    		for (int y = 0; y < matrixY; y++) {
    			PaneInfoTest paneInfoTest = new PaneInfoTest(panelID);
    			paneInfoTest.setReference("REF_XY|" + x + "|" + y);
    			paneInfoTest.setColor("#a5a5a5");
    			paneInfoTest.setNanoleafID(panelID);
    			
//    			paneInfoTest.setDrangsendSource("#{d.DragDropPaneBean.onDragSendPaneSource}");
    			
    			paneInfoList.add(paneInfoTest);
    			
    			
    			panelID++;
    			
    		}
    		paneRepeat.setPaneRepeatID(x);
    		paneRepeat.setPaneInfoList(paneInfoList);
    		
    		getPaneRepeatList().add(paneRepeat);
    		
    	}
	}



	


	private void createSourcePanels(int countPanels) {
    	
    	ThreadLocalRandom random = ThreadLocalRandom.current();
    	
    	for (int p = 0; p < countPanels; p++) {
    		PaneInfoTest paneInfoTest = null;
    		Integer randomID = random.nextInt(100000, 999999);
    		paneInfoTest = new PaneInfoTest(p);
    		paneInfoTest.setNanoleafID(randomID);
    		paneInfoTest.setColor("#a5a5a5");
    		paneInfoTest.setDrangsendSource("NANOLEAF:" + randomID);

    		getPaneIDSourceMap().put(randomID.toString(), Integer.valueOf(p));
    		
    		getPanelSourceList().add(paneInfoTest);
    		
    	}
		
	}



	public String getPageName() { return "/examples/componets/dragDropPane.xml"; }
    public String getRootExpressionUsedInPage() { return "#{d.DragDropPaneBean}"; }

    // ------------------------------------------------------------------------
    // 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
    // ------------------------------------------------------------------------
}
