/*
 *  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.navigation.pathfinder;

import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.core.BlockPos;
import net.minecraft.util.Mth;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.PathNavigationRegion;
import net.minecraft.world.level.pathfinder.BlockPathTypes;
import net.minecraft.world.level.pathfinder.Node;
import net.minecraft.world.level.pathfinder.Target;
import top.r3944realms.eroticdungeongame.content.entity.npc.NPCServerPlayer;

public abstract class NPCNodeEvaluator {
    protected PathNavigationRegion level;
    protected NPCServerPlayer npc;
    protected final Int2ObjectMap<Node> nodes = new Int2ObjectOpenHashMap<>();
    protected int entityWidth;
    protected int entityHeight;
    protected int entityDepth;
    protected boolean canPassDoors;
    protected boolean canOpenDoors;
    protected boolean canFloat;
    protected boolean canWalkOverFences;

    public void prepare(PathNavigationRegion level, NPCServerPlayer npc) {
        this.level = level;
        this.npc = npc;
        this.nodes.clear();
        this.entityWidth = Mth.floor(npc.getBbWidth() + 1.0F);
        this.entityHeight = Mth.floor(npc.getBbHeight() + 1.0F);
        this.entityDepth = Mth.floor(npc.getBbWidth() + 1.0F);
    }

    public void done() {
        this.level = null;
        this.npc = null;
    }

    protected Node getNode(BlockPos pos) {
        return this.getNode(pos.getX(), pos.getY(), pos.getZ());
    }

    /**
     * Returns a mapped point or creates and adds one
     */
    protected Node getNode(int x, int y, int z) {
        return this.nodes.computeIfAbsent(Node.createHash(x, y, z), (p_77332_) -> {
            return new Node(x, y, z);
        });
    }

    public abstract Node getStart();

    public abstract Target getGoal(double x, double y, double z);

    protected Target getTargetFromNode(Node node) {
        return new Target(node);
    }

    public abstract int getNeighbors(Node[] outputArray, Node node);

    public abstract BlockPathTypes getBlockPathType(BlockGetter level, int x, int y, int z, NPCServerPlayer npc);

    /**
     * Returns the node type at the specified postion taking the block below into account
     */
    public abstract BlockPathTypes getBlockPathType(BlockGetter level, int x, int y, int z);

    public void setCanPassDoors(boolean canEnterDoors) {
        this.canPassDoors = canEnterDoors;
    }

    public void setCanOpenDoors(boolean canOpenDoors) {
        this.canOpenDoors = canOpenDoors;
    }

    public void setCanFloat(boolean canFloat) {
        this.canFloat = canFloat;
    }

    public void setCanWalkOverFences(boolean canWalkOverFences) {
        this.canWalkOverFences = canWalkOverFences;
    }

    public boolean canPassDoors() {
        return this.canPassDoors;
    }

    public boolean canOpenDoors() {
        return this.canOpenDoors;
    }

    public boolean canFloat() {
        return this.canFloat;
    }

    public boolean canWalkOverFences() {
        return this.canWalkOverFences;
    }
}