"use strict";
this.name = "Spawn-sothis";
this.author = "phkb";
this.copyright = "This script is hereby placed in the public domain.";
this.version = "1.1";
this.description = "Sothis Trade Center main script";

this._config = {
    marketType: 3, // 0 = no market; 1 = original custom market; 2 = similar to main station; 3 = oolite-business goods
    minTL: 8,
	maxTL: 14,
	minEco: 0,
	maxEco: 7,
	minGov: 7,
	maxGov: 7,
	minProd: 0,
	maxProd: 60000,
};

this._STCConfig = {
    Name: this.name,
    Display: expandDescription("[STC_config_display]"),
    Alias: expandDescription("[STC_config_alias]"),
    Alive: "_STCConfig",
    SInt: {
        S0: { Name: "_config.marketType", Def: 2, Min: 0, Max: 3, Desc: expandDescription("[STC_markettype]") },
        S1: { Name: "_config.minTL", Def: 8, Min: 0, Max: 14, Desc: expandDescription("[STC_minTL]") },
        S2: { Name: "_config.maxTL", Def: 14, Min: 0, Max: 14, Desc: expandDescription("[STC_maxTL]") },
        S3: { Name: "_config.minEco", Def: 0, Min: 0, Max: 7, Desc: expandDescription("[STC_minEco]") },
        S4: { Name: "_config.maxEco", Def: 7, Min: 0, Max: 7, Desc: expandDescription("[STC_maxEco]") },
        S5: { Name: "_config.minGov", Def: 7, Min: 0, Max: 7, Desc: expandDescription("[STC_minGov]") },
        S6: { Name: "_config.maxGov", Def: 7, Min: 0, Max: 7, Desc: expandDescription("[STC_maxGov]") },
        S7: { Name: "_config.minProd", Def: 0, Min: 0, Max: 60000, Desc: expandDescription("[STC_minProd]") },
        S8: { Name: "_config.maxProd", Def: 60000, Min: 0, Max: 60000, Desc: expandDescription("[STC_maxProd]") },
        Info: expandDescription("[STC_config_sint_info]")
    },
};

//-------------------------------------------------------------------------------------------------------------
this.startUp = function () {
    if (missionVariables.Sothis_Config) this._config = JSON.parse(missionVariables.Sothis_Config);
	// make sure everything is a number
	for (var prop in this._config) {
		this._config[prop] = parseInt(this._config[prop]);
	}

    worldScripts.XenonUI.$addMissionScreenException("sothis");
	if (!worldScripts.CargoTypeExtension) { //check for new cargoes oxp
		this.$new_cargoes = false;
		delete this.playerBoughtEquipment;
		delete this.shipLaunchedFromStation;
	} else {
		this.$tc_eqTradenet = false;
		this.$new_cargoes = true;
	}
	this.$showWelcome = false;
}

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

//-------------------------------------------------------------------------------------------------------------
this.playerWillSaveGame = function() {
    if (worldScripts.Lib_Config) {
	    missionVariables.Sothis_Config = JSON.stringify(this._config);
	}
}

//-------------------------------------------------------------------------------------------------------------
this.systemWillPopulate = function () {
	this.$addSothis();
}

//-------------------------------------------------------------------------------------------------------------
this.$addSothis = function () { 
	//add sothis to corporate states
	//Ask spawn permission from Station Validator oxp
	if (worldScripts.station_validator && (worldScripts.station_validator.$deaths("sothis").length !== 0))
		return;

	if ((system.government >= this._config.minGov && system.government <= this._config.maxGov) &&
		(system.techLevel >= this._config.minTL && system.techLevel <= this._config.maxTL) && 
		(system.economy >= this._config.minEco && system.economy <= this._config.maxEco) && 
		(system.productivity >= this._config.minProd && system.productivity <= this._config.maxProd) && 
		system.mainStation) {
		var a = system.mainStation.position.x;
		var b = system.mainStation.position.y;
		var c = 1.5 * system.mainStation.position.distanceTo(system.mainPlanet);
		var x = - b * c / Math.sqrt(b * b + a * a);
		var y = a * c / Math.sqrt(b * b + a * a);
		var z = system.mainStation.position.z;
		var exactPosition = [x, y, z];
		system.setPopulator("sothis_tc", {
			callback: function (pos) {
				system.addShips("sothis", 1, pos, 0);
			}.bind(this),
			location: "COORDINATES",
			coordinates: exactPosition,
			deterministic: true
		})
	}
}

//-------------------------------------------------------------------------------------------------------------
this.shipDockedWithStation = function (station) { 
	//give special welcome screen when docking at sothis
	if (station.hasRole("sothis")) {
		this.$showWelcome = true;
		if (this.$new_cargoes) { 
			//give access to tradenet when visiting sothis
			if (player.ship.equipmentStatus("EQ_CTE_TRADERNET") != "EQUIPMENT_OK") {
				this.$tc_eqTradenet = false;
				player.ship.awardEquipment("EQ_CTE_TRADERNET");
				missionVariables.cargotypeextension_tradernet = clock.days + 30;
			} else
				this.$tc_eqTradenet = true;
		}
	}
}

//-------------------------------------------------------------------------------------------------------------
this.missionScreenOpportunity = function () {
	if (!this.$showWelcome) return;
	var messText = expandDescription("[STC_welcome]");
	if (this.$new_cargoes)
		messText = messText + expandDescription("[STC_new_cargoes]");
	var tcbgpic = "OOmap_G" + (galaxyNumber + 1) + ".png";
	mission.runScreen({
		screenID: "sothis",
		title: "Sothis Trade Center",
		message: messText,
		background: {name: tcbgpic, height:546 }
	});
	this.$showWelcome = false;
}

//-------------------------------------------------------------------------------------------------------------
this.playerBoughtEquipment = function (equipment) { 
	//check for tradernet
	if (equipment == "EQ_CTE_TRADERNET")
		this.$tc_eqTradenet = true;
	else if (equipment == "EQ_CTE_TRADERNET_RENEWAL")
		this.$tc_eqTradenet = true;
}

//-------------------------------------------------------------------------------------------------------------
this.shipLaunchedFromStation = function (station) { 
	//remove tradernet on launch from sothis if not bought
	if (this.$new_cargoes && station.hasRole("sothis")) {
		if (!this.$tc_eqTradenet) {
			player.ship.removeEquipment("EQ_CTE_TRADERNET");
			delete missionVariables.cargotypeextension_tradernet;
		}
	}
}
