package model.objects;

import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.awt.image.BufferedImage;

public class Player extends DrawObject {

	private int score, health = 10;

	private long lastMovement;
	private int lastDirection, frame;
	private Double beginlocation;
	private BufferedImage spriteimage;
	private String name;

	public Player(BufferedImage image, int x, int y, String name) {
		super(image.getSubimage(38, 0, 40, 54));
		super.setPosition(x, y);
		beginlocation = new Point2D.Double(x, y);
		this.name = name;
		spriteimage = image;
	}

	@Override
	public void update() {
		if (System.currentTimeMillis() - lastMovement >= 100) { // If the player
																// didn't move
																// for 200ms,
																// look forward
			image = spriteimage.getSubimage(38, 0, 40, 54);
			frame = 0;
		} else { // Look into the right walking direction
			frame++;
			frame %= 1000;
			image = spriteimage.getSubimage(2 + (((frame / 4) % 4) * 38), 54 * lastDirection, 38, 50);
		}
	}

	public void reset() {
		setPosition(beginlocation);
	}

	// ** Getters and Setters **//

	public String getName() {
		return name;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}

	public int getHealth() {
		return health;
	}

	public void setHealth(int health) {
		this.health = health;
	}

	@Override
	public void setX(int x) {
		if (x < getX())
			lastDirection = 1;
		else if (x > getX())
			lastDirection = 2;
		if (x != getX())
			lastMovement = System.currentTimeMillis();
		super.setX(x);
	}
}
