package com.tiedup.remake.state.components;

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

/**
 * Component responsible for retrieving player data and equipment.
 * Provides access to current bondage items and player information.
 *
 * Single Responsibility: Data retrieval
 * Complexity: LOW (simple getters)
 * Risk: LOW (read-only access)
 *
 * Epic 5F: Uses V2EquipmentHelper/BodyRegionV2.
 */
public class PlayerDataRetrieval {

    private final IPlayerBindStateHost host;

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

    // ========== Equipment Getters ==========

    public ItemStack getCurrentBind() {
        return V2EquipmentHelper.getInRegion(
            host.getPlayer(),
            BodyRegionV2.ARMS
        );
    }

    public ItemStack getCurrentGag() {
        return V2EquipmentHelper.getInRegion(
            host.getPlayer(),
            BodyRegionV2.MOUTH
        );
    }

    public ItemStack getCurrentBlindfold() {
        return V2EquipmentHelper.getInRegion(
            host.getPlayer(),
            BodyRegionV2.EYES
        );
    }

    public ItemStack getCurrentEarplugs() {
        return V2EquipmentHelper.getInRegion(
            host.getPlayer(),
            BodyRegionV2.EARS
        );
    }

    public ItemStack getCurrentClothes() {
        return V2EquipmentHelper.getInRegion(
            host.getPlayer(),
            BodyRegionV2.TORSO
        );
    }

    public ItemStack getCurrentMittens() {
        return V2EquipmentHelper.getInRegion(
            host.getPlayer(),
            BodyRegionV2.HANDS
        );
    }

    public ItemStack getCurrentCollar() {
        return V2EquipmentHelper.getInRegion(
            host.getPlayer(),
            BodyRegionV2.NECK
        );
    }

    // ========== Player Information ==========

    /**
     * Get the player's display name, checking collar for nickname first.
     * If collar has a nickname, returns that; otherwise returns player name.
     */
    public String getNameFromCollar() {
        Player player = host.getPlayer();
        ItemStack collar = getCurrentCollar();

        if (!collar.isEmpty() && CollarHelper.isCollar(collar)) {
            // Try to get nickname from collar NBT
            String nickname = CollarHelper.getNickname(collar);
            if (nickname != null && !nickname.isEmpty()) {
                return nickname;
            }
        }

        // Fallback to player name
        return player.getName().getString();
    }

    /**
     * Check if player has a named collar (collar with nickname).
     */
    public boolean hasNamedCollar() {
        ItemStack collar = getCurrentCollar();
        if (collar.isEmpty()) return false;

        if (CollarHelper.isCollar(collar)) {
            String nickname = CollarHelper.getNickname(collar);
            return nickname != null && !nickname.isEmpty();
        }
        return false;
    }

    /**
     * Check if player has clothes with small arms flag.
     */
    public boolean hasClothesWithSmallArms() {
        return false;
    }

    /**
     * Get the player as a LivingEntity.
     * Used for generic entity operations.
     */
    public LivingEntity asLivingEntity() {
        return host.getPlayer();
    }
}
