package de.nanoleaf.games.lightswitch;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.eclnt.editor.annotations.CCGenClass;
import org.eclnt.jsfserver.base.faces.event.ActionEvent;
import org.eclnt.jsfserver.pagebean.PageBean;

import de.tools.ReflectClass;



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

public class LightsOnOffBean extends PageBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private Boolean[][] statusArray;
    boolean m_enabledButton1;
    public boolean getEnabledButton1() { return m_enabledButton1; }
    public void setEnabledButton1(boolean value) { this.m_enabledButton1 = value; }
    
    boolean m_enabledButton2;
    public boolean getEnabledButton2() { return m_enabledButton2; }
    public void setEnabledButton2(boolean value) { this.m_enabledButton2 = value; }
    
    boolean m_enabledButton3;
    public boolean getEnabledButton3() { return m_enabledButton3; }
    public void setEnabledButton3(boolean value) { this.m_enabledButton3 = value; }
    
    boolean m_enabledButton4;
    public boolean getEnabledButton4() { return m_enabledButton4; }
    public void setEnabledButton4(boolean value) { this.m_enabledButton4 = value; }
    
    boolean m_enabledButton5;
    public boolean getEnabledButton5() { return m_enabledButton5; }
    public void setEnabledButton5(boolean value) { this.m_enabledButton5 = value; }
    
    boolean m_enabledButton6;
    public boolean getEnabledButton6() { return m_enabledButton6; }
    public void setEnabledButton6(boolean value) { this.m_enabledButton6 = value; }
    
    boolean m_enabledButton7;
    public boolean getEnabledButton7() { return m_enabledButton7; }
    public void setEnabledButton7(boolean value) { this.m_enabledButton7 = value; }
    
    boolean m_enabledButton8;
    public boolean getEnabledButton8() { return m_enabledButton8; }
    public void setEnabledButton8(boolean value) { this.m_enabledButton8 = value; }
    
    boolean m_enabledButton9;
    public boolean getEnabledButton9() { return m_enabledButton9; }
    public void setEnabledButton9(boolean value) { this.m_enabledButton9 = value; }

    int m_countSteps = 0;
    public int getCountSteps() { return m_countSteps; }
    public void setCountSteps(int value) { this.m_countSteps = value; }

    String m_resultText;
    public String getResultText() { return m_resultText; }
    public void setResultText(String value) { this.m_resultText = value; }

    public void onReset(ActionEvent event) {
    	statusArray = new Boolean[3][3];
    	nextButtons = new ArrayList<String>();
    	
    	
    	enabledAllButtons(true);
    	random = null;
    	m_countSteps = 0;
    	m_colorButton1 = null;
    	m_colorButton2 = null;
    	m_colorButton3 = null;
    	m_colorButton4 = null;
    	m_colorButton5 = null;
    	m_colorButton6 = null;
    	m_colorButton7 = null;
    	m_colorButton8 = null;
    	m_colorButton9 = null;
    	
    	int counter = 0;
    	for (int r = 0; r < statusArray.length; r++) {
    		for (int s = 0; s < statusArray[r].length; s++) {
    			counter++;
    		
    			if (getRandomBoolean()) {
    				getReflectClass().setMethod("setColorButton" + counter, getLightOn());
    				statusArray[r][s] = true;
    			} else {
    				statusArray[r][s] = false;
    			}
    			
    		}
    	}
    	
    	
    	resultTextSetter();
    }
    
    
    
    public static String m_lightOn = "#66FF00";
    public static String getLightOn() { return m_lightOn; }
    
    private Random random;

    public boolean getRandomBoolean() {
    	if (random == null) {
    		random = new Random();
    	}
        return random.nextBoolean();
    }

	String m_colorButton1;
    public String getColorButton1() { return m_colorButton1; }
    public void setColorButton1(String value) { this.m_colorButton1 = value; }

    String m_colorButton2;
    public String getColorButton2() { return m_colorButton2; }
    public void setColorButton2(String value) { this.m_colorButton2 = value; }
    
    String m_colorButton3;
    public String getColorButton3() { return m_colorButton3; }
    public void setColorButton3(String value) { this.m_colorButton3 = value; }
    
    String m_colorButton4;
    public String getColorButton4() { return m_colorButton4; }
    public void setColorButton4(String value) { this.m_colorButton4 = value; }
    
    String m_colorButton5;
    public String getColorButton5() { return m_colorButton5; }
    public void setColorButton5(String value) { this.m_colorButton5 = value; }
    
    String m_colorButton6;
    public String getColorButton6() { return m_colorButton6; }
    public void setColorButton6(String value) { this.m_colorButton6 = value; }
    
    String m_colorButton7;
    public String getColorButton7() { return m_colorButton7; }
    public void setColorButton7(String value) { this.m_colorButton7 = value; }
    
    String m_colorButton8;
    public String getColorButton8() { return m_colorButton8; }
    public void setColorButton8(String value) { this.m_colorButton8 = value; }
    
    String m_colorButton9;
    public String getColorButton9() { return m_colorButton9; }
    public void setColorButton9(String value) { this.m_colorButton9 = value; }
    
	
	private void stepper() {
		m_countSteps++;
	}
	
	private void enabledAllButtons(boolean enabled) {
		for (int i = 1; i <= 9; i++) {
			getReflectClass().setMethod("setEnabledButton" + i, enabled);
		}
	}
	
	
	private void resultTextSetter() {
		
		if (checkFinish()) {
			m_resultText = "Du hast mit " + getCountSteps() + " Schritten GEWONNEN. :-)";
			enabledAllButtons(false);
			
		} else {
			m_resultText = "Du hast bis jetzt " + getCountSteps() + " Schritte benötigt.";
		}
	}
	
	private boolean checkFinish() {
		boolean finish = true;
		boolean onOrOff = false;
		boolean buttonStatus = false;
		
		for (int r = 0; r < statusArray.length; r++) {
			for (int s = 0; s < statusArray[r].length; s++) {
				if (r == 0 && s == 0) {
					onOrOff = statusArray[r][s];
				}
				buttonStatus = statusArray[r][s];
				
				if (onOrOff != buttonStatus) {
					finish = false;
					break;
				}
				
			}
			if (finish == false) {
				break;
			}
		}
		return finish;
	}

    private void changeButtons() {
    	int r = 0;
    	int s = 0;
    	Boolean status;
    	int button = 0;
    	for (String buttonNext: nextButtons) {
    		r = Integer.parseInt(buttonNext.substring(0, 1));
    		s = Integer.parseInt(buttonNext.substring(1));
    		button = arrayButtonMap.get(buttonNext);
    		
    		
    		
    		status = statusArray[r][s];
    		
    		//System.out.println(button + " - " + buttonNext + " - " + status);
    		if (status.booleanValue() == true) {
    			getReflectClass().setMethod("setColorButton" + button, "");
				statusArray[r][s] = false;
			} else {
				getReflectClass().setMethod("setColorButton" + button, getLightOn());
				statusArray[r][s] = true;
			}
    	}
    	
    	
    	
    	nextButtons.clear();
	}

	
	List<String> nextButtons;
	
    public void onButton1(ActionEvent event) {
    	nextButtons.add("00");
    	nextButtons.add("01");
    	nextButtons.add("10");
    	
    	changeButtons();
    	stepper();
    	
    	resultTextSetter();
    }
    
	public void onButton2(ActionEvent event) {
    	nextButtons.add("01");
    	nextButtons.add("00");
    	nextButtons.add("02");
    	nextButtons.add("11");
    	
    	changeButtons();
    	
    	stepper();
    	
    	resultTextSetter();
    }

    public void onButton3(ActionEvent event) {
    	nextButtons.add("02");
    	nextButtons.add("01");
    	nextButtons.add("12");
    	
    	changeButtons();
    	
    	stepper();
    	
    	resultTextSetter();
    }

    public void onButton4(ActionEvent event) {
    	nextButtons.add("10");
    	nextButtons.add("00");
    	nextButtons.add("11");
    	nextButtons.add("20");
    	
    	changeButtons();
    	
    	stepper();
    	
    	resultTextSetter();
    }

    public void onButton5(ActionEvent event) {
    	nextButtons.add("11");
    	nextButtons.add("01");
    	nextButtons.add("10");
    	nextButtons.add("12");
    	nextButtons.add("21");
    	
    	changeButtons();
    	stepper();
    	
    	resultTextSetter();
    }

    public void onButton6(ActionEvent event) {
    	nextButtons.add("12");
    	nextButtons.add("02");
    	nextButtons.add("11");
    	nextButtons.add("22");
    	
    	changeButtons();
    	stepper();
    	
    	resultTextSetter();
    }

    public void onButton7(ActionEvent event) {
    	nextButtons.add("20");
    	nextButtons.add("10");
    	nextButtons.add("21");
    	
    	changeButtons();
    	stepper();
    	
    	resultTextSetter();
    }

    public void onButton8(ActionEvent event) {
    	nextButtons.add("21");
    	nextButtons.add("11");
    	nextButtons.add("20");
    	nextButtons.add("22");
    	
    	changeButtons();
    	stepper();
    	
    	resultTextSetter();
    }

    public void onButton9(ActionEvent event) {
    	nextButtons.add("22");
    	nextButtons.add("12");
    	nextButtons.add("21");
    	
    	changeButtons();
    	stepper();
    	
    	resultTextSetter();
    }


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

    ReflectClass m_reflectClass;
    public ReflectClass getReflectClass() { return m_reflectClass; }
    
    Map<String, Integer> arrayButtonMap = null;
    
    
	public LightsOnOffBean() {
    	m_reflectClass = new ReflectClass(this);
    	onReset(null);
    	initArrayButtonMap();
    	
    	checkSession();
    }

    private void checkSession() {
    	//System.out.println("ClientIP: " + SessionUtil.getClientIP());
    	
    	//CLog.L.log(CLog.LL_INF, (new StringBuilder(">>>>> ClientIP: " + SessionUtil.getClientIP()).append(" <<<<<").toString()));
	}
    
	private void initArrayButtonMap() {
    	arrayButtonMap = new HashMap<String, Integer>();
    	
    	arrayButtonMap.put("00", 1);
    	arrayButtonMap.put("01", 2);
    	arrayButtonMap.put("02", 3);
    	arrayButtonMap.put("10", 4);
    	arrayButtonMap.put("11", 5);
    	arrayButtonMap.put("12", 6);
    	arrayButtonMap.put("20", 7);
    	arrayButtonMap.put("21", 8);
    	arrayButtonMap.put("22", 9);
    	
		
	}
	@Override
    public String getPageName() { return "/nanoleaf/games/lightswitch/lightsOnOff.jsp"; }
    @Override
    public String getRootExpressionUsedInPage() { return "#{d.LightsOnOffBean}"; }

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