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

import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.entities.EntityMaster;
import java.util.EnumSet;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.ai.goal.Goal;

/**
 * AI Goal for EntityMaster to observe their pet player.
 *
 * In OBSERVING state, the Master watches the player intently.
 * This state prevents escape attempts from succeeding.
 *
 * The Master periodically enters DISTRACTED state, creating
 * windows where the player can attempt to struggle free.
 */
public class MasterObservePlayerGoal extends Goal {

    private final EntityMaster master;

    /** How long to observe before potentially doing something else (ticks) */
    private static final int OBSERVE_DURATION = 200; // 10 seconds

    /** Chance to transition to OBSERVING each tick when FOLLOWING */
    private static final float OBSERVE_CHANCE = 0.005f; // ~10% per 10 seconds

    private int observeTimer = 0;

    public MasterObservePlayerGoal(EntityMaster master) {
        this.master = master;
        this.setFlags(EnumSet.of(Goal.Flag.LOOK));
    }

    @Override
    public boolean canUse() {
        // Must have a pet
        if (!master.hasPet()) {
            return false;
        }

        MasterState state = master.getStateManager().getCurrentState();

        // If already observing, continue
        if (state == MasterState.OBSERVING) {
            return true;
        }

        // Random chance to start observing when following
        if (state == MasterState.FOLLOWING) {
            return master.getRandom().nextFloat() < OBSERVE_CHANCE;
        }

        return false;
    }

    @Override
    public boolean canContinueToUse() {
        if (!master.hasPet()) {
            return false;
        }

        ServerPlayer pet = master.getPetPlayer();
        if (pet == null || !pet.isAlive()) {
            return false;
        }

        // Continue observing until timer expires
        return (
            observeTimer < OBSERVE_DURATION &&
            master.getStateManager().getCurrentState() == MasterState.OBSERVING
        );
    }

    @Override
    public void start() {
        this.observeTimer = 0;
        master.setMasterState(MasterState.OBSERVING);

        TiedUpMod.LOGGER.debug(
            "[MasterObservePlayerGoal] {} started observing pet",
            master.getNpcName()
        );
    }

    @Override
    public void stop() {
        this.observeTimer = 0;

        // Return to following if still has pet
        if (
            master.hasPet() &&
            master.getStateManager().getCurrentState() == MasterState.OBSERVING
        ) {
            master.setMasterState(MasterState.FOLLOWING);
        }

        TiedUpMod.LOGGER.debug(
            "[MasterObservePlayerGoal] {} stopped observing",
            master.getNpcName()
        );
    }

    @Override
    public void tick() {
        ServerPlayer pet = master.getPetPlayer();
        if (pet == null) return;

        // Stare at pet intensely
        master.getLookControl().setLookAt(pet, 180.0F, 180.0F);

        observeTimer++;
    }
}
