"use strict";
this.name = "PlanetFall_Oohaul";
this.author = "Thargoid";
this.copyright = "Creative Commons Attribution - Non-Commercial - Share Alike 3.0 license with clauses - see readme.txt.";
this.description = "Oo-Haul escort mission from the main planet to the witchpoint & constore.";
this.version = "2.0";

//-------------------------------------------------------------------------------------------------------------
this.startUp = function () {
	missionVariables.PFoohaul_mis_stage = "1";
	// if the player has attached one of the transports, don't offer any more missions (but have a random change of having it cleared)
	if (missionVariables.PFoohaul_badrep == "Y") {
		if (Math.random() > 0.001) {
			missionVariables.PFoohaul_mis_stage = "4";
		} else {
			missionVariables.PFoohaul_badrep = null;
		}
	}
	var pf = worldScripts.PlanetFall2;
	if (pf) pf._landingImages["oohaul"] = ["oohaul1.png", "oohaul2.png", "oohaul3.png", "oohaul4.png", "oohaul5.png","oohaul6.png"];
}

//-------------------------------------------------------------------------------------------------------------
this.shipExitedWitchspace = function () {
	if (missionVariables.PFoohaul_mis_stage == "2" || missionVariables.PFoohaul_mis_stage == "3") {
		mission.setInstructionsKey(null);
		missionVariables.PFoohaul_mis_stage = "1";
		if (missionVariables.PFoohaul_mis_done == "TRAITOR") missionVariables.PFoohaul_mis_stage = "4"
		missionVariables.PFoohaul_mis_done = null;
	}
}

//-------------------------------------------------------------------------------------------------------------
this.missionScreenOpportunity = function () {
	if (!player.ship.dockedStation.hasRole("planetFall2_mainSurface_oohaul") && !player.ship.dockedStation.hasRole("constore")) { // if we're not at a constore or the Oohaul planetside distribution centre, bail out here
		return;
	}

	if (player.ship.dockedStation.hasRole("planetFall2_mainSurface_oohaul") && missionVariables.PFoohaul_mis_stage == "1") {
		this.PFoohaul_conArray = system.shipsWithPrimaryRole("constore");
		if (system.government > 3) {
			missionVariables.PFoohaul_credits = 2276;
			if (this.PFoohaul_conArray.length > 0) {
				mission.runScreen({ 
					titleKey: "PFoohaul_con_title", 
					messageKey: "PFoohaul_con_desc_lodanger", 
					choicesKey: "PFoohaul_con_choice", 
					shipKey: "PFoohaulerWit" 
				}, 
					this.choices);
			} else {
				mission.runScreen({ 
					titleKey: "PFoohaul_wit_title", 
					messageKey: "PFoohaul_wit_desc_lodanger", 
					choicesKey: "PFoohaul_wit_choice", 
					shipKey: "PFoohaulerWit" 
				},
					this.choices);
			}
			missionVariables.PFoohaul_mis_stage = "2";
		} else {
			missionVariables.PFoohaul_credits = 4330;
			if (this.PFoohaul_conArray.length > 0) {
				mission.runScreen({ 
					titleKey: "PFoohaul_con_title", 
					messageKey: "PFoohaul_con_desc_hidanger", 
					choicesKey: "PFoohaul_con_choice", 
					shipKey: "PFoohaulerCon"
				}, 
					this.choices);
			} else {
				mission.runScreen({ 
					titleKey: "PFoohaul_wit_title", 
					messageKey: "PFoohaul_wit_desc_hidanger", 
					choicesKey: "PFoohaul_wit_choice", 
					shipKey: "PFoohaulerCon" 
				}, 
					this.choices);
			}
			missionVariables.PFoohaul_mis_stage = "2";
		}
		return;
	}

	if (player.ship.dockedStation.hasRole("constore") && missionVariables.PFoohaul_mis_stage == "3") {
		if (missionVariables.PFoohaul_mis_done == "YES") {
			mission.runScreen({ 
				titleKey: "PFoohaul_complete_title", 
				messageKey: "PFoohaul_con_desc_docked" 
			});
			missionVariables.PFoohaul_mis_done = null;
			player.credits += parseInt(missionVariables.PFoohaul_credits);
			missionVariables.PFoohaul_credits = null;
			missionVariables.PFoohaul_mis_stage = "1";
			mission.setInstructionsKey(null)
            if (worldScripts.GalCopBB_Reputation) {
                var rep = worldScripts.GalCopBB_Reputation;
                rep.$adjustReputationForEntity(expandDescription("[gcm_reputation_localgov]"), system.ID, "Success", 1, 1);
            }
		} else if (missionVariables.PFoohaul_mis_done === "TRAITOR") {
            mission.runScreen({
                titleKey: "PFoohaul_title",
                messageKey: "PFoohaul_traitor",
            });
            mission.setInstructions(null);
            missionVariables.PFoohaul_credits = null;
            missionVariables.PFoohaul_mis_stage = null;
			missionVariables.PFoohaul_badrep = "Y";
            if (worldScripts.GalCopBB_Reputation) {
                var rep = worldScripts.GalCopBB_Reputation;
                rep.$adjustReputationForEntity(expandDescription("[gcm_reputation_localgov]"), system.ID, "Failed", 1, 1);
            }
		} else {
			missionVariables.PFoohaul_mis_stage = null;
			missionVariables.PFoohaul_credits = null;
			mission.setInstructionsKey(null)
		}
	}
}

