package zero1hd.polyjet.ui.stages;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Pool;
import com.badlogic.gdx.utils.Pools;
import com.badlogic.gdx.utils.viewport.FitViewport;

import zero1hd.polyjet.Polyjet;
import zero1hd.polyjet.controls.KeyMap;
import zero1hd.polyjet.entity.BarBeat;
import zero1hd.polyjet.entity.PolyJetEntity;
import zero1hd.polyjet.entity.Projectile;


public class GamePlayArea extends Stage {
	public PolyJetEntity polyJet;
	float timeCounter;
	
	//Required to know when to spawn entities on screen.
	int audioIndex;
	
	private float health = 50;
	private float maxHealth = 100;
	private float yTeleport = Polyjet.GAME_AREA_HEIGHT/2;
	private int score;
	
	private final Pool<Projectile> projectilePool = Pools.get(Projectile.class, 50);
	private final Pool<BarBeat> barBeatPool = Pools.get(BarBeat.class);
	
	Group projectile;
	
	Group foes;
	
	Texture basicLaserTexture;
	public GamePlayArea(Polyjet core) {
		super(new FitViewport(Polyjet.GAME_AREA_WIDTH, Polyjet.GAME_AREA_HEIGHT));
		
		Pixmap basicLaser = new Pixmap(2, 32, Format.RGBA8888);
		basicLaser.setColor(0f, 0.4f, 1f, 1f);
		basicLaser.fill();
		
		basicLaserTexture = new Texture(basicLaser);
		basicLaser.dispose();
		
		getCamera().far = 32f;
		getCamera().near = 0f;
		
		Gdx.app.debug("Camera position (x,y,z)", getCamera().position.x + " " + getCamera().position.y + " " + getCamera().position.z);
		getCamera().update();
		
		polyJet = new PolyJetEntity(core, 54, "standard");
		addActor(polyJet);
		
		projectile = new Group();
		projectile.setName("basic Projectiles");
		addActor(projectile);
		
		foes = new Group();
		addActor(foes);
	}
	
	public void init() {
		health = 50;
		maxHealth = 100;
	}
	@Override
	public void act(float delta) {
		timeCounter+=delta;
		
		for (int projectileID = 0; projectileID < projectile.getChildren().size; projectileID++) {
			Projectile currentProjectile = (Projectile) projectile.getChildren().get(projectileID);
			if (currentProjectile.getHitPoints() <= 0) {
				currentProjectile.printDebug();
				currentProjectile.remove();
				projectilePool.free(currentProjectile);
			}
		}
		
		for (int barBeatID = 0; barBeatID < foes.getChildren().size; barBeatID++) {
			BarBeat currentBarBeat = (BarBeat) foes.getChildren().get(barBeatID);
			if (currentBarBeat.getHitPoints() <= 0) {
				currentBarBeat.remove();
				barBeatPool.free(currentBarBeat);
			}
		}
		
		health -= 2*delta;
		
		if (health <= 0) {
			health = 0;
		}
		
		if (polyJet.getX() <= 1) {
			polyJet.moveLeft = false;
			polyJet.setX(1f);
		}
		
		if (polyJet.getX() >= Polyjet.GAME_AREA_WIDTH-1-polyJet.getWidth()) {
			polyJet.moveRight = false;
			polyJet.setX(Polyjet.GAME_AREA_WIDTH-1f-polyJet.getWidth());
		}
		
		if (polyJet.getY() >= Polyjet.GAME_AREA_HEIGHT - 1 - polyJet.getHeight()) {
			polyJet.moveUp = false;
			polyJet.setY(Polyjet.GAME_AREA_HEIGHT - 1 - polyJet.getHeight());
		}
		
		if (polyJet.getY() <= 1) {
			polyJet.moveDown = false;
			polyJet.setY(1f);
		}
		super.act(delta);
	}
	
	public void setAudioIndex(int audioIndex) {
		this.audioIndex = audioIndex;
	}
	public void addScore (int score) {
		this.score += score;
		health += score;
		if (health > maxHealth) {
			health = maxHealth;
		}
	}

	public void spawnProjectile(int ID) {
		switch (ID) {
		case 0:
			Projectile currentProjectile = projectilePool.obtain();
			currentProjectile.initiate(
					polyJet.getX()+polyJet.getWidth()/2-(0.125f)/2,
					polyJet.getY()+polyJet.getHeight()+0.5f,
					100,
					1,
					basicLaserTexture, 0.125f, 2f);
			projectile.addActor(currentProjectile);
			
			health -= 5;
			break;
		}
	}
	
	
	@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;
		}
		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.shoot) {
			spawnProjectile(0);
		}
		return false;
	}

	public int getScore() {
		return score;
	}
	
	public float getHealth() {
		return health;
	}
	
	public float getMaxHealth() {
		return maxHealth;
	}
	public float getyTeleport() {
		return yTeleport;
	}
}
