package com.tiedup.remake.network.minigame;

import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.network.base.AbstractClientPacket;
import java.util.UUID;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 *
 * Contains:
 * - Session UUID
 * - Sweet spot center and width (server-authoritative)
 * - Current position
 * - Remaining lockpick uses
 */
public class PacketLockpickMiniGameState extends AbstractClientPacket {

    private final UUID sessionId;
    private final float sweetSpotCenter;
    private final float sweetSpotWidth;
    private final float currentPosition;
    private final int remainingUses;

    public PacketLockpickMiniGameState(
        UUID sessionId,
        float sweetSpotCenter,
        float sweetSpotWidth,
        float currentPosition,
        int remainingUses
    ) {
        this.sessionId = sessionId;
        this.sweetSpotCenter = sweetSpotCenter;
        this.sweetSpotWidth = sweetSpotWidth;
        this.currentPosition = currentPosition;
        this.remainingUses = remainingUses;
    }

    public void encode(FriendlyByteBuf buf) {
        buf.writeUUID(sessionId);
        buf.writeFloat(sweetSpotCenter);
        buf.writeFloat(sweetSpotWidth);
        buf.writeFloat(currentPosition);
        buf.writeVarInt(remainingUses);
    }

    public static PacketLockpickMiniGameState decode(FriendlyByteBuf buf) {
        UUID sessionId = buf.readUUID();
        float sweetSpotCenter = buf.readFloat();
        float sweetSpotWidth = buf.readFloat();
        float currentPosition = buf.readFloat();
        int remainingUses = buf.readVarInt();
        return new PacketLockpickMiniGameState(
            sessionId,
            sweetSpotCenter,
            sweetSpotWidth,
            currentPosition,
            remainingUses
        );
    }

    @Override
    @OnlyIn(Dist.CLIENT)
    protected void handleClientImpl() {
        ClientHandler.handle(this);
    }

    @OnlyIn(Dist.CLIENT)
    private static class ClientHandler {

        private static void handle(PacketLockpickMiniGameState pkt) {
            net.minecraft.client.Minecraft mc =
                net.minecraft.client.Minecraft.getInstance();
            if (mc.player == null) {
                return;
            }

            TiedUpMod.LOGGER.debug(
                "[PacketLockpickMiniGameState] Received state: sweet={}(w={}), pos={}, uses={}",
                pkt.sweetSpotCenter,
                pkt.sweetSpotWidth,
                pkt.currentPosition,
                pkt.remainingUses
            );

            // Open the lockpick mini-game screen
            mc.setScreen(
                new com.tiedup.remake.client.gui.screens.LockpickMiniGameScreen(
                    pkt.sessionId,
                    pkt.sweetSpotCenter,
                    pkt.sweetSpotWidth,
                    pkt.currentPosition,
                    pkt.remainingUses
                )
            );
        }
    }

    // Getters
    public UUID getSessionId() {
        return sessionId;
    }

    public float getSweetSpotCenter() {
        return sweetSpotCenter;
    }

    public float getSweetSpotWidth() {
        return sweetSpotWidth;
    }

    public float getCurrentPosition() {
        return currentPosition;
    }

    public int getRemainingUses() {
        return remainingUses;
    }
}
