package com.tiedup.remake.items;

import com.tiedup.remake.items.base.ItemCollar;
import com.tiedup.remake.items.bondage3d.IHas3DModelConfig;
import com.tiedup.remake.items.bondage3d.Model3DConfig;
import java.util.List;
import java.util.Set;
import net.minecraft.ChatFormatting;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;

/**
 * Choke Collar - Pet play collar used by Masters.
 *
 * <p>Special feature: Can be put in "choke mode" which applies a drowning effect.</p>
 * <p>Used by Masters for punishment. The effect simulates choking by reducing air supply,
 * which triggers drowning damage if left active for too long.</p>
 *
 * <p><b>Mechanics:</b></p>
 * <ul>
 *   <li>When choking is active, the wearer's air supply decreases rapidly</li>
 *   <li>This creates the drowning effect (damage and bubble particles)</li>
 *   <li>Masters should deactivate the choke before the pet dies</li>
 *   <li>The choke is controlled by the Master's punishment system</li>
 * </ul>
 *
 * @see com.tiedup.remake.entities.ai.master.MasterPunishGoal
 * @see com.tiedup.remake.events.restriction.PetPlayRestrictionHandler
 */
public class ItemChokeCollar extends ItemCollar implements IHas3DModelConfig {

    private static final String NBT_CHOKING = "choking";

    private static final Model3DConfig CONFIG = new Model3DConfig(
        "tiedup:models/obj/choke_collar_leather/model.obj",
        "tiedup:models/obj/choke_collar_leather/texture.png",
        0.0f,
        1.47f,
        0.0f, // Collar band centered at neck level
        1.0f,
        0.0f,
        0.0f,
        180.0f, // Flip Y to match rendering convention
        Set.of()
    );

    public ItemChokeCollar() {
        super(new Item.Properties());
    }

    @Override
    public String getItemName() {
        return "choke_collar";
    }

    /**
     * Check if choke mode is active.
     *
     * @param stack The collar ItemStack
     * @return true if choking is active
     */
    public boolean isChoking(ItemStack stack) {
        CompoundTag tag = stack.getTag();
        return tag != null && tag.getBoolean(NBT_CHOKING);
    }

    /**
     * Set choke mode on/off.
     * When active, applies drowning effect to wearer (handled by PetPlayRestrictionHandler).
     *
     * @param stack The collar ItemStack
     * @param choking true to activate choking, false to deactivate
     */
    public void setChoking(ItemStack stack, boolean choking) {
        stack.getOrCreateTag().putBoolean(NBT_CHOKING, choking);
    }

    /**
     * Check if collar is in pet play mode (from Master).
     *
     * @param stack The collar ItemStack
     * @return true if this is a pet play collar
     */
    public boolean isPetPlayMode(ItemStack stack) {
        CompoundTag tag = stack.getTag();
        return tag != null && tag.getBoolean("petPlayMode");
    }

    @Override
    public void appendHoverText(
        ItemStack stack,
        @Nullable Level level,
        List<Component> tooltip,
        TooltipFlag flag
    ) {
        super.appendHoverText(stack, level, tooltip, flag);

        // Show choke status
        if (isChoking(stack)) {
            tooltip.add(
                Component.literal("CHOKING ACTIVE!")
                    .withStyle(ChatFormatting.DARK_RED)
                    .withStyle(ChatFormatting.BOLD)
            );
        }

        // Show pet play mode status
        if (isPetPlayMode(stack)) {
            tooltip.add(
                Component.literal("Pet Play Mode").withStyle(
                    ChatFormatting.LIGHT_PURPLE
                )
            );
        }

        // Description
        tooltip.add(
            Component.literal("A special collar used for pet play punishment")
                .withStyle(ChatFormatting.DARK_GRAY)
                .withStyle(ChatFormatting.ITALIC)
        );
    }

    /**
     * Choke collar cannot shock like shock collar.
     */
    @Override
    public boolean canShock() {
        return false;
    }

    // 3D Model Support

    @Override
    public boolean uses3DModel() {
        return true;
    }

    @Override
    public ResourceLocation get3DModelLocation() {
        return ResourceLocation.tryParse(CONFIG.objPath());
    }

    @Override
    public Model3DConfig getModelConfig() {
        return CONFIG;
    }
}
