package com.tiedup.remake.client.gui.screens;

import com.tiedup.remake.network.ModNetwork;
import com.tiedup.remake.network.item.PacketAdjustItem;
import com.tiedup.remake.state.PlayerBindState;
import com.tiedup.remake.v2.BodyRegionV2;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 * Screen for adjusting Y position of the player's own gags and blindfolds.
 * Shows 3D preview of player with real-time adjustment.
 *
 */
@OnlyIn(Dist.CLIENT)
public class AdjustmentScreen extends BaseAdjustmentScreen {

    public AdjustmentScreen() {
        super(Component.translatable("gui.tiedup.adjust_position"));
    }

    // ==================== ABSTRACT IMPLEMENTATIONS ====================

    @Override
    protected LivingEntity getTargetEntity() {
        return this.minecraft.player;
    }

    @Override
    protected ItemStack getGag() {
        Player player = this.minecraft.player;
        if (player == null) return ItemStack.EMPTY;

        PlayerBindState state = PlayerBindState.getInstance(player);
        if (state == null) return ItemStack.EMPTY;

        return state.getEquipment(BodyRegionV2.MOUTH);
    }

    @Override
    protected ItemStack getBlindfold() {
        Player player = this.minecraft.player;
        if (player == null) return ItemStack.EMPTY;

        PlayerBindState state = PlayerBindState.getInstance(player);
        if (state == null) return ItemStack.EMPTY;

        return state.getEquipment(BodyRegionV2.EYES);
    }

    @Override
    protected void sendAdjustment(Mode mode, float value, float scale) {
        BodyRegionV2 region = switch (mode) {
            case GAG -> BodyRegionV2.MOUTH;
            case BLINDFOLD -> BodyRegionV2.EYES;
            case BOTH -> null; // Handled separately in applyAdjustment
        };

        if (region != null) {
            ModNetwork.sendToServer(new PacketAdjustItem(region, value, scale));
        }
    }

    // ==================== STATIC HELPERS ====================

    /**
     * Check if this screen should be openable (player has adjustable items).
     */
    public static boolean canOpen() {
        Minecraft mc = Minecraft.getInstance();
        if (mc.player == null) return false;

        PlayerBindState state = PlayerBindState.getInstance(mc.player);
        if (state == null) return false;

        return (
            !state.getEquipment(BodyRegionV2.MOUTH).isEmpty() ||
            !state.getEquipment(BodyRegionV2.EYES).isEmpty()
        );
    }
}
