package com.tiedup.remake.entities;

import com.tiedup.remake.items.base.ItemCollar;
import com.tiedup.remake.util.teleport.Position;
import com.tiedup.remake.v2.BodyRegionV2;
import java.util.List;
import java.util.UUID;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

/**
 * Helper class to access collar configuration for EntityKidnapper.
 *
 * <p>Provides null-safe accessors for collar settings like:
 * <ul>
 *   <li>Kidnapping mode state</li>
 *   <li>Prison/Home positions</li>
 *   <li>Blacklist/Whitelist filtering</li>
 *   <li>Post-capture behavior flags</li>
 * </ul>
 *
 * <p>This is a transient helper - not persisted to NBT.
 */
public class KidnapperCollarConfig {

    private final EntityKidnapper kidnapper;

    public KidnapperCollarConfig(EntityKidnapper kidnapper) {
        this.kidnapper = kidnapper;
    }

    // COLLAR ITEM ACCESS

    /**
     * Get the collar item if equipped.
     * @return ItemCollar or null if no collar or not an ItemCollar
     */
    @Nullable
    public ItemCollar getCollarItem() {
        ItemStack collar = kidnapper.getEquipment(BodyRegionV2.NECK);
        if (collar.isEmpty()) return null;
        if (collar.getItem() instanceof ItemCollar itemCollar) {
            return itemCollar;
        }
        return null;
    }

    /**
     * Get the collar ItemStack.
     * @return The collar stack (may be empty)
     */
    public ItemStack getCollarStack() {
        return kidnapper.getEquipment(BodyRegionV2.NECK);
    }

    // KIDNAPPING MODE

    /**
     * Check if kidnapping mode is enabled via collar.
     */
    public boolean isKidnappingModeEnabled() {
        if (!kidnapper.hasCollar()) return false;

        ItemCollar itemCollar = getCollarItem();
        if (itemCollar == null) return false;

        return itemCollar.isKidnappingModeEnabled(getCollarStack());
    }

    /**
     * Check if kidnapping mode is fully ready (enabled + prison set).
     */
    public boolean isKidnappingModeReady() {
        if (!kidnapper.hasCollar()) return false;

        ItemCollar itemCollar = getCollarItem();
        if (itemCollar == null) return false;

        return itemCollar.isKidnappingModeReady(getCollarStack());
    }

    // POSITION GETTERS

    /**
     * Get cell ID from collar.
     */
    @Nullable
    public java.util.UUID getCellId() {
        ItemCollar itemCollar = getCollarItem();
        if (itemCollar == null) return null;

        return itemCollar.getCellId(getCollarStack());
    }

    /**
     * Check if collar has a cell assigned.
     */
    public boolean hasCellAssigned() {
        ItemCollar itemCollar = getCollarItem();
        if (itemCollar == null) return false;

        return itemCollar.hasCellAssigned(getCollarStack());
    }

    // BEHAVIOR FLAGS

    /**
     * Check if should warn masters after capturing slave.
     */
    public boolean shouldWarnMasters() {
        ItemCollar itemCollar = getCollarItem();
        if (itemCollar == null) return false;

        return itemCollar.shouldWarnMasters(getCollarStack());
    }

    /**
     * Check if should tie slave to pole in prison.
     */
    public boolean shouldTieToPole() {
        ItemCollar itemCollar = getCollarItem();
        if (itemCollar == null) return false;

        return itemCollar.shouldTieToPole(getCollarStack());
    }

    // BLACKLIST/WHITELIST

    /**
     * Check if player is valid target for kidnapping mode.
     * Uses collar blacklist/whitelist.
     *
     * <p>Logic:
     * <ul>
     *   <li>If whitelist is not empty, player MUST be on whitelist</li>
     *   <li>Otherwise, player must NOT be on blacklist</li>
     * </ul>
     */
    public boolean isValidKidnappingTarget(Player player) {
        ItemCollar itemCollar = getCollarItem();
        if (itemCollar == null) return true; // No collar config = everyone is valid

        ItemStack collarStack = getCollarStack();
        UUID playerUUID = player.getUUID();

        // If whitelist exists and is not empty, player MUST be on it
        List<UUID> whitelist = itemCollar.getWhitelist(collarStack);
        if (!whitelist.isEmpty()) {
            return whitelist.contains(playerUUID);
        }

        // Otherwise, check blacklist (blacklisted = not a valid target)
        return !itemCollar.isBlacklisted(collarStack, playerUUID);
    }
}
