package com.r3944realms.leashedplayer.network.server;

import com.r3944realms.leashedplayer.LeashedPlayer;
import com.r3944realms.leashedplayer.modInterface.ILivingEntityExtension;
import com.r3944realms.leashedplayer.modInterface.PlayerLeashable;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs;
import net.neoforged.neoforge.network.handling.IPayloadContext;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public record IncreaseLeashRopeLength(Code code, String playerUUID) implements CustomPacketPayload {
    public static final CustomPacketPayload.Type<IncreaseLeashRopeLength> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(LeashedPlayer.MOD_ID,"add_leash_rope_length"));
    public static final StreamCodec<FriendlyByteBuf, IncreaseLeashRopeLength> STREAM_CODEC =
            StreamCodec.composite(
                    NeoForgeStreamCodecs.enumCodec(Code.class), IncreaseLeashRopeLength::code,
                    ByteBufCodecs.STRING_UTF8, IncreaseLeashRopeLength::playerUUID,
                    IncreaseLeashRopeLength::new
    );
    public static final String INCREASE_LEASH_ROPE_LENGTH = "leashedplayer.leash_rope.length.increase",
            INCREASE_SELF_LEASH_ROPE_LENGTH = INCREASE_LEASH_ROPE_LENGTH + ".self";


    public void handle(IPayloadContext context) {
        context.enqueueWork(() -> {
            Player player = context.player();
            ServerLevel level = (ServerLevel) player.level();
            Player playerByUUID = level.getPlayerByUUID(UUID.fromString(playerUUID));
            if (playerByUUID != null) {
                ILivingEntityExtension entityExtension = (ILivingEntityExtension) playerByUUID;
                Entity leashDataEntity = PlayerLeashable.getLeashDataEntity((ServerPlayer) playerByUUID, level);
                if ((leashDataEntity != null && code == Code.OTHER_ST && leashDataEntity == player ) || (leashDataEntity != null && code == Code.OTHER_ST)) {
                    float newValue = Math.max(Math.min(entityExtension.getLeashLength() + 1, LeashedPlayer.M4()), LeashedPlayer.M3());
                    entityExtension.setLeashLength(newValue);
                    ((ServerPlayer) playerByUUID).sendSystemMessage(Component.translatable(INCREASE_SELF_LEASH_ROPE_LENGTH, newValue), true);

                    ((ServerPlayer) playerByUUID).sendSystemMessage(Component.translatable(INCREASE_LEASH_ROPE_LENGTH, playerByUUID.getDisplayName(), newValue), true);
                } else if (code == Code.SELF) {
                    float newValue = Math.max(Math.min(entityExtension.getLeashLength() + 1, LeashedPlayer.M4()), LeashedPlayer.M3());
                    entityExtension.setLeashLength(newValue);
                    ((ServerPlayer) playerByUUID).sendSystemMessage(Component.translatable(INCREASE_SELF_LEASH_ROPE_LENGTH, newValue), true);
                    ((ServerPlayer) playerByUUID).sendSystemMessage(Component.translatable(INCREASE_SELF_LEASH_ROPE_LENGTH, newValue), true);

                } else {
                    ((ServerPlayer) playerByUUID).sendSystemMessage(Component.translatable(Code.LEASH_LENGTH_FAILED_SET), true);
                    ((ServerPlayer) player).sendSystemMessage(Component.translatable(Code.LEASH_LENGTH_FAILED_SET), true);

                }
            }
        });
    }
    @Override
    public @NotNull Type<? extends CustomPacketPayload> type() {
        return TYPE;
    }
}
