/*
 * Copyright (c) 2005 Absolutely Training Limited
 * 
 * Created on 08-Dec-2005
 */
/**
 * Translation of the Java ResultCategoryGroup class into JavaScript
 * 
 * @author paulb
 */
 
function ResultCategoryGroup( props ) {

	this.resultCategories = [];
    this.maxScore = 100;
	
	for ( var p in props ) {
		this[p] = props[p];
	}
}

ResultCategoryGroup.FAIL_RATING = "Fail";
ResultCategoryGroup.DEFAULT_PASS_MARK = 70;

ResultCategoryGroup.prototype.getCategoryForScore = function(score) {

    if (score > this.maxScore) {
        throw new Error("Score " + score + " exceeds maximum score of " + this.maxScore);
    }

    for (var i = 0; i < this.resultCategories.length - 1; i++) {
        var low = this.resultCategories[i].getLowScore();
        var high = this.resultCategories[i + 1].getLowScore();
        if (score >= low && score < high) {
            return this.resultCategories[i];
        }
    }
    return this.resultCategories[this.resultCategories.length - 1];
};
    
ResultCategoryGroup.prototype.getAssessmentForScore = function(score) {

    if (score > this.maxScore) {
        throw new Error("Score " + score + " exceeds maximum score of " + this.maxScore);
    }
    for (var i = this.resultCategories.length - 1; i >= 0; i--) {
        if (score >= this.resultCategories[i].getLowScore()) {
            return this.resultCategories[i].getRatingLabel();
        }
    }
    return ResultCategoryGroup.FAIL_RATING;
};

ResultCategoryGroup.prototype.getPassMark = function() {
    for (var i = 0; i < this.resultCategories.length; i++) {
    	var category = this.resultCategories[i];
        if ( category.isPassMark() ) {
            return category.getLowScore();
        }
    }
    
    return ResultCategoryGroup.DEFAULT_PASS_MARK;
};

ResultCategoryGroup.prototype.isPassMark = function(score) {
    return score >= this.getPassMark();
};

ResultCategoryGroup.prototype.toString = function() {
    var sb = "";

    for (var i = 0; i < this.resultCategories.length; i++) {
        sb += (i > 0) ? "|" : "";
        sb += this.resultCategories[i].toString();
    }
    return sb;
};

ResultCategoryGroup.prototype.getResultCategoryGroupString = function() {
    return this.toString();
};

ResultCategoryGroup.prototype.getResultCategories = function() {
    return this.resultCategories;
};


