package inf101.v18.rogue101.examples;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import inf101.v18.gfx.gfxmode.ITurtle;
import inf101.v18.grid.GridDirection;
import inf101.v18.rogue101.game.IGame;
import inf101.v18.rogue101.objects.IItem;
import inf101.v18.rogue101.objects.INonPlayer;
import inf101.v18.rogue101.shared.NPC;
import inf101.v18.rogue101.states.Attack;

public class Rabbit implements INonPlayer {
	private int food = 0;
	private int hp = getMaxHealth();
	private static final List<Class<?>> validItems = new ArrayList<>();
	static {
		validItems.add(Carrot.class);
	}

	@Override
	public void doTurn(IGame game) {
		if (food == 0) {
			hp--;
		} else {
			food--;
		}
		if (hp < 1) {
			return;
		}

		if (NPC.tryAttack(game, 1, Attack.MELEE)) {
			return;
		}

		for (IItem item : game.getLocalItems()) {
			if (item instanceof Carrot) {
				System.out.println("found carrot!");
				int eaten = item.handleDamage(game, this, getDamage());
				if (eaten > 0) {
					System.out.println("ate carrot worth " + eaten + "!");
					food += eaten;
					game.displayMessage("You hear a faint crunching (" + getName() + " eats " + item.getArticle() + " " + item.getName() + ")");
					return;
				}
			}
		}

		if (NPC.trackItem(game, validItems)) {
			return;
		}

		List<GridDirection> possibleMoves = game.getPossibleMoves();
		if (!possibleMoves.isEmpty()) {
			Collections.shuffle(possibleMoves);
			game.move(possibleMoves.get(0));
		}
	}

	@Override
	public boolean draw(ITurtle painter, double w, double h) {
		return false;
	}

	@Override
	public int getAttack() {
		return 10;
	}

	@Override
	public int getCurrentHealth() {
		return hp;
	}

	@Override
	public int getDamage() {
		return 5;
	}

	@Override
	public int getDefence() {
		return 10;
	}

	@Override
	public int getMaxHealth() {
		return 30;
	}

	@Override
	public String getName() {
		return "Rabbit";
	}

	@Override
	public int getSize() {
		return 4;
	}

	@Override
	public int getVision() {
		return 4;
	}

	@Override
	public IItem getItem(Class<?> type) {
		return null;
	}

	@Override
	public String getSymbol() {
		return hp > 0 ? "R" : "¤";
	}

	@Override
	public int handleDamage(IGame game, IItem source, int amount) {
		hp -= amount;
		return amount;
	}
}
