package LasTres::PJAction::GolpearArbolCentralTribuDeLaLima;

use v5.36.0;
use strict;
use warnings;
use utf8;

use feature 'signatures';

use Moo;

with 'LasTres::PJAction';

## DO NOT EXTEND NOT SUPPORTED.

sub name {
    return 'Golpear el árbol.';
}

sub identifier {
    return 'golpear_arbol_central_tribu_de_la_lima';
}

sub is_disabled ( $self, $pj ) {
    if ($pj->health < 1) {
        return 1;
    }
    return 0;
}

sub disabled_reason ( $self, $pj ) {
    if ($pj->health < 1) {
        return "Estás debilitado.";    
    }
    return undef;
}

sub _chance_enemy ( $self, $pj ) {
    if ( $pj->level > 20 ) {
        return 0;
    }
    if ( $pj->level > 10 ) {
        return 25;
    }
    return 50;
}

sub callback ( $self, $pj ) {
    $pj = $pj->get_from_storage;
    my $damage_for_hitting_the_tree = int( $pj->health / 10 );
    my $current_health = int( $pj->health - $damage_for_hitting_the_tree );
    # We do not let the player die. That would be problematic out of combat.
    $pj->health($current_health);
    $pj->update;
    my $team = $pj->team;
    $team->append_log_line(
        [
            {
                text  => $pj->nick,
                color => 'green',
            },
            {
                    text => ' golpeó el árbol y se hizo '
                  . $damage_for_hitting_the_tree
                  . ' de daño. '
                  . $pj->health . '/'
                  . $pj->max_health . '.'
            }
        ]
    );
    if ($team->is_defeated) {
        return;
    }

    if ( LasTres::Util::rand_range_int( 0, 100 ) <= $self->_chance_enemy($pj) )
    {
        $self->_start_battle($pj);
        return;
    }
}

sub _start_battle ( $self, $pj ) {
    require LasTres::EnemyArea;
    require LasTres::Battle;
    require LasTres::Util;
    my $team   = $pj->team;
    $team->append_log_line([
        { 
            text => 'Una criatura que vive en el árbol se ha molestado, preparate para luchar...'
        }
    ]);

    my $battle = LasTres::Battle->start_battle_machine($team,
        [
            LasTres::EnemyArea->new(
                race  => 'conejo',
                nick  => 'Conejo territorial.',
                level => LasTres::Util::rand_range_int(1,3),
            )
        ]
    );
}
1;
