/* jshint bitwise:false, forin:false */
/* global clock,galaxyNumber,guiScreen,mission,missionVariables,player,system,worldScripts */
/* (C) Svengali 2016-2018, License CC-by-nc-sa-4.0 */
(function () {
	"use strict";
	this.name = "Lib_PAD_Events";

	this.$config = {
		bounty: 0,
		credits: 0,
		fined: 0,
		rank: expandMissionText("LIB_PAD_RANK_HARMLESS"),
		status: expandMissionText("LIB_PAD_STATUS_CLEAN"),
		updCR: 0,
		updNavy: 1,
		updNova: 1,
		updTP: 0
	};
	this.$delay = [];
	this.$init = 1;
	this.startUp = function () {
		this.$init = 0;
	};
	this.startUpComplete = function () {
		var m = ["PRELUDE", "MISSION_COMPLETE", "STAGE_1", "RUNNING"], mc = -1, mt = -1;
		if (missionVariables.Lib_PAD_EVENTS) this.$config = JSON.parse(missionVariables.Lib_PAD_EVENTS);
		if (missionVariables.conhunt) mc = m.indexOf(missionVariables.conhunt);
		if (missionVariables.thargplans) mt = m.indexOf(missionVariables.thargplans);
		if (this.$config.updNavy && worldScripts.Lib_PAD.$data.GALCOP.NAVY.entry === "") this._add("GALCOP.NAVY.entry", clock.clockString, 1);
		this.$config.updNavy = 0;
		if (mc > -1) this._addCurruthers(1);
		if (mt > -1) {
			this._addFortesque(1);
			if (mt > 0) this._addBlake(1);
		}
		if (this.$config.updNova && missionVariables.nova === "NOVA_HERO") {
			this._add("PERSONS.GENERIC.info", expandMissionText("LIB_EVENTS_NOVA"), 1);
			this.$config.updNova = 0;
		}
		this.$config.bounty = player.bounty;
		this.$config.credits = player.credits;
		this.$config.rank = player.rank;
		this.$config.status = player.legalStatus;
	};
	this.playerWillSaveGame = function () {
		missionVariables.Lib_PAD_EVENTS = JSON.stringify(this.$config);
	};
	this.guiScreenChanged = function () {
		if (!player.ship.docked) return;
		if (guiScreen === "GUI_SCREEN_MISSION" && mission.screenID) {
			switch (mission.screenID) {
				case "oolite-constrictor-hunt-briefing":
					if (this.$config.updNavy) this._add("GALCOP.NAVY.entry", clock.clockString.substr(0, 13));
					this.$config.updNavy = 0;
					this._add("LOGS.GENERIC.list", expandMissionText("LIB_EVENT_CONSTRICTOR_START"));
					this._addCurruthers();
					this.$config.updCR = 1;
					break;
				case "oolite-constrictor-hunt-debriefing":
					this._add("LOGS.GENERIC.list", expandMissionText("LIB_EVENT_CONSTRICTOR_WIN1"));
					this._add("GALCOP.NAVY.missions", expandMissionText("LIB_EVENT_CONSTRICTOR_WIN2"));
					this._add("GALCOP.NAVY.kills", "++", 1);
					this.$config.updCR = 0;
					break;
				case "oolite-thargoid-plans-briefing1":
					this._addFortesque();
					break;
				case "oolite-thargoid-plans-briefing2":
					this._add("LOGS.GENERIC.list", expandMissionText("LIB_EVENT_THARPLANS_START"));
					this._addBlake();
					this.$config.updTP = 1;
					break;
				case "oolite-thargoid-plans-debriefing":
					this._add("GALCOP.NAVY.missions", expandMissionText("LIB_EVENT_THARPLANS_WIN2"));
					this._add("LOGS.GENERIC.list", expandMissionText("LIB_EVENT_THARPLANS_WIN1"));
					this.$config.updTP = 0;
					break;
				case "oolite-nova-hero":
				case "oolite-nova-disappointed":
				case "oolite-nova-ignored":
				case "oolite-nova-coward":
					this._add("PERSONS.GENERIC.info", expandMissionText("LIB_EVENTS_NOVA"));
					this.$config.updNova = 0;
					break;
			}
		}
	};
	this.playerCompletedContract = function (type, result, fee, contract) {
		var txt, f, prefix;
		if (!fee) {
			switch (type) {
				case "passenger": txt = expandMissionText("LIB_EVENT_CONTRACT_PSNGR_NOFEE", { name: contract.name }); break;
				case "parcel": txt = expandMissionText("LIB_EVENT_CONTRACT_PARCEL_NOFEE", { name: contract.name }); break;
				case "cargo": txt = expandMissionText("LIB_EVENT_CONTRACT_CARGO_NOFEE", { name: contract.cargo_description }); break;
			}
		} else {
			// logically, the "failed" result can't get here. in the event of a failed result, the fee would be 0
			switch(result) {
				case "success":
					prefix = expandMissionText("LIB_EVENT_CONTRACT_ARRIVE");
					break;
				case "short":
					prefix = expandMissionText("LIB_EVENT_CONTRACT_SHORT");
					break;
				default:
					prefix = expandMissionText("LIB_EVENT_CONTRACT_LATE");
			}

			f = formatCredits(fee / 10, 1);

			switch (type) {
				case "passenger":
					txt = expandMissionText("LIB_EVENT_CONTRACT_PSNGR_FEE", { prefix: prefix, fee: f, name: contract.name });
					break;
				case "parcel":
					txt = expandMissionText("LIB_EVENT_CONTRACT_PARCEL_FEE", { prefix: prefix, fee: f, name: contract.name });
					break;
				case "cargo":
					txt = expandMissionText("LIB_EVENT_CONTRACT_CARGO_FEE", { prefix: prefix, fee: f, name: contract.cargo_description });
					break;
			}
		}
		if (txt) this._add("LOGS.GENERIC.list", txt);
	};
	this.playerEnteredNewGalaxy = function () {
		this.$delay.push(["LOGS.GENERIC.list", expandMissionText("LIB_EVENT_JUMP_GAL", { num: +(galaxyNumber + 1) })]);
	};
	this.playerRescuedEscapePod = function (fee, reason, pilot) {
		fee = formatCredits(fee, 1);
		if (system.isInterstellarSpace) {
			if (fee) this.$delay.push(["LOGS.GENERIC.list", expandMissionText("LIB_EVENT_RESCUED_POD_INT_FEE", { name: pilot.name, fee: (fee / 10) })]);
			else this.$delay.push(["LOGS.GENERIC.list", expandMissionText("LIB_EVENT_RESCUED_POD_INT", { name: pilot.name })]);
		} else {
			if (fee) this.$delay.push(["LOGS.GENERIC.list", expandMissionText("LIB_EVENT_RESCUED_POD_FEE", { name: pilot.name, sys: system.name, fee: (fee / 10) })]);
			else this.$delay.push(["LOGS.GENERIC.list", expandMissionText("LIB_EVENT_RESCUED_POD", { name: pilot.name, sys: system.name })]);
		}
	};
	this.shipLaunchedEscapePod = function () {
		if (system.isInterstellarSpace) this.$delay.push(["LOGS.GENERIC.list", expandMissionText("LIB_EVENT_EJECT_INT")]);
		else this.$delay.push(["LOGS.GENERIC.list", expandMissionText("LIB_EVENT_EJECT", { sys: system.name })]);
	};
	this.shipWillDockWithStation = function () {
		this.$config.bounty = player.bounty;
		this.$config.credits = player.credits;
		if (player.rank !== this.$config.rank) this._add("LOGS.GENERIC.list", expandMissionText("LIB_EVENT_RANK", { rank: player.rank }));
		this.$config.rank = player.rank;
		this.$config.status = player.legalStatus;
		this._addDelayed();
	};
	this.shipDockedWithStation = function (st) {
		if (!player.ship.docked) return;
		var m;
		if (this.$config.credits !== player.credits) {
			if (player.credits > this.$config.credits) m = expandMissionText("LIB_EVENT_EARNED", { amount: formatCredits(player.credits - this.$config.credits, 1), stn: st.displayName });
			else m = expandMissionText("LIB_EVENT_LOST", { amount: formatCredits(this.$config.credits - player.credits, 1), stn: st.displayName });
			if (system.isInterstellarSpace) m += ".";
			else m += " " + system.name + ".";
			this._add("LOGS.GENERIC.list", m);
		}
		if (player.ship.markedForFines) this.$config.fined++;
	};
	this.playerBoughtNewShip = function () {
		this.$config.credits = player.credits;
	};
	// Note: Handler is fired (too?) early, so guarding (this.$init) is necessary!!!
	this.equipmentAdded = function (eq) {
		if (this.$init) return;
		this.$config.credits = player.credits;
		if (eq === "EQ_NAVAL_ENERGY_UNIT") {
			this._add("LOGS.GENERIC.list", expandMissionText("LIB_EVENT_NEU"));
			this._add("GALCOP.NAVY.awards", expandMissionText("LIB_EVENT_AWARD_NEU"));
		}
		if (eq === "EQ_CLOAKING_DEVICE") {
			this.$delay.push(["LOGS.GENERIC.list", expandMissionText("LIB_EVENT_CLOAK")]);
			this.$delay.push(["PERSONS.GENERIC.info", expandMissionText("LIB_EVENT_AWARD_CLOAK")]);
		}
	};
	this.shipLaunchedFromStation = function () {
		this.$config.bounty = player.bounty;
		this.$config.credits = player.credits;
		this.$config.rank = player.rank;
		this.$config.status = player.legalStatus;
	};
	this.shipBountyChanged = function (delta) {
		var m;
		if (this.$config.bounty === player.bounty + delta) return;
		if (!player.bounty) m = expandMissionText("LIB_EVENT_BOUNTY_CLEARED");
		else {
			if (delta > 0) m = expandMissionText("LIB_EVENT_BOUNTY_RCVD");
			if (delta < 0) m = expandMissionText("LIB_EVENT_BOUNTY_REDUCED");
			if (this.$config.status !== player.legalStatus) m += expandMissionText("LIB_EVENT_BOUNTY_STATUS", { status: player.legalStatus });
		}
		if (m) this.$delay.push(["LOGS.GENERIC.list", m]);
		this.$config.bounty = player.bounty;
		this.$config.status = player.legalStatus;
	};
	this.shipKilledOther = function (whom) {
		if (player.rank !== this.$config.rank) {
			this.$delay.push(["LOGS.GENERIC.list", expandMissionText("LIB_EVENT_RANK", { rank: player.rank })]);
			this.$config.rank = player.rank;
		}
		if (this.$config.updTP && whom && whom.isThargoid) this.$delay.push(["GALCOP.NAVY.kills", "++", 1]);
	};
	this.shipWillExitWitchspace = function () {
		if (system.isInterstellarSpace) this.$delay.push(["LOGS.GENERIC.list", expandMissionText("LIB_EVENT_MISJUMP")]);
	};
	this._add = function (p, w, s) {
		worldScripts.Lib_PAD._setPageEntry(p, w, s);
	};
	this._addDelayed = function (p, w, s) {
		var l = this.$delay.length;
		if (l) {
			for (var i = 0; i < l; i++) worldScripts.Lib_PAD._setPageEntry(this.$delay[i][0], this.$delay[i][1], this.$delay[i][2]);
		}
		this.$delay.length = 0;
	};
	this._addPage = function (p, w, t, s) {
		worldScripts.Lib_PAD._addPageInCategory(p, w, t, s);
	};
	this._addCurruthers = function (s) {
		this._addPage("PERSONS.CAPTAIN CURRUTHERS",
			{ name: expandMissionText("LIB_CHAR_NAME_CURRUTHERS"), origin: expandMissionText("LIB_CHAR_RESTRICTED"), species: expandMissionText("LIB_PAD_SPECIES_HUMAN_COLONIAL"), gender: expandMissionText("LIB_PAD_GENDER_MALE"), age: 45, ship: expandMissionText("LIB_CHAR_SHIP_VIPER"), rank: expandMissionText("LIB_CHAR_RANK_CAPTAIN"), t0: 21, t1: "lib_ovc_curruthers.png", t2: 8 }, ["GALCOP.NAVY"], s);
	};
	this._addFortesque = function (s) {
		this._addPage("PERSONS.CAPTAIN FORTESQUE", { name: expandMissionText("LIB_CHAR_NAME_FORTESQUE"), origin: expandMissionText("LIB_CHAR_RESTRICTED"), species: expandMissionText("LIB_PAD_SPECIES_HUMAN_COLONIAL"), gender: expandMissionText("LIB_PAD_GENDER_MALE"), age: 41, ship: expandMissionText("LIB_CHAR_SHIP_VIPER"), rank: expandMissionText("LIB_CHAR_RANK_CAPTAIN"), t0: 21, t1: "lib_ovc_fortesque.png", t2: 8 }, ["GALCOP.NAVY"], s);
	};
	this._addBlake = function (s) {
		this._addPage("PERSONS.AGENT BLAKE", { name: expandMissionText("LIB_CHAR_NAME_BLAKE"), origin: expandMissionText("LIB_CHAR_RESTRICTED"), species: expandMissionText("LIB_CHAR_RESTRICTED"), gender: expandMissionText("LIB_PAD_GENDER_MALE"), age: 34, ship: expandMissionText("LIB_CHAR_RESTRICTED"), rank: expandMissionText("LIB_CHAR_RANK_AGENT"), t0: 21, t1: "lib_ovc_blake.png" }, ["GALCOP.NAVY"], s);
	};
}).call(this);
