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

public class MissileControlModule : AngularModule {
	private int slots;

	public MissileControlModule(JObject json) : base(json) {
		slots = (int)json["slots"];

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

	public int getSlots() {
		return slots;
	}

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

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

		return array;
	}

	override public void updateClasses() {
	}

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

public class MissileControlModuleData : AngularModuleData{
	Set<DeployedUnit> currentlyManaged;

	public MissileControlModuleData(int parent, Module template, float angleOffset, Vector position) : base(parent, template, angleOffset, position) {
		currentlyManaged = new Set<DeployedUnit>();
	}

	public int getRemainingSlots() {
		return ((MissileControlModule)template).getSlots() - currentlyManaged.count();
	}

	public void addControlledMissile(DeployedUnit missile) {
		currentlyManaged.add(missile);
	}

	override public void checkDestroyedUnits(List<DeployedUnit> destroyedUnits) {
		foreach(DeployedUnit destroyed in destroyedUnits) {
			currentlyManaged.remove(destroyed);

		}
	}
}