package bwyap.familyfeud.game.play.state;

import bwyap.familyfeud.game.InvalidDataException;
import bwyap.familyfeud.game.QuestionSet;
import bwyap.familyfeud.game.play.FFPlayState;
import bwyap.familyfeud.game.play.FFPlayStateType;

/**
 * This state handles when the two families are facing off 
 * to see which one gets to choose to play
 * @author bwyap
 *
 */
public class StateFaceOff extends FFPlayState {

	public static final int ACTION_OPENANSWER = 0x0;
	public static final int ACTION_CHOOSEFAMILY = 0x1;
	
	private QuestionSet questions;
	private int selectedIndex;
	private int selectedFamilyIndex = -1;
	
	protected StateFaceOff(QuestionSet questions) {
		super(FFPlayStateType.FACE_OFF);
		this.questions = questions;
	}

	@Override
	public void initState(Object data) {
		if (data instanceof Integer) {
			this.selectedIndex = (Integer) data;
		}
		else throw new InvalidDataException("StateFaceOff: Integer expected from intialization data");
	}

	@Override
	public void cleanupState() {
		// Set the selected question and selected family as the data for the next state
		data = new Object[]{questions.getQuestion(selectedIndex), selectedFamilyIndex};
	}

	@Override
	public boolean executeAction(int action, Object[] data) {
		switch(action) {
		case ACTION_OPENANSWER:
			// Reveal an answer
			if (data[1] instanceof Integer) {
				openAnswer((Integer) data[1]);
			}
			else throw new InvalidDataException("Expecting a {*, Integer} when using action ACTION_OPENANSWER");
			break;
		case ACTION_CHOOSEFAMILY:
			// Choose the family that won the face off
			if (data[1] instanceof Integer) {
				selectFamily((Integer) data[1]);
			}
			else throw new InvalidDataException("Expecting a {*, Integer} when using action ACTION_CHOOSEFAMILY");
			break;
		default: 
			throw new RuntimeException("Invalid action");
		}
		return false;
	}
	
	@Override
	public boolean canAdvance() {
		// Must selected a family before advancing to the next state
		return selectedFamilyIndex >= 0;
	}
	
	/**
	 * Reveal and answer
	 * @param index
	 */
	private void openAnswer(int index) {
		questions.getQuestion(selectedIndex).getAnswers().get(index).setReveal(true);
	}
	
	/**
	 * Select the family that won the face off
	 * @param index
	 */
	private void selectFamily(int index) {
		selectedFamilyIndex = index;
	}
	
}

@@ -226,16 +226,53 @@
 ateType;
+%0Aimport bwyap.utility.logging.Logger;
 %0A%0A/**%0A *
@@ -495,16 +495,62 @@
  = 0x0;%0A
+%09public static final int ACTION_STRIKE = 0x1;%0A
 %09public 
@@ -590,17 +590,17 @@
 ILY = 0x
-1
+2
 ;%0A%09%0A%09pri
@@ -1848,16 +1848,91 @@
 %09break;%0A
+%09%09case ACTION_STRIKE:%0A%09%09%09// Incorrect answer given%0A%09%09%09strike();%0A%09%09%09break;%0A%0A
 %09%09defaul
@@ -2155,24 +2155,133 @@
 ;%0A%09%7D%0A%09%0A%09/**%0A
+%09 * Answer not on board%0A%09 */%0A%09private void strike() %7B%0A%09%09Logger.log(%22Strike: answer not on board%22);%0A%09%7D%0A%09%0A%09/**%0A
 %09 * Reveal a
@@ -2281,17 +2281,16 @@
 eveal an
-d
  answer%0A
@@ -2429,16 +2429,114 @@
 (true);%0A
+%09%09Logger.log(%22Revealed answer: %22 + questions.getQuestion(selectedIndex).getAnswers().get(index));%0A
 %09%7D%0A%09%0A%09/*

Add missing log message