package zero1hd.rhythmbullet.audio.map;

import zero1hd.rhythmbullet.audio.MusicDataPack;
import zero1hd.rhythmbullet.entity.Entity;
import zero1hd.rhythmbullet.entity.EntityFrame;
import zero1hd.rhythmbullet.entity.coordinator.Coordinator;
import zero1hd.rhythmbullet.entity.coordinator.CoordinatorFrame;

public class GamePlayMap {
	private MusicDataPack musicData;
	private MapWindowData[] spawnList;
	private boolean building;
	private int index;
	
	private byte[] hudType;
	/**
	 * GamePlayMap is what the game area will use to generate entities and judge current audio data
	 * @param audioData audio data
	 */
	public GamePlayMap(MusicDataPack audioData, int totalWindows) {
		this.musicData = audioData;
		spawnList = new MapWindowData[totalWindows];
		hudType = new byte[totalWindows];
	}
	
	public int setIndex(int index) {
		int previousIndex = this.index;
		if (index < 0) {
			this.index = 0;
		} else if (index >= spawnList.length) {
			toHead();
		} else {
			this.index = index;
		}
		return previousIndex;
	}
	
	public void setHUDType(byte data) {
		hudType[index] = data;
	}
	
	public byte[] getHudType() {
		return hudType;
	}
	
	public EntitySpawnInfo addEntity(EntityFrame<? extends Entity> entityType, CoordinatorFrame<? extends Coordinator> coordinator) {
		if (building) {
			if (spawnList[index] == null) {
				spawnList[index] = new MapWindowData();
			}
			EntitySpawnInfo esi = new EntitySpawnInfo(entityType, coordinator);
			spawnList[index].addEntity(esi);
			return esi;
		} else {
			throw new IllegalStateException("Stupid, you need to call begin building first first.");
		}
	}
	
	public void nextWindowData() {
		if (building) {
			index++;
		} else {
			throw new IllegalStateException("Stupid, you need to call begin building first first.");
		}
	}
	
	public void toHead() {
		index = spawnList.length-1;
	}
	
	public MusicDataPack getMusicData() {
		return musicData;
	}
	
	public void beginBuild() {
		if (!building) {
			index = 0;
			building = true;
		} else {
			throw new IllegalStateException("Excuse me, but your already building...");
		}
	}
	
	public void endBuild() {
		if (building) {
			index = 0;
			building = false;
		} else {
			throw new IllegalStateException("Nothings being built...");
		}
	}
	
	public MapWindowData getCurrentWindowBasedOnIndex() {
		if (index != musicData.getPlaybackIndexPosition()) {
			index = musicData.getPlaybackIndexPosition();
			if (index < spawnList.length) {
				return spawnList[index];
			}
		}
		return null;
	}
	
	public int getIndex() {
		return index;
	}
}
