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

import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.CustomLogic;
import cz.jzitnik.game.entities.Player;
import cz.jzitnik.game.logic.CustomLogicInterface;
import cz.jzitnik.tui.ScreenRenderer;

@CustomLogic
public class Burning implements CustomLogicInterface {
    @Override
    public void nextIteration(Game game, ScreenRenderer screenRenderer) {
        var world = game.getWorld();
        int[] data = game.getPlayerCords();
        int x = data[0];
        int y = data[1];
        Player player = game.getPlayer();

        var blocks = world[y][x];

        if (blocks.stream().anyMatch(i -> i.getClass().isAnnotationPresent(cz.jzitnik.game.annotations.Burning.class))) {
            player.setBurning(true);
            player.setBurningTimeout(0);
        } else {
            boolean prev = player.isBurning();
            if (prev) {
                player.setBurning(false);
                player.setBurningTimeout(5);
            } else if (player.getBurningTimeout() != 0) {
                player.setBurningTimeout(player.getBurningTimeout() - 1);
            }
        }

        if (player.isBurning() || player.getBurningTimeout() != 0) {
            player.dealDamage(game, screenRenderer);
            player.setBurningState(true);
        } else {
            player.setBurningState(false);
        }
    }
}
