package com.tiedup.remake.items;

import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.entities.EntityDamsel;
import com.tiedup.remake.personality.PersonalityState;
import com.tiedup.remake.personality.PersonalityType;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;

/**
 * Debug Wand for testing the Personality System.
 *
 * <p>OP item for developers/testers to manipulate NPC personality state.
 *
 * <p>Controls:
 * <ul>
 *   <li>Right-click on Damsel: Cycle personality type</li>
 *   <li>Shift + Right-click on Damsel: Show status</li>
 * </ul>
 */
public class ItemDebugWand extends Item {

    public ItemDebugWand() {
        super(new Properties().stacksTo(1));
    }

    @Override
    public InteractionResult interactLivingEntity(
        ItemStack stack,
        Player player,
        LivingEntity target,
        InteractionHand hand
    ) {
        if (player.level().isClientSide()) {
            return InteractionResult.SUCCESS;
        }

        if (!(target instanceof EntityDamsel damsel)) {
            player.displayClientMessage(
                Component.literal("Target must be a Damsel!").withStyle(
                    ChatFormatting.RED
                ),
                true
            );
            return InteractionResult.FAIL;
        }

        PersonalityState state = damsel.getPersonalityState();
        if (state == null) {
            player.displayClientMessage(
                Component.literal("Damsel has no personality state!").withStyle(
                    ChatFormatting.RED
                ),
                true
            );
            return InteractionResult.FAIL;
        }

        boolean isShift = player.isShiftKeyDown();

        if (isShift) {
            // Show status
            showStatus(damsel, state, player);
        } else {
            // Cycle personality type
            cyclePersonality(damsel, state, player);
        }

        return InteractionResult.SUCCESS;
    }

    private void cyclePersonality(
        EntityDamsel damsel,
        PersonalityState state,
        Player player
    ) {
        PersonalityType current = state.getPersonality();
        PersonalityType[] types = PersonalityType.values();

        // Find next type
        int currentIndex = current.ordinal();
        int nextIndex = (currentIndex + 1) % types.length;
        PersonalityType next = types[nextIndex];

        // Create new state with new personality
        damsel.setPersonalityType(next);

        player.displayClientMessage(
            Component.literal("Personality: ")
                .withStyle(ChatFormatting.YELLOW)
                .append(
                    Component.literal(current.name()).withStyle(
                        ChatFormatting.GRAY
                    )
                )
                .append(
                    Component.literal(" -> ").withStyle(ChatFormatting.WHITE)
                )
                .append(
                    Component.literal(next.name()).withStyle(
                        ChatFormatting.GOLD
                    )
                ),
            false
        );

        TiedUpMod.LOGGER.info(
            "[DebugWand] {} personality changed: {} -> {}",
            damsel.getNpcName(),
            current,
            next
        );
    }

    private void showStatus(
        EntityDamsel damsel,
        PersonalityState state,
        Player player
    ) {
        player.displayClientMessage(Component.literal(""), false); // Blank line
        player.displayClientMessage(
            Component.literal("=== " + damsel.getNpcName() + " ===").withStyle(
                ChatFormatting.GOLD,
                ChatFormatting.BOLD
            ),
            false
        );

        // Personality
        player.displayClientMessage(
            Component.literal("Personality: ")
                .withStyle(ChatFormatting.GRAY)
                .append(
                    Component.literal(state.getPersonality().name()).withStyle(
                        ChatFormatting.YELLOW
                    )
                ),
            false
        );

        // Mood
        float mood = state.getMood();
        ChatFormatting moodColor =
            mood > 60
                ? ChatFormatting.GREEN
                : mood < 40
                    ? ChatFormatting.RED
                    : ChatFormatting.YELLOW;
        player.displayClientMessage(
            Component.literal("Mood: ")
                .withStyle(ChatFormatting.GRAY)
                .append(
                    Component.literal(String.format("%.0f%%", mood)).withStyle(
                        moodColor
                    )
                ),
            false
        );

        // Needs
        var needs = state.getNeeds();
        player.displayClientMessage(
            Component.literal("Needs: ")
                .withStyle(ChatFormatting.GRAY)
                .append(
                    Component.literal(
                        String.format(
                            "Hunger:%.0f Rest:%.0f",
                            needs.getHunger(),
                            needs.getRest()
                        )
                    ).withStyle(ChatFormatting.WHITE)
                ),
            false
        );

        // Bondage state
        player.displayClientMessage(
            Component.literal("State: ")
                .withStyle(ChatFormatting.GRAY)
                .append(
                    Component.literal(
                        (damsel.isTiedUp() ? "TIED " : "") +
                            (damsel.isGagged() ? "GAGGED " : "") +
                            (damsel.isBlindfolded() ? "BLIND " : "") +
                            (damsel.hasCollar() ? "COLLARED " : "") +
                            (damsel.isSitting() ? "SIT " : "") +
                            (damsel.isKneeling() ? "KNEEL " : "")
                    ).withStyle(ChatFormatting.RED)
                ),
            false
        );
    }

    @Override
    public void appendHoverText(
        ItemStack stack,
        @Nullable Level level,
        List<Component> tooltip,
        TooltipFlag flag
    ) {
        tooltip.add(
            Component.literal("Debug tool for Personality System").withStyle(
                ChatFormatting.GRAY
            )
        );
        tooltip.add(Component.literal(""));
        tooltip.add(
            Component.literal("Right-click: Cycle Personality").withStyle(
                ChatFormatting.GREEN
            )
        );
        tooltip.add(
            Component.literal("Shift + Right-click: Show Status").withStyle(
                ChatFormatting.YELLOW
            )
        );
        tooltip.add(Component.literal(""));
        tooltip.add(
            Component.literal("OP Item - Testing Only").withStyle(
                ChatFormatting.RED,
                ChatFormatting.ITALIC
            )
        );
    }

    @Override
    public boolean isFoil(ItemStack stack) {
        return true; // Enchantment glint to show it's special
    }
}
