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

public class CommandModule : Module {
	private float launchDistance;
	private List<Tuple<float, float>> landingPattern;
	private bool canSteer;
	private bool canCommand;

	public CommandModule(JObject json) : base(json) {
		

		JToken token;

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

		landingPattern = new List<Tuple<float, float>>();
		if (json.TryGetValue("landingPattern", out token)) {
			foreach (JObject element in json["landingPattern"]) {
				landingPattern.Add(new Tuple<float, float>((float)element["distance"], (float)element["altitude"]));
			}
		}

		if (json.TryGetValue("canSteer", out token)) {
			canSteer = (bool)json["canSteer"];
		}
		else {
			canSteer = true;
		}

		if (json.TryGetValue("canCommand", out token)) {
			canCommand = (bool)json["canCommand"];
		}
		else {
			canCommand = true;
		}

		icon = Resources.Load("Sprites/Modules/Icons/Commands/" + getUniqueName(), typeof(Sprite)) as Sprite;
		silhouette = Resources.Load("Sprites/Modules/Silhouettes/Commands/" + getUniqueName(), typeof(Sprite)) as Sprite;
	}

	public bool getCanSteer() {
		return canSteer;
	}

	public bool getCanCommand() {
		return canCommand;
	}

	public float getLaunchDistance() {
		return launchDistance;
	}

	public List<Tuple<float, float>> getLandingPattern() {
		return landingPattern;
	}

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

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

		return array;
	}

	override public bool canBeDisabled() {
		return false;
	}

	override public void updateClasses() {
	}

	override public ModuleData getData(int parent, float angleOffset, Vector position) {
		return new CommandModuleData (parent, this, position);
	}
}

public class CommandModuleData : ModuleData{
	public CommandModuleData(int parent, Module template, Vector position) : base(parent, template, position) {
	}
}