﻿// this script assumes that cardSides and cardDeck
// are defined elsewhere

function FlashCards(cards)
{
	this.cardDesc = cards.getCardDesc();
	this.cardDeck = cards.getCardDeck();
	this.currentCard=null;
	this.cardsToUse=[];
	this.usedCards=[];
	this.usedCount=0;
	this.reuseCards = false;
	this.lessonsToUse = [];
	this.retestDeck=[];
	
}

FlashCards.prototype.clearCardData = function()
{
	this.currentCard=null;
	this.cardsToUse=[];
	this.usedCards=[];
	this.usedCount=0;
	this.reuseCards=false;
	this.lessonsToUse=[];
}

FlashCards.prototype.setLessonsToUse = function(ltu)
{
	this.lessonsToUse = ltu;
}

FlashCards.prototype.getLessonsToUse = function()
{
	return this.lessonsToUse;
}

FlashCards.prototype.setReuseCards = function(rc)
{
	this.reuseCards = rc;
}

FlashCards.prototype.getReuseCards = function()
{
	return this.reuseCards;
}

FlashCards.prototype.getCardsToUseArray = function()
{
	return this.cardsToUse;
}

FlashCards.prototype.getCardsToUseCount = function()
{
	return this.cardsToUse.length;
}

FlashCards.prototype.getCardDeck = function()
{
	return this.cardDeck;
}

FlashCards.prototype.getCardDesc = function()
{
	return this.cardDesc;
}

FlashCards.prototype.isLastCard = function()
{
	var toReturn = (this.usedCount == this.cardsToUse.length);
	return toReturn;
}

FlashCards.prototype.getUsedCount = function()
{
	return this.usedCount;
}

FlashCards.prototype.getCurrentCard = function()
{
	return this.currentCard;
}

/**
 * This method assume that the lessonsToUse array
 * has been initialized so that if lessonCards[i] is
 * to be included in this lesson then lessonsToUse[i]
 * will be true.
 */
FlashCards.prototype.constructCardsToUse = function()
{
	for(var i=0; i<this.lessonsToUse.length; i++)
	{
		if(this.lessonsToUse[i])
		{
			var lessonDeck = this.cardDeck.getLessonDeck(i);
			for(var j=0; j<lessonDeck.getCardCount(); j++)
			{
				// JavaScript automatically increases size
				this.cardsToUse[this.cardsToUse.length] = lessonDeck.getCard(j);
				this.usedCards[this.cardsToUse.length] = false;
			}
		}
	}
}


/**
 * This is called to choose another card.  It takes into account
 * whether there is replacement or not.
 */
FlashCards.prototype.chooseNext = function()
{
	var randomIndex = -1;

	// first, find a random card
	// if we are not reusing cards, then we'll have to
	// search until we find one
	// if we are reusing cards, we just find a random one
	if(!this.reuseCards)
	{

		if(this.usedCount == this.cardsToUse.length)
		{
			return false;
		}
		else
		{

			var done = false;
			// attempt to get a random hit five times
			// if all of those have been used, then just get
			// the next valid one
			for(var i=0; i<5 && !done; i++)
			{
				var attemptIndex = getRandomInteger(0,this.cardsToUse.length);
				if(!this.usedCards[attemptIndex])
				{
					randomIndex = attemptIndex;
					done = true;
				}
			}
			if(randomIndex == -1)
			{
				for(var i=0; i<this.cardsToUse.length && !done; i++)
				{
					if(!this.usedCards[i])
					{
						randomIndex = i;
						done = true;
					}
				}
			}
		}

		this.usedCards[randomIndex] = true;
	}
	else
	{
		randomIndex = getRandomInteger(0,this.cardsToUse.length);
	}

	this.usedCount++;
	this.currentCard = this.cardsToUse[randomIndex];
	/*
	for(var i=0;i<this.cardsToUse[randomIndex].length;i++)
	{
		this.currentCard[i] = this.cardsToUse[randomIndex][i];
	}
	*/
	
	return true;
}

/**
 * Resets information regarding which cards in the cardsToUse
 * array have been used.  This is used when there is no
 * replacement and all of the cards have been used.
 */
FlashCards.prototype.resetUsedCards = function()
{
	for(var i=0; i<this.usedCards.length; i++)
	{
		this.usedCards[i] = false;
	}
	this.usedCount = 0;
}

FlashCards.prototype.addCurrentCardToRetestDeck = function()
{
	this.retestDeck[this.retestDeck.length] = this.currentCard;
}

FlashCards.prototype.useRetestDeck = function()
{
	for(var i=0; i<this.retestDeck.length; i++)
	{
		this.cardsToUse[i] = this.retestDeck[i];
		this.usedCards[i] = false;
	}
	
	this.retestDeck = [];
}

FlashCards.prototype.getRetestCardCount = function()
{
	return this.retestDeck.length;
}

/**
 * Utility: returns an integer >= lower and < upper
 */
function getRandomInteger(lower,upper)
{
	if(lower == upper)
		return lower;
	if(lower > upper)
	{
		var temp = upper;
		upper = lower;
		lower = temp;
	}

	var a = Math.floor(Math.random() * (upper-lower));
	a += lower;

	// account for the very very rare case where Math.random() returned 1
	if(a == upper)
		a = upper-1;

	return a;
}


function CardDescription() {}
CardDescription.prototype.getNumberOfSides = function(){}
CardDescription.prototype.getSideDesc = function(i) {}
CardDescription.prototype.getSideQuestionStyle = function(i) {}
CardDescription.prototype.getSideAnswerStyle = function(i) {}

function CardDeck() {}
CardDeck.prototype.getLessonCount = function() {}
CardDeck.prototype.getLessonDesc = function(i) {}
CardDeck.prototype.getLessonDeck = function(i) {}


function LessonDeck() {}
LessonDeck.prototype.getCardCount = function() {}
LessonDeck.prototype.getCard = function(i) {}

function Card() {}
Card.prototype.getSide = function(i) {}

function Cards() {}
Cards.prototype.getCardDeck = function() {}
Cards.prototype.getCardDesc = function() {}