package com.tiedup.remake.entities.ai.guard;

import com.tiedup.remake.entities.EntityLaborGuard;
import com.tiedup.remake.items.ItemTaser;
import com.tiedup.remake.items.ModItems;
import java.util.EnumSet;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

/**
 * AI Goal: Guard fights back when attacked by a player.
 *
 * Adapted from KidnapperFightBackGoal for EntityDamsel-based guard.
 * Differences from KidnapperFightBackGoal:
 * - No hasCaptives()/isWaitingForJobToBeCompleted() check (always active)
 * - No isMaster() check (guard fights all attackers)
 * - Does NOT become hostile toward the monitored prisoner
 *   (prisoner attacks are handled separately in EntityLaborGuard.hurt())
 */
public class GuardFightBackGoal extends Goal {

    private final EntityLaborGuard guard;
    private LivingEntity attacker;
    private int aggressionTimer;

    private static final int AGGRESSION_DURATION = 200; // 10 seconds
    private int attackCooldown = 0;

    public GuardFightBackGoal(EntityLaborGuard guard) {
        this.guard = guard;
        this.setFlags(EnumSet.of(Goal.Flag.MOVE, Goal.Flag.LOOK));
    }

    @Override
    public boolean canUse() {
        if (guard.isTiedUp()) {
            return false;
        }

        LivingEntity lastAttacker = guard.getLastAttacker();
        if (lastAttacker == null || !(lastAttacker instanceof Player player)) {
            return false;
        }

        // Don't fight the prisoner we're guarding (handled via shock in hurt())
        if (
            guard.getPrisonerUUID() != null &&
            player.getUUID().equals(guard.getPrisonerUUID())
        ) {
            return false;
        }

        this.attacker = lastAttacker;
        return true;
    }

    @Override
    public void start() {
        // Equip taser
        guard.setItemInHand(
            InteractionHand.MAIN_HAND,
            new ItemStack(ModItems.TASER.get())
        );

        this.aggressionTimer = AGGRESSION_DURATION;
        this.attackCooldown = 0;
    }

    @Override
    public void tick() {
        aggressionTimer--;
        if (attackCooldown > 0) {
            attackCooldown--;
        }

        if (attacker != null && attacker.isAlive()) {
            // Look at attacker
            guard.getLookControl().setLookAt(attacker);

            // Pursue
            guard.getNavigation().moveTo(attacker, 1.3);

            // Attack if close enough
            if (guard.distanceTo(attacker) < 2.0 && attackCooldown <= 0) {
                guard.swing(InteractionHand.MAIN_HAND);

                boolean damaged = guard.doHurtTarget(attacker);

                if (damaged) {
                    ItemStack heldItem = guard.getItemInHand(
                        InteractionHand.MAIN_HAND
                    );
                    if (heldItem.getItem() instanceof ItemTaser taserItem) {
                        taserItem.hurtEnemy(heldItem, attacker, guard);
                    }
                }

                attackCooldown = 20;
            }
        }
    }

    @Override
    public boolean canContinueToUse() {
        if (aggressionTimer <= 0) return false;
        if (attacker == null || !attacker.isAlive()) return false;
        if (guard.distanceTo(attacker) > 30) return false;
        return true;
    }

    @Override
    public void stop() {
        guard.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY);
        this.attacker = null;
        this.aggressionTimer = 0;
    }
}
