package cz.jzitnik.game.logic.services.burning;

import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.CustomLogic;
import cz.jzitnik.game.annotations.LightBurningMob;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.game.entities.items.Item;
import cz.jzitnik.game.logic.CustomLogicInterface;
import cz.jzitnik.tui.ScreenRenderer;

@CustomLogic
public class LightBurningMobLogic implements CustomLogicInterface {
    private static final int RADIUS = 30;

    @Override
    public void nextIteration(Game game, ScreenRenderer screenRenderer) {
        if (!game.isNight()) {
            return;
        }

        int[] data = game.getPlayerCords();
        var world = game.getWorld();
        int playerX = data[0];
        int playerY = data[1];

        int startX = Math.max(0, playerX - RADIUS);
        int startY = Math.max(0, playerY - RADIUS);
        int endX = Math.min(world[0].length - 1, playerX + RADIUS);
        int endY = Math.min(world.length - 1, playerY + RADIUS);

        for (int y = startY; y <= endY; y++) {
            for (int x = startX; x <= endX; x++) {
                var blocks = world[y][x];

                for (Block block : blocks) {
                    if (block.getClass().isAnnotationPresent(LightBurningMob.class)) {
                        int dealDamage = game.getInventory().getItemInHand().map(Item::getDealDamage).orElse(1);
                        if (block.getHp() - dealDamage <= 0) {
                            // Mob is killed
                            world[y][x].remove(block);
                        } else {
                            block.decreaseHp(dealDamage);
                            block.setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
                                    .setHurtAnimation(true, block.getSpriteState().get()));
                            if (block.getLinkedMobTexture() != null) {
                                block.getLinkedMobTexture().setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
                                        .setHurtAnimation(true, block.getLinkedMobTexture().getSpriteState().get()));
                            }
                        }

                        new Thread(() -> {
                            try {
                                Thread.sleep(500);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }

                            block.setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
                                    .setHurtAnimation(false, block.getSpriteState().get()));

                            if (block.getLinkedMobTexture() != null) {
                                block.getLinkedMobTexture().setSpriteState(game.getGameStates().dependencies.entityHurtAnimation.get(block.getBlockId())
                                        .setHurtAnimation(false, block.getLinkedMobTexture().getSpriteState().get()));
                            }
                        }).start();
                    }
                }
            }
        }
    }
}
