package com.tiedup.remake.network.master;

import com.tiedup.remake.entities.EntityMaster;
import com.tiedup.remake.entities.ai.master.MasterState;
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;

/**
 * Packet to synchronize Master entity state to tracking clients.
 *
 * Direction: Server → Client (S2C)
 *
 * Syncs:
 * - Current MasterState
 * - Pet player UUID
 * - Remaining distraction ticks
 */
public class PacketMasterStateSync extends AbstractClientPacket {

    private final int entityId;
    private final int stateOrdinal;
    private final UUID petPlayerUUID;
    private final long remainingDistractionTicks;

    /**
     * Create a state sync packet.
     *
     * @param entityId The master entity ID
     * @param stateOrdinal The state ordinal value
     * @param petPlayerUUID The pet player UUID (may be null)
     * @param remainingDistractionTicks Remaining ticks of distraction
     */
    public PacketMasterStateSync(
        int entityId,
        int stateOrdinal,
        UUID petPlayerUUID,
        long remainingDistractionTicks
    ) {
        this.entityId = entityId;
        this.stateOrdinal = stateOrdinal;
        this.petPlayerUUID = petPlayerUUID;
        this.remainingDistractionTicks = remainingDistractionTicks;
    }

    /**
     * Encode the packet to buffer.
     */
    public void encode(FriendlyByteBuf buf) {
        buf.writeInt(entityId);
        buf.writeInt(stateOrdinal);
        buf.writeBoolean(petPlayerUUID != null);
        if (petPlayerUUID != null) {
            buf.writeUUID(petPlayerUUID);
        }
        buf.writeLong(remainingDistractionTicks);
    }

    /**
     * Decode the packet from buffer.
     */
    public static PacketMasterStateSync decode(FriendlyByteBuf buf) {
        int entityId = buf.readInt();
        int stateOrdinal = buf.readInt();
        UUID petPlayerUUID = null;
        if (buf.readBoolean()) {
            petPlayerUUID = buf.readUUID();
        }
        long remainingDistractionTicks = buf.readLong();
        return new PacketMasterStateSync(
            entityId,
            stateOrdinal,
            petPlayerUUID,
            remainingDistractionTicks
        );
    }

    /**
     * Handle packet on client side.
     */
    @Override
    @OnlyIn(Dist.CLIENT)
    protected void handleClientImpl() {
        ClientHandler.handle(this);
    }

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

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

            net.minecraft.world.entity.Entity entity = mc.level.getEntity(
                pkt.entityId
            );
            if (entity instanceof EntityMaster master) {
                // Apply state sync
                // Note: Client-side state is read-only via synced entity data
                // This packet is for extended state info like distraction timer
            }
        }
    }
}
