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

import com.tiedup.remake.entities.EntityLaborGuard;
import com.tiedup.remake.items.ModItems;
import com.tiedup.remake.state.IRestrainable;
import com.tiedup.remake.util.KidnappedHelper;
import java.util.EnumSet;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.item.ItemStack;

/**
 * Guard walks up to prisoner and whips them when they're being lazy.
 *
 * Triggered by GuardMonitorGoal setting needsWhip flag on the guard.
 * Uses MOVE+LOOK flags so it interrupts FollowPrisoner to walk TO the prisoner.
 */
public class GuardWhipGoal extends Goal {

    private static final double WHIP_REACH = 2.5;
    private static final int MAX_DURATION = 100; // 5 sec to reach prisoner

    private final EntityLaborGuard guard;
    private ServerPlayer target;
    private int ticksActive;

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

    @Override
    public boolean canUse() {
        if (!guard.needsWhip()) return false;
        ServerPlayer prisoner = guard.getPrisoner();
        return prisoner != null && prisoner.isAlive();
    }

    @Override
    public boolean canContinueToUse() {
        return (
            guard.needsWhip() &&
            target != null &&
            target.isAlive() &&
            ticksActive < MAX_DURATION
        );
    }

    @Override
    public void start() {
        this.target = guard.getPrisoner();
        this.ticksActive = 0;

        // Equip whip visually
        guard.setItemInHand(
            InteractionHand.MAIN_HAND,
            new ItemStack(ModItems.WHIP.get())
        );
    }

    @Override
    public void tick() {
        ticksActive++;

        if (target == null) return;

        // Look at prisoner
        guard.getLookControl().setLookAt(target, 30.0f, 30.0f);

        double distance = guard.distanceTo(target);

        if (distance <= WHIP_REACH) {
            // Close enough — whip!
            performWhip();
            guard.setNeedsWhip(false);
            return;
        }

        // Navigate to prisoner
        if (ticksActive % 10 == 0) {
            guard.getNavigation().moveTo(target, 1.2);
        }
    }

    @Override
    public void stop() {
        guard.setNeedsWhip(false);
        guard.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY);
        guard.getNavigation().stop();
        target = null;
    }

    private void performWhip() {
        if (target == null) return;

        // Swing arm animation
        guard.swing(InteractionHand.MAIN_HAND);

        // Shock the prisoner
        IRestrainable cap = KidnappedHelper.getKidnappedState(target);
        if (cap != null) {
            cap.shockKidnapped("Get to work!", 1.5f);
        }

        // Small knockback toward the guard
        target.knockback(
            0.3,
            guard.getX() - target.getX(),
            guard.getZ() - target.getZ()
        );

        // Guard speaks
        guard.guardSay(target, "guard.labor.whip", "Move it!");

        // Remove whip from hand after use
        guard.setItemInHand(InteractionHand.MAIN_HAND, ItemStack.EMPTY);
    }
}
