package com.tiedup.remake.network.personality;

import java.util.function.Supplier;
import net.minecraft.ChatFormatting;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.SoundEvents;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.network.NetworkEvent;

/**
 * Packet sent from server to client to alert an owner that someone
 * is trying to free their slave.
 *
 */
public class PacketSlaveBeingFreed {

    private final String slaveName;
    private final String liberatorName;
    private final int x;
    private final int y;
    private final int z;

    /**
     * Create alert packet.
     *
     * @param slaveName Name of the slave being freed
     * @param liberatorName Name of the player trying to free them
     * @param x X coordinate
     * @param y Y coordinate
     * @param z Z coordinate
     */
    public PacketSlaveBeingFreed(
        String slaveName,
        String liberatorName,
        int x,
        int y,
        int z
    ) {
        this.slaveName = slaveName;
        this.liberatorName = liberatorName;
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public void encode(FriendlyByteBuf buf) {
        buf.writeUtf(slaveName, 64);
        buf.writeUtf(liberatorName, 64);
        buf.writeInt(x);
        buf.writeInt(y);
        buf.writeInt(z);
    }

    public static PacketSlaveBeingFreed decode(FriendlyByteBuf buf) {
        return new PacketSlaveBeingFreed(
            buf.readUtf(64),
            buf.readUtf(64),
            buf.readInt(),
            buf.readInt(),
            buf.readInt()
        );
    }

    public void handle(Supplier<NetworkEvent.Context> ctx) {
        ctx
            .get()
            .enqueueWork(() -> {
                if (FMLEnvironment.dist == Dist.CLIENT) {
                    handleClient();
                }
            });
        ctx.get().setPacketHandled(true);
    }

    @OnlyIn(Dist.CLIENT)
    private void handleClient() {
        net.minecraft.client.Minecraft mc =
            net.minecraft.client.Minecraft.getInstance();

        if (mc.player == null) return;

        // Build alert message
        Component message = Component.literal("")
            .append(
                Component.translatable("msg.tiedup.network.warning_prefix").withStyle(
                    ChatFormatting.RED,
                    ChatFormatting.BOLD
                )
            )
            .append(
                Component.literal(liberatorName).withStyle(
                    ChatFormatting.YELLOW
                )
            )
            .append(
                Component.translatable("msg.tiedup.network.is_trying_to_free").withStyle(
                    ChatFormatting.RED
                )
            )
            .append(
                Component.literal(slaveName).withStyle(ChatFormatting.YELLOW)
            )
            .append(
                Component.translatable("msg.tiedup.network.at_location").withStyle(
                    ChatFormatting.RED
                )
            )
            .append(
                Component.literal(
                    "[" + x + ", " + y + ", " + z + "]"
                ).withStyle(ChatFormatting.AQUA)
            )
            .append(Component.literal("!").withStyle(ChatFormatting.RED));

        // Send to player chat
        mc.player.displayClientMessage(message, false);

        // Play warning sound
        mc.player.playSound(SoundEvents.BELL_BLOCK, 1.0f, 1.0f);
    }

    // --- Getters ---

    public String getSlaveName() {
        return slaveName;
    }

    public String getLiberatorName() {
        return liberatorName;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getZ() {
        return z;
    }
}
