package cz.jzitnik.game.mobs.services.zombie;

import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.EntityHurtAnimationHandler;
import cz.jzitnik.game.annotations.EntityKillHandler;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.mobs.*;
import cz.jzitnik.game.sprites.Zombie;

import java.util.*;

import static cz.jzitnik.game.sprites.Zombie.ZombieState.*;

@EntityHurtAnimationHandler("zombie")
@EntityKillHandler("zombie")
public class ZombieHurtKillLogic implements EntityHurtAnimationChanger, EntityKillInterface {
    private final Random random = new Random();

    public Zombie.ZombieState setHurtAnimation(boolean hurt, Enum current) {
        if (hurt) {
            return switch (current) {
                case TOP, TOP_HURT -> TOP_HURT;
                case BOTTOM, BOTTOM_HURT -> BOTTOM_HURT;
                default -> throw new IllegalStateException("Unexpected state: " + current);
            };
        }

        return switch (current) {
            case TOP, TOP_HURT -> TOP;
            case BOTTOM, BOTTOM_HURT -> BOTTOM;
            default -> throw new IllegalStateException("Unexpected state: " + current);
        };
    }

    @Override
    public void killed(Game game, Block mob, int x, int y) {
        int rottenFlesh = random.nextInt(3) + 1;

        boolean isTop = mob.getSpriteState().isPresent() && (mob.getSpriteState().get().equals(TOP_HURT) || mob.getSpriteState().get().equals(TOP));
        int newY = isTop ? y + 1 : y - 1;

        game.getWorld()[newY][x].remove(mob.getLinkedMobTexture());

        // Rotten flesh is currently not an item. So this is commented out
        // TODO: Add rotten flesh to the game
        //InventoryItem drop = new InventoryItem(rottenFlesh, ItemBlockSupplier.getItem("rotten_flesh"));
        //game.getInventory().addItem(drop);
    }
}