"use strict";
this.name               = "tech_ref_lib";
this.author               = "spara";
this.copyright            = "2013 Mika Spåra";
this.description         = "Reveal technical information of a target";
this.version            = "1.0.1";
this.licence     = "CC BY-NC-SA 3.0";

//1.0.1 different method for naming unnamed ships. No more warnings in test build.

this.startUp = function() {
	//these are recognised as spacecraft
	this.$spaceCraftScanClasses = ["CLASS_NEUTRAL", "CLASS_MILITARY", "CLASS_POLICE", "CLASS_THARGOID"];
	//these are treated as classified spacecraft, only type (name property) will be shown
	this.$classifiedScanClasses = ["CLASS_MILITARY", "CLASS_POLICE", "CLASS_THARGOID"];
	this.$classifiedRoles = ["constrictor", "kephalan", "odonatean", "scorpax", "bigTrader", "monkpatrol", "griff_blackmonk_defenceship", "monkhit1", "monkhit2", "monkhold1", "monkhold2", "ev_green_gecko", "sin-pirate"];//classify constrictor, aliens oxp, bigTrader ships, black monks, green gecko oxp and capisastra oxp.
	if (player.ship.equipmentStatus("EQ_SCANNER_SHOW_MISSILE_TARGET") === "EQUIPMENT_OK")
		player.ship.awardEquipment("EQ_TECH_REF_LIB_BASE");
}

//interface for other scripts to add classified roles
this.$addClassifiedRole = function(role) {
	if (this.$classifiedRoles.indexOf(role) === -1)
		this.$classifiedRoles.push(role);
}

//interface for other scripts to remove classified roles
this.$removeClassifiedRole = function(role) {
	var index = this.$classifiedRoles.indexOf(role);
	if (index !== -1)
		this.$classifiedRoles.splice(index, 1);
}


//equipment activation n
this.activated = function() {
	//hud integration
	if (player.ship.equipmentStatus("EQ_TECH_REF_LIB") === "EQUIPMENT_OK")
		worldScripts.tech_ref_lib.$activated();
	//freebie version
	else worldScripts.tech_ref_lib.$showFreebie();
}

//equipment activation b
this.mode = function() {
	//freebie version
	worldScripts.tech_ref_lib.$showFreebie();
}

this.playerBoughtEquipment = this.equipmentRepaired = function(equipment) {
	if (equipment === "EQ_SCANNER_SHOW_MISSILE_TARGET")
		player.ship.awardEquipment("EQ_TECH_REF_LIB_BASE");
}

this.equipmentDestroyed = this.equipmentDamaged = function(equipment) {
	if (equipment === "EQ_SCANNER_SHOW_MISSILE_TARGET")
		player.ship.removeEquipment("EQ_TECH_REF_LIB_BASE");
}

//which crafts are included in the library
this.$validateTarget = function(target) {
	//check scanclasses
	if (this.$classifiedScanClasses.indexOf(target.scanClass) !== -1) return false;
	//check roles
	var targetRoles = target.roles;
	var i;
	for (i = 0; i < targetRoles.length; i++) {
		if (this.$classifiedRoles.indexOf(targetRoles[i]) !== -1) {
			return false;
		}
	}
	//check for script_info key
	if (target.scriptInfo.classifiedShip) return false;
	return true;
}

//check for ship image
this.$bgImage = function(name) {
	var names = ["Adder", "Anaconda", "Asp Mark II", "Boa", "Boa Class Cruiser", "Cobra Mark III", "Cobra Mark I", "Fer-de-Lance", "Gecko", "Krait", "Mamba", "Moray Star Boat", "Moray Medical Boat", "Python", "Orbital Shuttle", "Sidewinder Scout Ship", "Transporter", "Mining Transporter", "Worm"];
	var append = ["adder", "anaconda", "asp", "boa", "boacc", "cobramk3", "cobramk1", "ferdelance", "gecko", "krait", "mamba", "moray", "moray", "python", "shuttle", "sidewinder", "transporter", "transporter", "worm"];
	var index = names.indexOf(name);
	if (index !== -1) {
		return "ref_background_"+append[index]+".png";
	}
	else return "ref_background.png";
}

