using System.Collections.Generic;
using UnityEngine;

public abstract class Unit {
	protected int id;
	protected Class unitClass;
	protected UnitModules modules;
	protected Loadout currentLoadout = null;

	public Unit(Class unitClass) {
		this.unitClass = unitClass;
		id = Util.freshId();

		modules = new UnitModules(this.getID(), unitClass);
	}

	public Unit(Unit oldUnit) {
		this.unitClass = oldUnit.getClass();
		id = oldUnit.getID();

		modules = oldUnit.modules;
	}

	public Unit(int id, int classID) {
		this.unitClass = GameController.classFromID(classID);
		this.id = id;

		modules = new UnitModules(this.getID(), unitClass, false);
	}

	public int getID() {
		return id;
	}

	public Class getClass() {
		return unitClass;
	}

	public Country getOwner() {
		return unitClass.getCountry();
	}

	public List<Tuple<Module, ModuleData>> getModules() {
		return modules.getModules();
	}

	public void addReadyToLaunch(StoredUnit stored) {
		modules.addReadyToLaunch(stored);
	}

	public void addToStorage(StoredUnit stored) {
		modules.addToStorage(stored);
	}

	public bool hasFuelLeft(FuelType type) {
		return modules.getFuelAmount(type) > 0.0f;
	}

	public bool hasRadarOn() {
		return modules.hasRadarOn();
	}

	public float getMinSpeed() {
		return modules.getMinSpeed();
	}

	public float getMaxSpeed() {
		return modules.getMaxSpeed(new GraphParams());
	}

	public float getMinAltitude() {
		return modules.getMinAltitude();
	}

	public float getMaxAltitude() {
		return modules.getMaxAltitude();
	}

	public bool canBeCommanded() {
		return modules.canBeCommanded();
	}

	public bool canBeSteered() {
		return modules.canBeSteered();
	}

	public int availableMissileControlSlots(DeployedUnit target) {
		return modules.availableMissileControlSlots(target);
	}

	public void addControlledMissile(Unit missile, DeployedUnit target) {
		modules.addControlledMissile(missile, target);
	}

	public ContainerModuleData getContainerModule(Class toBeStored) {
		return modules.getContainerModule(toBeStored);
	}

	//SHOULD ONLY BE CALLED BY NETWORK MANAGER, WHEN RECEIVING A CREATE MODULE DATA COMMAND
	public void addModuleData(ModuleData data) {
		modules.addModuleData(data);
	}

	public List<ModuleData> getModuleDatas(string moduleName) {
		List<ModuleData> res = new List<ModuleData>();

		foreach(Tuple<Module, ModuleData> tuple in getModules()) {
			if (tuple._1.getUniqueName() == moduleName) {
				res.Add(tuple._2);
			}
		}

		return res;
	}

	public void setCurrentLoadout(Loadout newLoadout) {
		currentLoadout = newLoadout;
	}

	public Loadout getCurrentLoadout() {
		return currentLoadout;
	}
}
