using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using UnityEngine;

public class EngineModule : Module {

	private float minSpeed;
	private float maxSpeed;
	private float acceleration;

	private float angularSpeed;
	private float verticalAngleRange;

	private float cruiseAltitude;
	private float minAltitude;
	private float maxAltitude;

	private FuelType fuelType;
	private float fuelConsumption;

	public EngineModule(JObject json) : base(json) {
		maxSpeed = (float)json["maxSpeed"];
		acceleration = (float)json["acceleration"];
		angularSpeed = (float)json["angularSpeed"];

		JToken token;
		if (json.TryGetValue("minSpeed", out token)) {
			minSpeed = (float)json["minSpeed"];
		}
		else {
			minSpeed = 0.0f;
		}

		if (json.TryGetValue("cruiseAltitude", out token)) {
			cruiseAltitude = (float)json["cruiseAltitude"];
		}
		else {
			cruiseAltitude = 0.0f;
		}

		if (json.TryGetValue("minAltitude", out token)) {
			minAltitude = (float)json["minAltitude"];
		}
		else {
			minAltitude = 0.0f;
		}

		if (json.TryGetValue("maxAltitude", out token)) {
			maxAltitude = (float)json["maxAltitude"];
		}
		else {
			maxAltitude = 0.0f;
		}

		if (json.TryGetValue("verticalAngleRange", out token)) {
			verticalAngleRange = (float)json["verticalAngleRange"];
		}
		else {
			verticalAngleRange = 0.0f;
		}

		fuelType = (FuelType)((int)json["fuelType"] - 1);
		fuelConsumption = (float)json["fuelConsumption"];

		sprite = Resources.Load("Sprites/Modules/Engines/" + getUniqueName(), typeof(Sprite)) as Sprite;
	}

	public static EngineModule[] loadModules(string[] filenames) {
		EngineModule[] array = new EngineModule[filenames.Length];

		for (int i = 0; i < filenames.Length; i++) {
			array[i] = new EngineModule(jsonFromFile(filenames[i]));
		}

		return array;
	}

	public float getMaxSpeed() {
		return maxSpeed;
	}

	public float getMinSpeed() {
		return minSpeed;
	}

	public float getAcceleration() {
		return acceleration;
	}

	public float getAngularSpeed() {
		return angularSpeed;
	}

	public float getVerticalAngleRange() {
		return verticalAngleRange;
	}

	public float getCruiseAltitude() {
		return cruiseAltitude;
	}

	public float getMinAltitude() {
		return minAltitude;
	}

	public float getMaxAltitude() {
		return maxAltitude;
	}

	public FuelType getFuelType() {
		return fuelType;
	}

	public float getFuelConsumption() {
		return fuelConsumption;
	}

	override public void updateClasses() {
	}

	override public ModuleData getData(int parent) {
		return new EngineModuleData(parent, this);
	}
}

public class EngineModuleData : ModuleData {
	public EngineModuleData(int parent, Module template) : base(parent, template) {
	}

	override public void deployUnit(int storedUnitID) {
		Debug.LogError("This module cannot deploy units");
	}
}