package net.sourceforge.jsurveyor;

import java.util.ArrayList;

/**
 * Class to model multipechoice questions
 * Besides what is implemented from its superclass it handles the list of possible answers for the multiplechoice question.
 * 
 * @version 1.0
 * @author Claus Methmann Christensen
 * @author Anders Spliid Hansen
 * @author Esben Iversen Hansen
 * @author Lars H Jensen
 * @author Marcus Overheu
 * @author Thomas Ramdal
 * @author Peter Thomsen
 */
public class QuestionMultipleChoice extends Question {

	/**
	 * Field choice
	 */
	private String[] choice;

	/**
	 * Constructor for QuestionMultipleChoice
	 */
	public QuestionMultipleChoice() {
		super("", true);
		choice = new String[0];
	}
	
	/**
	 * Constructor for QuestionMultipleChoice
	 * @param text String
	 * @param writeable boolean
	 */
	public QuestionMultipleChoice(String text, boolean writeable) {
		super(text, writeable);
		choice = new String[0];
	}
	
	/**
	 * Constructor for QuestionMultipleChoice.
	 * @param text String
	 * @param choice String[]
	 * @param writeable boolean
	 */
	public QuestionMultipleChoice(String text, String[] choice, boolean writeable) {
		super(text, writeable);
		this.choice = choice;
	}

	/**
	 * Constructor for QuestionMultipleChoice, use through DataAccess.
	 * @param questionID int
	 * @param text String
	 * @param writeable boolean
	 * @param required boolean
	 * @param depends boolean
	 * @param dependency ArrayList
	 * @param choice String[]
	 */
	public QuestionMultipleChoice(int questionID, String text, boolean writeable, boolean required, boolean depends, ArrayList dependency, String[] choice) {
		super(questionID, text, writeable, required, depends, dependency);
		this.choice = choice;
	}

	/**
	 * This method returns the text of the different choices.
	 * @return String[]
	 */
	public String[] getChoice() {
		return choice;
	}

	/**
	 * Sets the text of the different choices.
	 * @param choice String[]
	 * @throws IllegalAccessException
	 */
	public void setChoice(String[] choice) throws IllegalAccessException {
		if (!this.getWriteable()) {
			throw new IllegalAccessException();
		}
		this.choice = choice;
	}

	/** Adds an array of choices to the existing array of choices, starting from the nth position
	 * @param newchoice String[]
	 * @param n int
	 * @throws IllegalAccessException
	.*/
	public void addChoice(String[] newchoice, int n) throws IllegalAccessException {
		assert(n >= 0 && n <= choice.length) : "illegal value sent as argument";

		if (!this.getWriteable()) {
			throw new IllegalAccessException();
		}

		String[] newarray = new String[choice.length + newchoice.length];
		// the following arraycopy method-calls merge the old array with the 
		// new element(s), by copying to newarray
		System.arraycopy(choice, 0, newarray, 0, n);
		System.arraycopy(newchoice, 0, newarray, n, newchoice.length);
		System.arraycopy(choice, n, newarray, n + newchoice.length, choice.length - n);

		choice = newarray;
	} // end addChoice	

	/** 
	 * Removes a choice at the nth position from the existing array of choices
	 * @param n int
	 * @throws IllegalAccessException
	 */
	public void removeChoice(int n) throws IllegalAccessException {
		assert(n >= 0 && n <= choice.length) : "illegal value sent as argument";

		if (!this.getWriteable())
			throw new IllegalAccessException();

		String[] newarray = new String[choice.length - 1];
		// the following arraycopy method calls removes the nth element, and
		// copies the array onto a new array, with size = oldsize - 1.
		System.arraycopy(choice, 0, newarray, 0, n);
		System.arraycopy(choice, n + 1, newarray, n, newarray.length - n);

		choice = newarray;
	} // end removeChoice

	/**
	 * Returns the questionID prefixed with a M to represent it is a multiplechoice.
	 * Returns a string representation of the object
	 * @return String
	 */
	public String toString() {
		return "M" + super.getQuestionID();
	}
	
} // end class