package de.nanoleaf.games.tictactoe;

import java.io.Serializable;

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.TicTacToeBean}")

public class TicTacToeBean extends PageBean implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	int[] playerArray = new int[10];
	
	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; }
    
    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; }
    
    private void changeColor() {
    	
    	if (getLightPlayerNext().equals(getLightPlayer1())) {
    		setLightPlayerNext(getLightPlayer2());
    		setNextPlayer(2);
    	} else {
    		setLightPlayerNext(getLightPlayer1());
    		setNextPlayer(1);
    	}
    }
    
    public void onButton1(ActionEvent event) {
    	if (getEnabledButton1() == true) {
    		setColorButton1(getLightPlayerNext());
    		setEnabledButton1(false);
    		playerArray[1] = getNextPlayer();
        	changeColor();
    	}
    	
    	
    	resultTextSetter();
    }
    
	
	public void onButton2(ActionEvent event) {
		if (getEnabledButton2() == true) {
	    	setColorButton2(getLightPlayerNext());
	    	setEnabledButton2(false);
	    	playerArray[2] = getNextPlayer();
	    	changeColor();
		}
    	
    	resultTextSetter();
    }

    public void onButton3(ActionEvent event) {
    	if (getEnabledButton3() == true) {
	    	setColorButton3(getLightPlayerNext());
	    	setEnabledButton3(false);
	    	playerArray[3] = getNextPlayer();
	    	changeColor();
    	}
    	
    	resultTextSetter();
    }

    public void onButton4(ActionEvent event) {
    	if (getEnabledButton4() == true) {
	    	setColorButton4(getLightPlayerNext());
	    	setEnabledButton4(false);
	    	playerArray[4] = getNextPlayer();
	    	changeColor();
    	}
    	
    	resultTextSetter();
    }

    public void onButton5(ActionEvent event) {
    	if (getEnabledButton5() == true) {
	    	setColorButton5(getLightPlayerNext());
	    	setEnabledButton5(false);
	    	playerArray[5] = getNextPlayer();
	    	changeColor();
    	}
    	
    	resultTextSetter();
    }

    public void onButton6(ActionEvent event) {
    	if (getEnabledButton6() == true) {
	    	setColorButton6(getLightPlayerNext());
	    	setEnabledButton6(false);
	    	playerArray[6] = getNextPlayer();
	    	changeColor();
    	}
    	
    	resultTextSetter();
    }

    public void onButton7(ActionEvent event) {
    	if (getEnabledButton7() == true) {
	    	setColorButton7(getLightPlayerNext());
	    	setEnabledButton7(false);
	    	playerArray[7] = getNextPlayer();
	    	changeColor();
    	}
    	
    	resultTextSetter();
    }

    public void onButton8(ActionEvent event) {
    	if (getEnabledButton8() == true) {
	    	setColorButton8(getLightPlayerNext());
	    	setEnabledButton8(false);
	    	playerArray[8] = getNextPlayer();
	    	changeColor();
    	}
    	
    	resultTextSetter();
    }

    public void onButton9(ActionEvent event) {
    	if (getEnabledButton9() == true) {
	    	setColorButton9(getLightPlayerNext());
	    	setEnabledButton9(false);
	    	playerArray[9] = getNextPlayer();
	    	changeColor();
    	}
    	
    	resultTextSetter();
    }
    
    public String m_lightPlayer1 = "#66FF00";
    public String getLightPlayer1() { return m_lightPlayer1; }
    
    public String m_lightPlayer2 = "#FF0000";
    public String getLightPlayer2() { return m_lightPlayer2; }
    
    public String m_lightPlayerNext = "#66FF00";
    public void setLightPlayerNext(String lightPlayerNext) { m_lightPlayerNext = lightPlayerNext; }
	public String getLightPlayerNext() { return m_lightPlayerNext; }
	
	public int m_nextPlayer = 1;
    public int getNextPlayer() { return m_nextPlayer; }
	public void setNextPlayer(int nextPlayer) { m_nextPlayer = nextPlayer; }

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

    public void onReset(ActionEvent event) {
    	    	
    	enabledAllButtons(true);
    	
    	playerArray = new int[10];
    	for (int i = 0; i < 10; i++) {
    		playerArray[i] = 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;
    	
    	setNextPlayer(1);
    	setLightPlayerNext(getLightPlayer1());
    	
    	resultTextSetter();
    }
    
    private void enabledAllButtons(boolean enabled) {
		for (int i = 1; i <= 9; i++) {
			getReflectClass().setMethod("setEnabledButton" + i, enabled);
		}
	}
    
    CheckTicTacToe m_checkTicTacToe = new CheckTicTacToe();
	
    public CheckTicTacToe getCheckTicTacToe() {
		return m_checkTicTacToe;
	}
	private boolean checkFinish() {
		boolean finish = false;
		
		finish = getCheckTicTacToe().checkFinal(playerArray);

		return finish;
	}
    
    ReflectClass m_reflectClass;
    public ReflectClass getReflectClass() { return m_reflectClass; }
	
	private void resultTextSetter() {
		
		if (checkFinish()) {
			m_resultText = " GEWONNEN. :-)";
			enabledAllButtons(false);
			
		} else {
			m_resultText = "Player "+ getNextPlayer();
		}
	}
    // ------------------------------------------------------------------------
    // inner classes
    // ------------------------------------------------------------------------
    
    /* Listener to the user of the page bean. */
    public interface IListener extends Serializable
    {
    }
    
    // ------------------------------------------------------------------------
    // members
    // ------------------------------------------------------------------------
    
    private IListener m_listener;
    
    // ------------------------------------------------------------------------
    // constructors & initialization
    // ------------------------------------------------------------------------

    public TicTacToeBean() {
    	m_reflectClass = new ReflectClass(this);
    	onReset(null);
    }

    public TicTacToeBean(String lightPlayer1, String lightPlayer2) {
    	this.m_lightPlayer1 = lightPlayer1;
    	this.m_lightPlayer2 = lightPlayer2;
    	
    	m_reflectClass = new ReflectClass(this);
    	onReset(null);
    }
    
    
    public String getPageName() { return "/nanoleaf/games/tictactoe/tictactoe.xml"; }
    public String getRootExpressionUsedInPage() { return "#{d.TicTacToeBean}"; }

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