package com.tiedup.remake.state.components;

import com.tiedup.remake.state.hosts.IPlayerBindStateHost;
import com.tiedup.remake.v2.BodyRegionV2;
import com.tiedup.remake.v2.bondage.capability.V2EquipmentHelper;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

/**
 * Component responsible for querying player restraint state.
 * Provides read-only checks for equipment status.
 *
 * Single Responsibility: State queries
 * Complexity: LOW (read-only delegation)
 * Risk: LOW (no state modification)
 *
 * Epic 5F: Uses V2EquipmentHelper/BodyRegionV2.
 */
public class PlayerStateQuery {

    private final IPlayerBindStateHost host;

    public PlayerStateQuery(IPlayerBindStateHost host) {
        this.host = host;
    }

    // ========== State Query Methods ==========

    /** Check if player has ropes/ties equipped. */
    public boolean isTiedUp() {
        return V2EquipmentHelper.isRegionOccupied(
            host.getPlayer(),
            BodyRegionV2.ARMS
        );
    }

    /** Check if player is currently gagged. */
    public boolean isGagged() {
        return V2EquipmentHelper.isRegionOccupied(
            host.getPlayer(),
            BodyRegionV2.MOUTH
        );
    }

    /** Check if player is blindfolded. */
    public boolean isBlindfolded() {
        return V2EquipmentHelper.isRegionOccupied(
            host.getPlayer(),
            BodyRegionV2.EYES
        );
    }

    /** Check if player has earplugs. */
    public boolean hasEarplugs() {
        return V2EquipmentHelper.isRegionOccupied(
            host.getPlayer(),
            BodyRegionV2.EARS
        );
    }

    public boolean isEarplugged() {
        return hasEarplugs();
    }

    /** Check if player is wearing a collar. */
    public boolean hasCollar() {
        return V2EquipmentHelper.isRegionOccupied(
            host.getPlayer(),
            BodyRegionV2.NECK
        );
    }

    /** Returns the current collar ItemStack, or empty if none. */
    public ItemStack getCurrentCollar() {
        if (!hasCollar()) return ItemStack.EMPTY;
        return V2EquipmentHelper.getInRegion(
            host.getPlayer(),
            BodyRegionV2.NECK
        );
    }

    public boolean hasClothes() {
        return V2EquipmentHelper.isRegionOccupied(
            host.getPlayer(),
            BodyRegionV2.TORSO
        );
    }

    /** Check if player has mittens equipped. Mittens system */
    public boolean hasMittens() {
        return V2EquipmentHelper.isRegionOccupied(
            host.getPlayer(),
            BodyRegionV2.HANDS
        );
    }

    /** Check if player can be tied up (not already tied). */
    public boolean canBeTiedUp() {
        return !isTiedUp();
    }

    /** Check if player is both tied and gagged. */
    public boolean isBoundAndGagged() {
        return isTiedUp() && isGagged();
    }

    /** Check if player has knives in inventory. */
    public boolean hasKnives() {
        Player player = host.getPlayer();
        if (player == null) return false;

        // Check main inventory for knife items
        for (int i = 0; i < player.getInventory().getContainerSize(); i++) {
            ItemStack stack = player.getInventory().getItem(i);
            if (
                !stack.isEmpty() &&
                stack.getItem() instanceof com.tiedup.remake.items.base.IKnife
            ) {
                return true;
            }
        }
        return false;
    }

    /**
     * Check if player has a gagging effect enabled.
     * Checks both if gagged AND if the gag item implements the effect.
     */
    public boolean hasGaggingEffect() {
        if (!isGagged()) return false;
        ItemStack gag = V2EquipmentHelper.getInRegion(
            host.getPlayer(),
            BodyRegionV2.MOUTH
        );
        if (gag.isEmpty()) return false;
        return com.tiedup.remake.v2.bondage.datadriven.DataDrivenBondageItem.getComponent(
            gag, com.tiedup.remake.v2.bondage.component.ComponentType.GAGGING,
            com.tiedup.remake.v2.bondage.component.GaggingComponent.class) != null;
    }

    /**
     * Check if player has a blinding effect enabled.
     * Checks both if blindfolded AND if the blindfold item implements the effect.
     */
    public boolean hasBlindingEffect() {
        if (!isBlindfolded()) return false;
        ItemStack blindfold = V2EquipmentHelper.getInRegion(
            host.getPlayer(),
            BodyRegionV2.EYES
        );
        if (blindfold.isEmpty()) return false;
        return com.tiedup.remake.v2.bondage.datadriven.DataDrivenBondageItem.getComponent(
            blindfold, com.tiedup.remake.v2.bondage.component.ComponentType.BLINDING,
            com.tiedup.remake.v2.bondage.component.BlindingComponent.class) != null;
    }

    /**
     * Check if player can be kidnapped by random events.
     * Checks game rules and current state.
     */
    public boolean canBeKidnappedByEvents() {
        Player player = host.getPlayer();
        // Check if kidnapper spawning is enabled
        if (player != null && player.level() != null) {
            if (
                !com.tiedup.remake.core.SettingsAccessor.doKidnappersSpawn(
                    player.level().getGameRules()
                )
            ) {
                return false;
            }
        }
        // Can't be kidnapped if already tied up or captive (grace period protection)
        return !isTiedUp() && !host.getKidnapped().isCaptive();
    }
}
