package edu.wisc.cs.games10.chrononaut;

import processing.core.*;

public class HatEnemy extends PatrolEnemy {
	
	private PImage hatImage[];
	private PImage hatImg;
	private float animationTime;
	private int hatIndex;
	
	private float lastTimeFired;
	private PImage testBullet;
	
	// size of the image in tiles (for drawing offset)
	private static final float imageTileHeight = 2;
	private static final float imageTileOffset = 17.0f / 32;
	
	public HatEnemy(Level _level, Vector2D _loc, PApplet canvas) {
		hatImage = new PImage[2];
		hatImage[0] = getImage("enemies/hat/01.png", canvas);
		hatImage[1] = getImage("enemies/hat/02.png", canvas);
		
		testBullet = getImage("bullets/bearsBullet.png", canvas);
		
		// walk animation
		hatImg = hatImage[0];
		hatIndex = 0;
		animationTime = 0;
		
		loc = _loc.copy();
		level = _level;
		
		tileWidth = 1;
		tileHeight = 50.0f / 32;
	}

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

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

	public void update(float time, Era era) {
		super.update(time, era);
		
		// walk animation
		animationTime += time;
		if(animationTime > 250)
		{
			animationTime = 0;
			hatImg = hatImage[++hatIndex % 2];
		}
	}
	
	public void attack(Era era, Player p, float time) {
		lastTimeFired += time;
		int tiley = (int) Math.floor(loc.y);
		int tilex = (int) Math.floor(loc.x);
		
		// don't fire if we can't (too soon or stuck)
		if (level.getTile(era, tilex, tiley) == Tile.SOLID ||
				(lastTimeFired < 2000)) {
			return;
		}
		
		if (Math.abs(loc.y - p.loc.y) < 2) {
			int playertilex = (int) Math.floor(p.loc.x);
			
			if( (vel.x < 0) && (playertilex < tilex) && (playertilex - tilex > -10) ) {
				Bullet b = new Bullet(5000f, new Vector2D(loc.x-.5f, loc.y + tileHeight -1.2f), 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 < 10) ) {
				Bullet b = new Bullet(5000f, new Vector2D(loc.x+1, loc.y + tileHeight -1.2f), 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);
		
		int firstVertexTexCoord;
		int secondVertexTexCoord;
		int thirdVertexTexCoord;
		int fourthVertexTexCoord;
		
		if(vel.x < 0)
		{
			firstVertexTexCoord = 0;
			secondVertexTexCoord = 32;
			thirdVertexTexCoord = 32;
			fourthVertexTexCoord = 0;
		}
		else
		{
			firstVertexTexCoord = 32;
			secondVertexTexCoord = 0;
			thirdVertexTexCoord = 0;
			fourthVertexTexCoord = 32;
		}
		
		canvas.beginShape(PConstants.QUADS);
			canvas.texture(hatImg);
			canvas.vertex(0, -imageTileOffset, firstVertexTexCoord, 0);
			canvas.vertex(tileWidth, -imageTileOffset, secondVertexTexCoord, 0);
			canvas.vertex(tileWidth, imageTileHeight - imageTileOffset, thirdVertexTexCoord, 64);
			canvas.vertex(0, imageTileHeight - imageTileOffset, fourthVertexTexCoord, 64);
		canvas.endShape();
		
		canvas.popMatrix();
	}
	
	public void kill() {
		level.killEnemy(this);
	}
}
