/**
 * RpgClass - a DnD class constructor
 * 
 * @class Rpgclass
 * @author Martin Andersen
 **/
if (!window.RPG) {
	window.RPG = {};
}

RPG.abilityNames = [
	'Strength',
	'Dexterity',
	'Constitution',
	'Intelligence',
	'Wisdom',
	'Charisma'
];

RPG.generateAbilityScores = function() {
	var scores = [];
	var modes = {
		'4d6': function() {
			var d6s = new Dice(4, RPG.die(6));
			scores = [];
			for (var j = 0; j < 6; j++) {
				d6s.roll();
				d6s.rolls.sort();
				d6s.rolls.shift();
				d6s.rolls.total = 0;
				for (var k = 0; k < d6s.rolls.length; k++) {
					d6s.rolls.total += d6s.rolls[k];
				}
				scores.push(d6s.rolls.total);
			}
			return scores;
		},
		'3d6': function() {
			var d6s = new Dice(3, RPG.die(6));
			scores = [];
			for (var l = 0; l < 6; l++) {
				d6s.roll();
				scores.push(d6s.rolls.total);
			}
			return scores;
		},
		'Elite': function(){
			scores = [15, 14, 13, 12, 10, 8];
			return scores;
		},
		'Non-elite': function(){
			scores = [13, 12, 11, 10, 9, 8];
			return scores;
		}
	};
	var HTML = function() {
		var modeSelect = function() {
			var selEl = document.createElement("select");
			for (mode in modes) {
				if (modes.hasOwnProperty(mode)) {
					var modeOpt = new Option(mode, mode);
					selEl.appendChild(modeOpt);
				}
			}
			selEl.onChange = function() {
				generate(selEl.options[selEl.selectedIndex].value);
			};
			return selEl;
		}();
		var genBtn = function() {
			var btn = document.createElement('button');
			btn.innerHTML += "Generate";
			btn.onClick = function () {
				generate(modeSelect.options[modeSelect.selectedIndex].value);
				return false;
			}();
			return btn;
		}();
		var setStats = function() {
			var a = [];
			for (var i=0; i < RPG.abilityNames.length; i++) {
				var label = document.createElement('label')
				label.innerHTML += RPG.abilityNames[i];
				var selEl = document.createElement("select");
				for (var j=0; j < scores.length ; j++) {
					var scoreOpt = new Option (scores[j], scores[j]);
					selEl.appendChild(scoreOpt);
				}
				selEl.selectedIndex = i;
				label.appendChild(selEl);
				a.push(label);
			}
			return a;
		}();
		var genStatField = document.createElement('fieldset');
		genStatField.innerHTML += "<legend>Generate ability scores</legend>";
		genStatField.appendChild(modeSelect);
		genStatField.appendChild(genBtn);
		for (var i=0; i < setStats.length; i++) {
			genStatField.appendChild(setStats[i]);
		}
		return genStatField;
	}();
	function generate(mode) {
		if (modes[mode]) {
			scores = modes[mode]();
		}
		var setStats = function() {
			var a = [];
			for (var i=0; i < RPG.abilityNames.length; i++) {
				var label = document.createElement('label')
				label.innerHTML += RPG.abilityNames[i];
				var selEl = document.createElement("select");
				for (var j=0; j < scores.length ; j++) {
					var scoreOpt = new Option (scores[j], scores[j]);
					selEl.appendChild(scoreOpt);
				}
				selEl.selectedIndex = i;
				label.appendChild(selEl);
				a.push(label);
			}
			return a;
		}();
		//console.log(scores);
		return scores;
	}
	/*scores.total = function() {
		var t=0;
		for (var i=0; i > scores.length; i++) {
			t += scores[i];
		}
		return t;
	};
	scores.average = function() {
		var a = scores.total / scores.length ;
		return a;
	}; */

		return {
			scores: scores,
			//total: total,
			//average: average,
			HTML: HTML
		};
}();

RPG.charGenForm = function() {
	var formEl = document.createElement("form");
	formEl.appendChild(RPG.generateAbilityScores.HTML);
	return formEl;
}();

RPG.charGen = function() {
	
	var hd; // hit die, usually set by class
	var hp; // hit points
	var initiative; // initiative
	var bab; // base attack bonus
	var ac = { // armor class
		base: 10
	};
	function createAbility(score) {
		return {
			score: score,
			mod: function() {
				return Math.floor((this.score/2)-5);
			}()
		};
	}
	var levels = []; // class levels

	function setAbilities(newAbilities) {
		if (newAbilities.length !== RPG.abilityNames.length) {
			throw new Error("Abilities must be an array with " + RPG.abilityNames.length + " items");
		}
		//for i in
	}
	
	function levelUp(classname) {
		if (RPG.classes[classname]) {
			newclass = RPG.classes[classname];
			hd = newclass.hd;
			if (!levels.length) { // this is the character's first level
				
				hp = hd.sides; // full HD at first level				
			} else {
				hp += hd.roll();
				
			}
			// push the new level onto the levels array
			levels.push(newclass);
		} else {
			throw new Error("The class " + classname + " could not be found");
		}
	}

	return function(initialclass, abilities) {
		// public
		return {
			//name: name,
			abilities: abilities,
			hd: hd,
			hp: hp,
			abilities: abilities,
			levels: levels,
			levelUp: levelUp
		};
	}
}();