package edu.wisc.cs.games10.chrononaut;


import processing.core.*;

public class BranderBotEnemy extends PatrolEnemy {
	
	private float bbspeed = 0.001f; // 1 tile / second
	private PImage bbImage[];
	private PImage bbImg;
	//private float animationTime;     future use?
	private int bbIndex;
	
	private float animationTime;
	private boolean hasCharged;
	private boolean isCharging;
	
	
	public BranderBotEnemy(Level _level, Vector2D _loc, PApplet canvas) {
		bbImage = new PImage[2];
		bbImage[0] = getImage("enemies/brander_bot/02.png", canvas);
		bbImage[1] = getImage("enemies/brander_bot/01.png", canvas);
		
		// walk animation
		bbImg = bbImage[0];
		bbIndex = 0;
		animationTime = 0;
		
		loc = _loc.copy();
		level = _level;
		
		tileWidth = bbImg.width / 32.0f;
		tileHeight = bbImg.height / 32.0f;
	}

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

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

	@Override
	public void update(float time, Era era) {
		super.update(time, era);
		
		// set velocity
		if (hasChangedDirections) {
			if (vel.x < 0) vel.x = -bbspeed;
			else vel.x = bbspeed;
			hasCharged = false;
			isCharging = false;
		}
		if (!hasCharged && isCharging) {
			vel.x *= 3;
			hasCharged = true;
		}
		
		// walk animation
		animationTime += time;
		if(animationTime > 250)
		{
			animationTime = 0;
			bbImg = bbImage[++bbIndex % 2];
		}
	}
	
	@Override
	public void attack(Era era, Player p, float time) {
		// charge whenever can see Lamark
		isCharging = (vel.x < 0 && p.loc.x < loc.x) || (vel.x > 0 && p.loc.x > loc.x);
	}
	
	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 = 58;
			thirdVertexTexCoord = 58;
			fourthVertexTexCoord = 0;
		}
		else
		{
			firstVertexTexCoord = 58;
			secondVertexTexCoord = 0;
			thirdVertexTexCoord = 0;
			fourthVertexTexCoord = 58;
		}
		
		canvas.beginShape(PConstants.QUADS);
			canvas.texture(bbImg);
			canvas.vertex(0, 0, firstVertexTexCoord, 0);
			canvas.vertex(1.8125f, 0, secondVertexTexCoord,0);
			canvas.vertex(1.8125f, 1.8125f, thirdVertexTexCoord,58);
			canvas.vertex(0, 1.8125f, fourthVertexTexCoord,58);
		canvas.endShape();
		
		canvas.popMatrix();
	}
	
	public void kill() {
		level.killEnemy(this);
	}

}
