package zero1hd.rhythmbullet.stages;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.FitViewport;

import zero1hd.rhythmbullet.RhythmBullet;
import zero1hd.rhythmbullet.audio.map.EntitySpawnInfo;
import zero1hd.rhythmbullet.audio.map.GamePlayMap;
import zero1hd.rhythmbullet.audio.map.MapWindowData;
import zero1hd.rhythmbullet.controls.KeyMap;
import zero1hd.rhythmbullet.entity.CollisionDetector;
import zero1hd.rhythmbullet.entity.Entity;
import zero1hd.rhythmbullet.entity.EntityManager;
import zero1hd.rhythmbullet.entity.ally.Laser;
import zero1hd.rhythmbullet.entity.ally.PolyjetEntity;
import zero1hd.rhythmbullet.entity.coordinator.CoordinatorManager;
import zero1hd.rhythmbullet.util.ScoreManager;


public class GamePlayArea extends Stage {
	public PolyjetEntity polyjet;
	private GamePlayMap audioMap;
	
	public CoordinatorManager cm;
	public EntityManager em;
	private CollisionDetector collisionDetector;
	
	public ScoreManager score = new ScoreManager();
	
	public GamePlayArea(AssetManager assetManager, Preferences prefs) {
		super(new FitViewport(RhythmBullet.GAME_AREA_WIDTH, RhythmBullet.GAME_AREA_HEIGHT));
		Gdx.app.debug("Game Area", "new area created");
		
		polyjet = new PolyjetEntity(assetManager, 25f, 25f, 100, "standard");
		em = new EntityManager(assetManager, prefs, this);
		cm = new CoordinatorManager(em);
		
		collisionDetector = new CollisionDetector(em.activeEnemies, em.activeAllies, assetManager, prefs);
		em.activeAllies.add(polyjet);
		addActor(polyjet);
	}
	
	public void setAudioMap(GamePlayMap audioMap) {
		this.audioMap = audioMap;
	}
	
	public GamePlayMap getAudioMap() {
		return audioMap;
	}
	
	@Override
	public void draw() {
		super.draw();
	}
	
	@Override
	public void act(float delta) {
		MapWindowData mwd;
		if (audioMap != null && audioMap.getMusicData().getPlaybackMusic().isPlaying()) {
			audioMap.getMusicData().playbackIndexUpdate();
			if ((mwd = audioMap.getCurrentWindowBasedOnIndex()) != null) {
				EntitySpawnInfo[] currentSpawnInfo = mwd.getArray();
				if (currentSpawnInfo != null) {
					for (int i = 0; i < currentSpawnInfo.length; i++) {
						Entity entity = currentSpawnInfo[i].getEntityToSpawn().buildEntity();
						if (currentSpawnInfo[i].getEntityCoordinator() != null) {
							entity.setCoordinator(currentSpawnInfo[i].getEntityCoordinator().buildCoordinator());
						}

						entity.init(currentSpawnInfo[i].parameters);
						addActor(entity);
					}
				}
			}
		}

		collisionDetector.collisionCheck();
		score.addScore(collisionDetector.getAmassedPoints());
		
		super.act(delta);
	}
	
	@Override
	public boolean keyDown(int keycode) {
		if (keycode == KeyMap.left) {
			polyjet.moveLeft = true;
		}
		if (keycode == KeyMap.right) {
			polyjet.moveRight = true;
		}
		
		if (keycode == KeyMap.up) {
			polyjet.moveUp = true;
		}
		
		if (keycode == KeyMap.down) {        
			polyjet.moveDown = true;
		}
		
		if (keycode == KeyMap.accelerate) {
			polyjet.accelerate = true;
		}
		return false;
	}
	
	@Override
	public boolean keyUp(int keycode) {
		if (keycode == KeyMap.left) {
			polyjet.moveLeft = false;
		}
		if (keycode == KeyMap.right) {
			polyjet.moveRight = false;
		}
		if (keycode == KeyMap.up) {
			polyjet.moveUp = false;
		}
		if (keycode == KeyMap.down) {
			polyjet.moveDown = false;
		}
		
		if (keycode == KeyMap.accelerate) {
			polyjet.accelerate = false;
		}
		
		if (keycode == KeyMap.shoot) {
			Laser laser	= em.laser.buildEntity();
			laser.init(polyjet.getX() + polyjet.getWidth()/2f, polyjet.getY() + polyjet.getHeight()+1f, 60f);
			addActor(laser);
		}
		return false;
	}

	public PolyjetEntity getPolyjetEntity() {
		return polyjet;
	}
	
	@Override
	public void dispose() {
		super.dispose();
	}
	
	public CollisionDetector getCollisionDetector() {
		return collisionDetector;
	}
}
