package net.sourceforge.jsurveyor;
import java.util.ArrayList;

/**
 * Class to model pages in questionnaires.
 * It handles a list of the specific order of the questions on the page. 
 * 
 * @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 QuestionPage {
	
	/**
	 * Field refQuestions
	 */
	private ArrayList refQuestions = new ArrayList();;
	/**
	 * Field questionPageID
	 */
	private int questionPageID;
	
	/**
	 * Constructor for QuestionPage
	 */
	public QuestionPage() {	
	}
	
	/**
	 * Constructor for QuestionPage, use through DataAccess.
	 * @param questionPageID int
	 * @param questions ArrayList
	 */
	public QuestionPage(int questionPageID, ArrayList questions) {
		this.questionPageID = questionPageID;
		this.refQuestions = questions;
	}
	
	/**
	 * Add a question to the page.
	 * @pre the question is not already added.
	 * @param question Question
	 */
	public void addQuestion(Question question) {
		assert !(
				isQuestionAdded(question)) : "The question is already added";
		refQuestions.add(question.toString());
	}
	
	/**
	 * Add a question to the page at a specific position.
	 * @pre the question is not already added.
	 * @param question Question
	 * @param position int
	 */
	public void addQuestionAtPosition(Question question, int position) {
		assert !(
				isQuestionAdded(question)) : "The question is already added";
		refQuestions.add(position, question.toString());
	}

	/**
	 * Remove a question from the page.
	 * @pre the question is on the page.
	 * @param question Question
	 */
	public void removeQuestion(Question question) {
		assert isQuestionAdded(question) : "The question is not added";
		refQuestions.remove(question.toString());
	}
	
	/**
	 * Checks if a question is on the page.
	 * @param question Question
	 * @return boolean
	 */
	private boolean isQuestionAdded(Question question) {
		return refQuestions.indexOf(question.toString()) != -1;
	}
	
	/**
	 * Returns an ArrayList with toString representations of the added Questions.
	 * @return ArrayList
	 */
	public ArrayList getRefQuestions() {
		return refQuestions;
	}
	
	/**
	 * Sets the ID of the page.
	 * @param questionPageID int
	 */
	public void setQuestionPageID(int questionPageID) {
		this.questionPageID = questionPageID;
	}
	
	/**
	 * Returns the ID of the page.
	 * @return int
	 */
	public int getQuestionPageID() {
		return questionPageID;
	}

	/**
	 * Returns a string representation of the object.
	 * @return String
	 */
	public String toString() {
		return "P" + questionPageID;
	}
	
}
