package eu.oschl.gui.actions;

import eu.oschl.gui.Output;
import eu.oschl.textadventure.Game;
import javafx.scene.paint.Color;

public class Slay implements Action {
    private final Game game;

    public Slay(Game game) {
        this.game = game;
    }

    @Override
    public String getId() {
        return "slay";
    }

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

    @Override
    public String getDescription() {
        return "fight an enemy";
    }

    @Override
    public void execute(String[] args) {
        if (
                this.game.getCurrentRoom().getEnemy().isEmpty() ||
                !this.game.getCurrentRoom().getEnemy().get().getName().equalsIgnoreCase(String.join(" ", args))
        ) {
            Output.print("This enemy is not here.", Color.RED);
            return;
        }

        var enemy = this.game.getCurrentRoom().getEnemy().get();

        if (enemy.strength > 0 && game.getInventory().getWeapon().isEmpty()) {
            Output.print(enemy.getName() + " is too strong. A weapon is required.", Color.RED);
            return;
        }

        if (enemy.strength > 0 && enemy.strength > game.getInventory().getWeapon().get().getDamage()) {
            Output.print(enemy.getName() + " is too strong. A stronger weapon is required.", Color.RED);
            return;
        }

        var result = enemy.kill();

        if (result) {
            if (game.getInventory().getWeapon().isPresent()) {
                Output.print(game.getInventory().getWeapon().get().getAttackText());
            }

            if (enemy.getKillText().isPresent()) {
                Output.printLine();
                Output.print(enemy.getKillText().get());
                Output.printLine();
            }

            Output.printLine();
            Output.print(enemy.getName(), Color.RED);
            Output.print(" lies murdered on the floor.");
        } else {
            Output.print("It's impossible to kill ", Color.RED);
            Output.print(enemy.getName(), Color.RED);
            Output.print(".", Color.RED);
        }
    }
}
