package com.tiedup.remake.entities;

import com.tiedup.remake.v2.BodyRegionV2;
import com.tiedup.remake.v2.bondage.CollarHelper;
import java.util.List;
import java.util.UUID;
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 STACK ACCESS

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

    /**
     * Check if the kidnapper has a valid collar.
     */
    private boolean hasValidCollar() {
        return kidnapper.hasCollar() && CollarHelper.isCollar(getCollarStack());
    }

    // KIDNAPPING MODE

    /**
     * Check if kidnapping mode is enabled via collar.
     */
    public boolean isKidnappingModeEnabled() {
        if (!hasValidCollar()) return false;
        return CollarHelper.isKidnappingModeEnabled(getCollarStack());
    }

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

    // POSITION GETTERS

    /**
     * Get cell ID from collar.
     */
    @Nullable
    public java.util.UUID getCellId() {
        if (!hasValidCollar()) return null;
        return CollarHelper.getCellId(getCollarStack());
    }

    /**
     * Check if collar has a cell assigned.
     */
    public boolean hasCellAssigned() {
        if (!hasValidCollar()) return false;
        return CollarHelper.hasCellAssigned(getCollarStack());
    }

    // BEHAVIOR FLAGS

    /**
     * Check if should warn masters after capturing slave.
     */
    public boolean shouldWarnMasters() {
        if (!hasValidCollar()) return false;
        return CollarHelper.shouldWarnMasters(getCollarStack());
    }

    /**
     * Check if should tie slave to pole in prison.
     */
    public boolean shouldTieToPole() {
        if (!hasValidCollar()) return false;
        return CollarHelper.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) {
        if (!hasValidCollar()) 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 = CollarHelper.getWhitelist(collarStack);
        if (!whitelist.isEmpty()) {
            return whitelist.contains(playerUUID);
        }

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