"use strict";
this.name = "hoopy_populator";
this.author = "Paul Wilkins (updated by Eric Walch, further updated by spara, even further updated by phkb)";
this.copyright = "(C) 2009 Paul Wilkins";
this.license = "CC BY-NC-SA 3.0";

this._limitCorpStates = false;
this._minTL = 10;
this._background = 1;
this._ambienceSound;
this._hoopyNames = expandMissionText("hoopy_name").split("|");
this._hoopyName = this._hoopyNames[Math.floor(Math.random() * this._hoopyNames.length)];

this._hoopyConfig = {
    Name: this.name,
    Display: expandMissionText("hoopy_casino_config"),
    Alias: expandMissionText("hoopy_casino_title", {name: this._hoopyName}),
    Alive: "_hoopyConfig",
	Bool: {
		B0: { Name: "_limitCorpStates", Def: true, Desc: expandMissionText("hoopy_config_bool") },
		Info: expandMissionText("hoopy_config_bool_info")
	},
	SInt: {
		S0: { Name: "_minTL", Def: 10, Min: 0, Max: 14, Desc: expandMissionText("hoopy_config_sint") },
		Info: expandMissionText("hoopy_config_sint_info")
	},
};

this.startUp = function() {
	if (missionVariables.Hoopy_CorpStates) this._limitCorpStates = (missionVariables.Hoopy_CorpStates == "Y" ? true : false);
	if (missionVariables.Hoopy_MinTL) this._minTL = parseInt(missionVariables.Hoopy_MinTL);
}

//add interface
this.$addMainInterface = function () {
	var casinos = system.shipsWithRole("casinoship");
	if (casinos.length > 0) {
		for (var i = 0; i < casinos.length; i++) {
			casinos[i].setInterface("hoopy-casino", {
				title: expandMissionText("hoopy_casino_title", {name: this._hoopyName} ),
				category: expandDescription("[interfaces-category-organisations]"),
				summary: expandMissionText("hoopy_casino_summary"),
				callback: this.$displayMenu.bind(this)
			});
		}
	}
}

this.startUpComplete = function () {
    // register our settings, if Lib_Config is present
    if (worldScripts.Lib_Config) {
        worldScripts.Lib_Config._registerSet(this._hoopyConfig);
    }

	var w = worldScripts.XenonUI;
	if (w) w.$addMissionScreenException("hoopy_casino");
	w = worldScripts.XenonReduxUI;
	if (w) w.$addMissionScreenException("hoopy_casino");
	this.$addMainInterface();
}

this.shipExitedWitchspace = function() {
	this.$addMainInterface();
}

//add casino to system
this.systemWillPopulate = function () {
	// add in high tech level systems, but not in comunist systems. But, always in systems famous for their casinos.
	var check = this._limitCorpStates ? (system.techLevel >= this._minTL && system.government == 7) : (system.techLevel >= this._minTL && system.scrambledPseudoRandomNumber() > 0.55 && system.government != 4);

	if (check || system.info.description.indexOf("hoopy casinos") > -1) {
		//no main station, no casinos.
		if (system.mainStation) {
			var coords = system.mainStation.position.subtract(system.mainStation.position.direction().multiply(50000));
			system.setPopulator("hoopy_casino", {
				callback: function (pos) {
					system.addShips("casinoship", 1, pos);
				}.bind(this),
				location: "COORDINATES",
				coordinates: coords
			})
		}
	}
}

this.playerWillSaveGame = function() {
	missionVariables.Hoopy_CorpStates = (this._limitCorpStates ? "Y" : "N");
	missionVariables.Hoopy_MinTL = this._milTL;
}

this.missionScreenEnded = function() {
	if (this._timer && this._timer.isRunning) this._timer.stop();
}

this.$displayMenu = function() {
	this._timer = new Timer(this, this.$changeBackground.bind(this), 0.25, 1.5);
	mission.runScreen({
		screenID: "hoopy_casino",
		title: expandMissionText("hoopy_casino_title", {name: this._hoopyName}),
		message: expandMissionText("hoopy_casino_message", {name: this._hoopyName}),
		background: { name: "hoopy_casino_front.png", height: 546 },
		choicesKey: "hoopy_casino_menu",
		allowInterrupt: true,
		exitScreen: "GUI_SCREEN_INTERFACES"
	},
		this.$processChoice.bind(this));
}

this.$processChoice = function(choice) {
	this._timer.stop();
	this._hoopyName = this._hoopyNames[Math.floor(Math.random() * this._hoopyNames.length)];

	switch (choice) {
		case "1_HOOPS": 
			worldScripts.hoopy_casino.$startGambling();
			return;
		case "2_POKER":
			worldScripts.hoopy_poker.$intro();
			return;
		case "3_BLACKJACK":
			worldScripts.hoopy_blackjack.$intro();
			return;
		case "4_HOLDEM":
			worldScripts.hoopy_holdem.$intro();
			return;
		case "5_SMUGGLERS":
			worldScripts.hoopy_dice.$intro();
			return;
	}
}

this.$changeBackground = function $changeBackground() {
	if (this._background == 1) this._background = 2; else this._background = 1;
	setScreenBackground({ name: "hoopy_casino_front" + this._background + ".png", height: 546 })
}
