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

import com.tiedup.remake.entities.EntityKidnapper;
import java.util.EnumSet;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.goal.Goal;

/**
 * AI Goal for EntityKidnapper to find suitable targets.
 *
 *
 * This goal:
 * 1. Searches for nearby players and damsels within range
 * 2. Validates targets (not creative, not already enslaved, etc.)
 * 3. Sets the kidnapper's target when found
 *
 * Based on original EntityAILookForPlayer from 1.12.2
 */
public class KidnapperFindTargetGoal extends Goal {

    private final EntityKidnapper kidnapper;
    private final int searchRadius;
    private LivingEntity foundTarget;
    private int cooldown;

    /**
     * Create a new find target goal.
     *
     * @param kidnapper The kidnapper entity
     * @param searchRadius Maximum search radius in blocks
     */
    public KidnapperFindTargetGoal(
        EntityKidnapper kidnapper,
        int searchRadius
    ) {
        this.kidnapper = kidnapper;
        this.searchRadius = searchRadius;
        this.cooldown = 0;
        this.setFlags(EnumSet.of(Goal.Flag.TARGET));
    }

    @Override
    public boolean canUse() {
        // Cooldown between searches
        if (this.cooldown > 0) {
            this.cooldown--;
            return false;
        }

        // Don't search if:
        // - Already has a slave
        // - Already has a target
        // - Is tied up
        // - Waiting for job to be completed
        if (this.kidnapper.hasCaptives()) {
            return false;
        }
        if (this.kidnapper.getTarget() != null) {
            return false;
        }
        if (this.kidnapper.isTiedUp()) {
            return false;
        }
        if (this.kidnapper.isWaitingForJobToBeCompleted()) {
            return false;
        }

        // Find closest suitable target (player or damsel)
        this.foundTarget = this.kidnapper.getClosestSuitableTarget(
            this.searchRadius
        );

        if (this.foundTarget != null) {
            return true;
        }

        // Set cooldown before next search (20 ticks = 1 second)
        this.cooldown = 20;
        return false;
    }

    @Override
    public boolean canContinueToUse() {
        // Stop if:
        // - Target is null
        // - Kidnapper got tied up
        // - Kidnapper already has a slave
        // - Kidnapper waiting for job
        // - Target is no longer valid
        if (this.foundTarget == null) {
            return false;
        }
        if (this.kidnapper.isTiedUp()) {
            return false;
        }
        if (this.kidnapper.hasCaptives()) {
            return false;
        }
        if (this.kidnapper.isWaitingForJobToBeCompleted()) {
            return false;
        }
        if (!this.kidnapper.isSuitableTarget(this.foundTarget)) {
            return false;
        }

        return true;
    }

    @Override
    public void start() {
        // Set the target on the kidnapper
        this.kidnapper.setTarget(this.foundTarget);
    }

    @Override
    public void stop() {
        this.foundTarget = null;
        // Don't clear kidnapper target here - let capture goal handle it
    }

    @Override
    public void tick() {
        // Keep looking at target
        if (this.foundTarget != null) {
            this.kidnapper.getLookControl().setLookAt(
                this.foundTarget,
                30.0F,
                30.0F
            );
        }
    }
}