//-------------------------------------------------------------------------------------------------------------
this.shipWillLaunchFromStation = function () {
	if (missionVariables.PFoohaul_mis_stage == "0") {
		missionVariables.PFoohaul_mis_stage = "1";
	}
	this.PFoohaul_conArray = system.shipsWithPrimaryRole("constore");
}

//-------------------------------------------------------------------------------------------------------------
this.shipLaunchedFromStation_hold = function() {
	if (this.oohauler) {
		this.oohauler.beaconLabel = this.oohauler.displayName;
		this.oohauler = null;
		delete this.shipLaunchedFromStation;
	}
}

//-------------------------------------------------------------------------------------------------------------
this.choices = function (choice) {
	if (choice == "PFoohaul_no" && missionVariables.PFoohaul_mis_stage == "2") {
		missionVariables.PFoohaul_mis_stage = "0";
		missionVariables.PFoohaul_credits = 0;
		return;
	}

	if (choice == "PFoohaul_yes_con" && missionVariables.PFoohaul_mis_stage == "2") {
		var oohaul = system.addShips("PHoohaulerCon", 1, system.mainStation.position.add([2000, 2000, 0]), 100);
		if (oohaul && oohaul.length > 0) {
			missionVariables.PFoohaul_mis_stage = "3";
			missionVariables.PFoohaul_cargo = 3;
			// make sure the player can keep up
			if (oohaul[0].maxSpeed > player.ship.maxSpeed) {
				oohaul[0].maxSpeed = player.ship.maxSpeed - 5;
			}
			this.oohauler = oohaul[0];
			this.shipLaunchedFromStation = this.shipLaunchedFromStation_hold;
			mission.setInstructionsKey("PFoohaul_con_desc_short");
			return;
		} else {
			mission.runScreen({
				titleKey: "PFoohaul_title",
				messageKey: "PFoohaul_spawn_fail",
				model: "PFoohaulerCon"
			});
			missionVariables.PHoohaul_mis_stage = null;
			return;
		}
	}

	if (choice == "PFoohaul_yes_wit" && missionVariables.PFoohaul_mis_stage == "2") {
		var oohaul = system.addShips("PFoohaulerWit", 1, system.mainStation.position.add([2000, 2000, 0]), 100);
		if (oohaul && oohaul.length > 0) {
			missionVariables.PFoohaul_mis_stage = "3";
			missionVariables.PFoohaul_cargo = 3;
			// make sure the player can keep up
			if (oohaul[0].maxSpeed > player.ship.maxSpeed) {
				oohaul[0].maxSpeed = player.ship.maxSpeed - 5;
			}
			this.oohauler = oohaul[0];
			this.shipLaunchedFromStation = this.shipLaunchedFromStation_hold;
			mission.setInstructionsKey("PFoohaul_wit_desc_short");
			return;
		} else {
			mission.runScreen({
				titleKey: "PFoohaul_title",
				messageKey: "PFoohaul_spawn_fail",
				model: "PFoohaulerCon"
			});
			missionVariables.PHoohaul_mis_stage = null;
			return;
		}
	}
}