package edu.wisc.cs.games10.chrononaut;

import processing.core.*;

public class SpectreEnemy extends PatrolEnemy {
	
	private PImage spectreImage;
	
	private float lastTimeFired;
	private PImage testBullet;
	
	boolean movingup;
	int floatHeight;
	int pauseBetween;
	
	public SpectreEnemy(Level _level, Vector2D _loc, PApplet canvas) {
		spectreImage = getImage("enemies/spectre/01.png", canvas);
		testBullet = getImage("bullets/longbearsBullet.png", canvas);
		
		movingup = true;
		floatHeight = 0;
		pauseBetween = 0;
		
		loc = _loc.copy();
		level = _level;
		
		tileWidth = ((float)spectreImage.width)/32f;
		tileHeight = ((float)spectreImage.height)/32f;
	}

	@Override
	public float getTileHeight() {
		return tileHeight;
	}

	@Override
	public float getTileWidth() {
		return tileWidth;
	}

	public void update(float time, Era era) {
		super.update(time, era);
		
		// float animation
		if(movingup) {
			if(floatHeight <6) {
				floatHeight++;
			} else {
				movingup = false;
			}
		}else {
			if(floatHeight > 0) {
				floatHeight --;
			}
			else {
				movingup = true;
			}
		}
	
	}
	
	public void attack(Era era, Player hero, float time) {
		lastTimeFired += time;
		if (lastTimeFired < 1333) {
			return;
		}
		int tiley = (int) Math.floor(loc.y + tileHeight);
		int playertiley = (int) Math.floor(hero.loc.y + hero.getTileHeight());
		
		if(tiley == playertiley || (tiley == playertiley-1) || (tiley == playertiley+1)) {
			int tilex = (int) Math.floor(loc.x);
			int playertilex = (int) Math.floor(hero.loc.x);
			
			if( (vel.x < 0) && (playertilex < tilex) && (playertilex - tilex > -15) ) {
				Bullet b = new Bullet(5000f, new Vector2D(loc.x-.5f, loc.y - (floatHeight)/32f + 23f/32f), new Vector2D(-.1f, 0f), testBullet);
				level.addBullet(b, era);
				lastTimeFired = 0;
				//fire a bullet to the left
			} else if( (vel.x > 0) && (playertilex > tilex) && (playertilex - tilex < 15) ) {
				Bullet b = new Bullet(5000f, new Vector2D(loc.x+tileWidth, loc.y - (floatHeight)/32f + 23f/32f), new Vector2D(.1f, 0f), testBullet);
				level.addBullet(b, era);
				lastTimeFired = 0;
				//fire a bullet to the right
			} 
		}
	}
	
	public void render(PApplet canvas) {
		// draw enemy
		// TODO: get image based on animation code
		canvas.pushMatrix();
		canvas.translate(loc.x, loc.y - (floatHeight)/32f);
		
		int firstVertexTexCoord;
		int secondVertexTexCoord;
		int thirdVertexTexCoord;
		int fourthVertexTexCoord;
		
		if(vel.x > 0)
		{
			firstVertexTexCoord = 0;
			secondVertexTexCoord = spectreImage.width;
			thirdVertexTexCoord = spectreImage.width;
			fourthVertexTexCoord = 0;
		}
		else
		{
			firstVertexTexCoord = spectreImage.width;
			secondVertexTexCoord = 0;
			thirdVertexTexCoord = 0;
			fourthVertexTexCoord = spectreImage.width;
		}
		
		canvas.beginShape(PConstants.QUADS);
			canvas.texture(spectreImage);
			canvas.vertex(0, 0, firstVertexTexCoord, 0);
			canvas.vertex(tileWidth, 0, secondVertexTexCoord,0);
			canvas.vertex(tileWidth, tileHeight, thirdVertexTexCoord,spectreImage.height);
			canvas.vertex(0, tileHeight, fourthVertexTexCoord,spectreImage.height);
		canvas.endShape();
		
		canvas.popMatrix();
	}
	
	public void kill() {
		level.killEnemy(this);
	}
}