//specs to a mission screen
this.$showFreebie = function() {
	var target = player.ship.target;
	//no target
	if (!target) {
		player.consoleMessage("No target.");
		return;
	}
	//target is not a spacecraft
	if (this.$spaceCraftScanClasses.indexOf(target.scanClass) === -1) {
		player.consoleMessage("Target is not a spacecraft.");
		return;
	}
	//check if target is classified
	var targetValidated = this.$validateTarget(target);
	//special type recognition for random hits & anarchies
	var name = expandMissionText("trophy_"+target.dataKey);
	if (!name) name = target.name;
	//build up the message
	var message = "Specifications for '" + target.displayName+"':\n\n";
	if (!targetValidated)
		message += "No match found from the Reference\nLibrary, ship specifications unknown.";
	else {
		message += "* Type: " + name + "\n";
		message += "* Size (W × H × L): " +  Math.floor(target.boundingBox.x) + " m × " +  Math.floor(target.boundingBox.y) + " m × " +  Math.floor(target.boundingBox.z) + " m\n";
		message += "* Speed / Thrust: " + target.maxSpeed/1000 + " / " +target.maxThrust/1000 + " LM\n";
		message += "* Roll / Pitch: " + target.maxRoll.toFixed(1) +" / "+ target.maxPitch.toFixed(1) + "\n";			
		message += "* Cargo Capacity: " + target.cargoSpaceCapacity + " TC\n";
		message += "* Energy Banks: " + Math.floor(target.maxEnergy/64) + "\n";
		if (target.forwardWeapon || target.aftWeapon || target.portWeapon || target.starboardWeapon)
			message += "* Weapon Mounts: yes\n";
		else message += "* Weapon Mounts: no\n";
		message += "* Missile Slots: "+target.missileCapacity+"\n";
		if (target.hasHyperspaceMotor) message += "* Hyperspace Capable: yes";
		else message += "* Hyperspace Capable: no";
	}
	//show the message in a mission screen
	mission.runScreen({
		title: "Technical Reference Library of Common Ships",
		message: message,
		background: this.$bgImage(name),
		allowInterrupt: true
	});
}

//specs to the hud 
this.$activated = function() {	
	var target = player.ship.target;
	//no target
	if (!target) {
		player.consoleMessage("No target.");
		return;
	}
	//target is not a spacecraft
	if (this.$spaceCraftScanClasses.indexOf(target.scanClass) === -1) {
		player.consoleMessage("Target is not a spacecraft.");
		return;
	}
	//check if target is classified
	var targetValidated = this.$validateTarget(target);
	//special type recognition for random hits & anarchies
	var name = expandMissionText("trophy_"+target.dataKey);
	if (!name) name = target.name;
	//build up the message
	
	if (!targetValidated)
		var message = "Target specs unknown.";
	else {
		var message = "Type: " + name + "\n";
		message += "Size (W × H × L): " +  Math.floor(target.boundingBox.x) + " m × " +  Math.floor(target.boundingBox.y) + " m × " +  Math.floor(target.boundingBox.z) + " m\n";
		message += "Speed / Thrust: " + target.maxSpeed/1000 + " / " +target.maxThrust/1000 + " LM\n";
		message += "Roll / Pitch: " + target.maxRoll.toFixed(1) +" / "+ target.maxPitch.toFixed(1) + "\n";			
		message += "Cargo Capacity: " + target.cargoSpaceCapacity + " TC\n";
		message += "Energy Banks: " + Math.floor(target.maxEnergy/64) + "\n";
		if (target.forwardWeapon || target.aftWeapon || target.portWeapon || target.starboardWeapon)
			message += "Weapon Mounts: yes\n";
		else message += "Weapon Mounts: no\n";
		message += "Missile Slots: "+target.missileCapacity+"\n";
		if (target.hasHyperspaceMotor) message += "Hyperspace: yes";
		else message += "Hyperspace: no";
	}
	//show the message in the hud
	player.consoleMessage(message,10);
}
