"use strict";
this.name = "hoopy_dice";
this.author = "spara";
this.copyright = "2014 spara";
this.license = "CC BY-NC-SA 3.0";

//game track with bet multipliers
this.$track = [0, 0, 0, 4, 0, 8, 0, 17, 0, 35];

//helper function for tabulating text on screen
this.$tabulate = function (line, width) {
	var hairSpace = String.fromCharCode(31);
	var space = defaultFont.measureString(" ");
	while (defaultFont.measureString(line) < width - space)
		line += " ";
	while (defaultFont.measureString(line) < width)
		line += hairSpace;
	return line;
}

//intro screen
this.$intro = function () {
	var bets = [10, 50, 100];
	//build and format the payoff table for screen
	var payoffMessage = expandMissionText("hoopy-dice-correct");
	for (var i = 0; i < 3; i++) {
		payoffMessage = this.$tabulate(payoffMessage, 5 * (i + 1));
		payoffMessage += formatCredits(bets[i], true, true);
	}
	payoffMessage = "\n\n" + payoffMessage + "\n\n";
	for (i = this.$track.length - 1; i > 0; i--) {
		if (this.$track[i] !== 0) {
			var line = i + 1;
			for (var j = 0; j < 3; j++) {
				line = this.$tabulate(line, 5 * (j + 1));
				line += this.$track[i] * bets[j];
			}
			payoffMessage += line + "\n";
		}
	}

	//format player credits for screen
	var credits = expandMissionText("hoopy-credits", { amount: formatCredits(player.credits, true, true) });

	var options = {
		"1_EXIT": expandMissionText("hoopy-exit")
	};
	if (player.credits >= 10)
		options["2_10"] = expandMissionText("hoopy-casino-bet", { amount: formatCredits(10, true, true) });
	else credits += expandMissionText("hoopy-no-credit");
	if (player.credits >= 50)
		options["3_50"] = expandMissionText("hoopy-casino-bet", { amount: formatCredits(50, true, true) });
	if (player.credits >= 100)
		options["4_100"] = expandMissionText("hoopy-casino-bet", { amount: formatCredits(100, true, true) });
	mission.runScreen({
		screenID: "hoopy_casino",
		titleKey: "hoopy-dice-title",
		message: expandMissionText("hoopy-dice-intro-story") + payoffMessage + credits,
		choices: options,
		background: "hoopy_dice_bg-intro.png"
	},
		function (choice) {
			if (choice === "2_10") this.$playGame(10);
			else if (choice === "3_50") this.$playGame(50);
			else if (choice === "4_100") this.$playGame(100);
			else if (choice == "1_EXIT") worldScripts.hoopy_populator.$displayMenu();
		}.bind(this)
	);
}

//da game
this.$playGame = function (bet, prevSum, round, track, select) {
	//init the game
	if (typeof round == "undefined") {
		var round = 0;
		track = "";
		player.credits -= bet;
	}

	//roll the dice
	var die1 = Math.ceil(6 * Math.random());
	var die2 = Math.ceil(6 * Math.random());
	var sum = die1 + die2;

	//check the guess
	var pass = true;
	if (typeof prevSum != "undefined") {
		if (select === "high_same") {
			if (sum < prevSum)
				pass = false;
		}
		else if (select === "high") {
			if (sum <= prevSum)
				pass = false;
		}
		else if (select === "low_same") {
			if (sum > prevSum)
				pass = false;
		}
		else if (select === "low") {
			if (sum >= prevSum)
				pass = false;
		}
	}

	//debug	
	//pass = true; 

	//build the track display
	track += sum;
	if (round <= this.$track.length - 1)
		track += " > ";
	var messageTrack = track;
	for (var i = round; i < this.$track.length; i++) {
		if (this.$track[i] !== 0)
			messageTrack += formatCredits(bet * this.$track[i], false, true);
		else messageTrack += "?";
		if (i < this.$track.length - 1)
			messageTrack += " > ";
	}
	//correct guess
	if (pass) {
		var options = {};
		//cash out
		if (round > 0 && this.$track[round - 1] !== 0) {
			var cash_out = formatCredits(bet * this.$track[round - 1], false, true);
			options["1_CASH"] = expandMissionText("hoopy-dice-cashout", { amount: cash_out });
			var message = expandMissionText("hoopy-dice-track-cash");
			var background = "hoopy_dice_bg-cash.png";
		}
		else {
			var message = expandMissionText("hoopy-dice-track-basic");
			var background = "hoopy_dice_bg-basic.png";
		}
		//guess choices
		if (round !== this.$track.length) {
			if (sum >= 7)
				options["2_HIGH_SAME"] = expandMissionText("hoopy-dice-highsame");
			else
				options["3_HIGH"] = expandMissionText("hoopy-dice-high");
			if (sum <= 7)
				options["4_LOW_SAME"] = expandMissionText("hoopy-dice-lowsame");
			else
				options["5_LOW"] = expandMissionText("hoopy-dice-low");
		}
		else
			var message = expandMissionText("hoopy-dice-track-final");
		mission.runScreen({
			screenID: "hoopy_casino",
			titleKey: "hoopy-dice-title",
			overlay: "hoopy_dice_" + die1 + "-" + die2 + ".png",
			message: messageTrack + message,
			choices: options,
			background: background
		},
			function (choice) {
				if (choice === "1_CASH") {
					this.$cashOut(bet, round);
					return;
				}
				else if (choice === "2_HIGH_SAME") {
					this.$playGame(bet, sum, round + 1, track, "high_same");
					return;
				}
				else if (choice === "3_HIGH") {
					this.$playGame(bet, sum, round + 1, track, "high");
					return;
				}
				else if (choice === "4_LOW_SAME") {
					this.$playGame(bet, sum, round + 1, track, "low_same");
					return;
				}
				else if (choice === "5_LOW") {
					this.$playGame(bet, sum, round + 1, track, "low");
					return;
				}
			}.bind(this)
		);
	} else {
		var options = {
			"1_EXIT": expandMissionText("hoopy-exit"),
			"2_AGAIN": expandMissionText("hoopy-play-again")
		};
		mission.runScreen({
			screenID: "hoopy_casino",
			titleKey: "hoopy-dice-title",
			overlay: "hoopy_dice_" + die1 + "-" + die2 + ".png",
			message: messageTrack + expandMissionText("hoopy-dice-track-fail"),
			choices: options,
			background: "hoopy_dice_bg-fail.png"
		},
			function (choice) {
				if (choice === "2_AGAIN") {
					this.$intro();
					return;
				}
				if (choice === "1_EXIT") {
					worldScripts.hoopy_populator.$displayMenu();
					return;
				}
			}.bind(this)
		);
	}
}

this.$cashOut = function (bet, round) {
	var win = this.$track[round - 1] * bet
	player.credits += win;
	var options = {
		"1_EXIT": expandMissionText("hoopy-exit"),
		"2_AGAIN": expandMissionText("hoopy-play-again")
	};
	mission.runScreen({
		screenID: "hoopy_casino",
		titleKey: "hoopy-dice-title",
		message: formatCredits(win, false, true) + expandMissionText("hoopy-dice-track-win"),
		choices: options,
		background: "hoopy_dice_bg-win.png"
	},
		function (choice) {
			if (choice === "2_AGAIN") {
				this.$intro();
				return;
			}
			if (choice === "1_EXIT") {
				worldScripts.hoopy_populator.$displayMenu();
				return;
			}
		}.bind(this)
	);
}
