"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];

//add interface
this.startUpComplete = this.shipExitedWitchspace = function () {
	var casinos = system.shipsWithRole("casinoship");
	if (casinos.length > 0) {
		for (var i = 0; i < casinos.length; i++) {
			casinos[i].setInterface("hoopy-dice", {
				title: expandMissionText("hoopy-smugglers-title"),
				category: expandMissionText("hoopy-gambling-category"),
				summary: expandMissionText("hoopy-dice-summary"),
				callback: this.$intro.bind(this)
			});
		}
	}
}

//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-smugglers-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);
		}
	);
}

//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-smugglers-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-smugglers-highsame");
			else
				options["3_HIGH"] = expandMissionText("hoopy-smugglers-high");
			if (sum <= 7)
				options["4_LOW_SAME"] = expandMissionText("hoopy-smugglers-lowsame");
			else
				options["5_LOW"] = expandMissionText("hoopy-smugglers-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;
				}
			}
		);
	}
	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;
				}
			}
		);
	}
}

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;
			}
		}
	);
}
