/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.content.entity.npc.ai.goal;

import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.goal.Goal;
import top.r3944realms.eroticdungeongame.content.entity.npc.NPCServerPlayer;

import java.util.EnumSet;

public class NPCFollowGoal extends Goal {
    private final NPCServerPlayer npc;
    private LivingEntity target;
    private final double speedModifier;
    private final int followDistance;
    private final int stopDistance;
    public NPCFollowGoal(NPCServerPlayer npc, double speed, int followDist, int stopDist) {
        this.npc = npc;
        this.speedModifier = speed;
        this.followDistance = followDist;
        this.stopDistance = stopDist;
        this.setFlags(EnumSet.of(Flag.MOVE, Flag.LOOK));
    }
    @Override
    public boolean canUse() {
        this.target = npc.getTarget();
        return target != null && target.isAlive() && npc.distanceToSqr(target) > stopDistance;
    }
    @Override
    public boolean canContinueToUse() {
        return target != null && target.isAlive() && npc.distanceToSqr(target) > followDistance;
    }

    @Override
    public void start() {
        npc.getNavigation().moveTo(target, speedModifier);
    }

    @Override
    public void stop() {
        npc.setTarget(null);
    }

    @Override
    public void tick() {
        npc.getLookControl().setLookAt(target, 30.0f, 30.0f);
        if (npc.distanceToSqr(target) > stopDistance) {
            npc.getNavigation().moveTo(target, speedModifier*5);
        } else {
            npc.getNavigation().stop();
        }
    }
}
