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

public class IlluminatorModule : AngularModule {
	private int slots;

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

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

	public int getSlots() {
		return slots;
	}

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

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

		return array;
	}

	override public void updateClasses() {
	}

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

public class IlluminatorModuleData : AngularModuleData{
	Set<DeployedUnit> currentlyTargeted;

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

	public int getRemainingSlots() {
		return ((IlluminatorModule)template).getSlots() - currentlyTargeted.count();
	}

	public void addTargetedMissile(DeployedUnit missile) {
		missile.setIlluminated(true);
		currentlyTargeted.add(missile);
	}

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