using UnityEngine.Assertions;

public enum LoadingState {None = 0, BeingPrepared = 1, BeingLoaded = 2, ReadyToLaunch = 3, BeingFired = 4};

public class StoredUnit : Unit {
	private ContainerModuleData store;
	private LoadingState loadingState;

	public StoredUnit(Class unitClass) : base(unitClass) {
		loadingState = LoadingState.None;
		GameController.addStoredUnit(this);
	}

	public StoredUnit(DeployedUnit deployed) : base(deployed) {
		loadingState = LoadingState.None;
		GameController.addStoredUnit(this);
	}

	public StoredUnit(int id, int classID) : base (id, classID) {
		loadingState = LoadingState.None;
		GameController.addStoredUnit(this);
	}

	public void changeStore(ContainerModuleData store) {
		if (this.store == store) {
			return;
		}

		removeFromStore();

		this.store = store;
		store.addToStored(this);
	}

	public void removeFromStore() {
		if (store == null) {
			return;
		}

		store.removeFromStored(this);

		store = null;
	}

	public ContainerModuleData getStore() {
		return store;
	}

	public bool isBeingLoaded() {
		return loadingState != LoadingState.None;
	}

	public void setLoadingState(LoadingState newState) {
		loadingState = newState;
	}

	public LoadingState getLoadingState() {
		return loadingState;
	}

	//Adds the corresponding DeployedUnit to the game
	public DeployedUnit deploy() {
		Vertex position = GameController.fromID(store.getParent()).getPosition();

		Assert.IsNotNull(store);
		store.removeFromStored(this);

		DeployedUnit deployed = new DeployedUnit(this, position);
		GameController.addDeployedUnit(deployed);
		GameController.removedStoredUnit(this);

		return deployed;
	}
}
