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

this._limitCorpStates = false;
this._minTL = 10;

this._hoopyConfig = {
    Name: this.name,
    Display: expandMissionText("hoopy_casino_config"),
    Alias: expandMissionText("hoopy_casino_title"),
    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);
}

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

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