/**
 * @author Tim
 */
FLN.Module = function(initData){
	this.completionPolicy = initData.completionPolicy;
    this.displayMenuPrereq = initData.displayMenuPrereq;
    this.numberOfPagesInDisplay = initData.numberOfPagesInDisplay; 
    this.firstPageId = initData.firstPageId;
    this.menuPageId = initData.menuPageId;
    this.alreadyCompleteThisSession = initData.alreadyCompleteThisSession;
	this.completedInPreviousSession = initData.completedInPreviousSession;
    /*
     * Do two things:
     * 1. create an object that hashes topic Ids against topic objects for easy look up of topics.
     * 2. create an array of the topic objects
     */
    this.topicsHash = {};
    this.topics = [];
    for (var i = 0; i < initData.topics.length; i++) {
        var t = new FLN.Topic(initData.topics[i]);
        this.topicsHash[t.id] = t;
        this.topics.push(t);
    }
    
    this.pagesHash = {};
    this.pages = [];
    //cycle through all topics
    for (var i = 0; i < this.topics.length; i++) {
        for (var j = 0; j < this.topics[i].pages.length; j++) {
            var p = this.topics[i].pages[j];
            this.pages.push(p);
            this.pagesHash[p.id] = p;
        }
    }
};
FLN.Module.prototype.getPageById = function(id){
    return this.pagesHash[id];
};

FLN.Module.prototype.getPageByShortId = function(shortId){
	var arr = FLN.Utils.filterObjectArray(this.pages,{"shortId": shortId});
    return arr[0]; //quick way of doing this.
};

FLN.Module.prototype.getTopicById = function(id){
    return this.topicsHash[id];
}

/**
 * gets the topic object that this page belongs to
 * @param {String} pageId The id of the page
 * @return {Object} result The topic object
 */
FLN.Module.prototype.getTopicByPageById = function(pageId){
    return this.getTopicById(this.pagesHash[pageId].topicId);
};

FLN.Module.prototype.getCompletedPagesThatContributeToCompletionArray = function(){
    var result = FLN.Utils.filterObjectArray(this.pages, {
        status: "completed",
        contributeToCompletion: true
    });
    return result;
}

/**
 * gets if the next page should be available
 * @return {Boolean} available if the the button should display
 */
FLN.Module.prototype.getIfNextPageAvailable = function(pageId){
    var available = true;
    var p = this.getPageById(pageId);
    switch (p.nextAvailablePolicy) {
        case "never":
            available = false;
            break;
        case "always":
            available = true;
            break;
        case "completed":
            available = p.status == "completed";
            break;
        default:
            //something bad has happenned
            //Yahoo.log("Unkown FLN.currentPage.enableNextPolicy: " + p.id);
    }
    return available;
};

/**
 * gets if the previous page should be available
 * @return {Boolean} display if the the button should display
 */
FLN.Module.prototype.getIfPreviousPageAvailable = function(pageId){
    var available = true;
    switch (this.getPageById(pageId).previousAvailablePolicy) {
        case "never":
            available = false;
            break;
        case "always":
            available = true;
            break;
        default:
            //something bad has happenned
            //Yahoo.log("Unkown FLN.currentPage.enableBackPolicy: " + pageId);
    }
    return available;
};



/**
 * Gets if the menu is available from a particular page
 * @param {String} pageId
 * @return {Boolean} available
 */
FLN.Module.prototype.getIfMenuAvailable = function(pageId){
    var available = true;
    switch (this.getPageById(pageId).menuAvailablePolicy) {
        case "always":
            available = true;
            break;
        case "never":
            available = false;
            break;
        case "default":
            available = this.calcIfMenuButtonPrereqsMet();
            break;
        default:
            //something bad has happenned
            //Yahoo.log("Unkown FLN.currentPage.enableMenuButtonPolicy: " + pageId);
    }
    return available;
};

/**
 * Gets if the prerequisite pages for making the menu available have all been completed.
 * @return {Boolean} result if the prerequisite pages for making the menu available have all been completed
 */
FLN.Module.prototype.calcIfMenuButtonPrereqsMet = function(){
    var result = true;
    for (var i = 0; i < this.displayMenuPrereq.length; i++) {
        if (this.getPageById(this.displayMenuPrereq[i]).status != "completed") {
            result = false;
        }
    }
    return result;
};

FLN.Module.prototype.calcIfCompletionRequirementMet = function(){
	var moduleCompleted = true;	
	
	//the criteria for completion is determined by Module.completionPolicy.
	//if completionPolicy is "allPagesComplete" then completion is true if all relevent pages are "completed".
	if(this.completionPolicy == "allPagesComplete"){
		var pages = this.pages;
	    for (var i = 0; i < pages.length; i++) {
	        if ((pages[i].status != "completed") && (pages[i].contributeToCompletion == true)) {
	            moduleCompleted = false;
	        }
	    }
	}else if(this.completionPolicy == "allAssessmentsPassed"){
		if(FLN.AssessmentController.getIfAllAssessmentsPassed()){
			moduleCompleted = true;
		}
	}
	
    return moduleCompleted;
};
