/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.core.network.toServer;

import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraftforge.network.NetworkEvent;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.content.block.blockentity.LoveMachineBlockEntity;

import java.util.function.Supplier;

public record OffsetConfigPacket(
        BlockPos blockPos , float speed, float offsetX, float offsetY, float offsetZ, boolean offsetEnabled
) {
    public OffsetConfigPacket(@NotNull FriendlyByteBuf buf) {
        this(
                buf.readBlockPos(),
                buf.readFloat(),
                buf.readFloat(),
                buf.readFloat(),
                buf.readFloat(),
                buf.readBoolean()
        );
    }
    public void encode(@NotNull FriendlyByteBuf buf) {
        buf.writeBlockPos(blockPos);
        buf.writeFloat(speed);
        buf.writeFloat(offsetX);
        buf.writeFloat(offsetY);
        buf.writeFloat(offsetZ);
        buf.writeBoolean(offsetEnabled);
    }

    public void handle(@NotNull Supplier<NetworkEvent.Context> context) {
        context.get().enqueueWork(() -> {
            ServerPlayer player = context.get().getSender();
            if (player != null) {
                Level level = player.level();
                if (level.hasChunkAt(blockPos)) {
                    BlockEntity blockEntity = level.getBlockEntity(blockPos);
                    if (blockEntity instanceof LoveMachineBlockEntity loveMachine) {
                        // 验证玩家有权限
                        if (player.distanceToSqr(blockPos.getX() + 0.5, blockPos.getY() + 0.5, blockPos.getZ() + 0.5) <= 64) {
                            loveMachine.setSpeed(speed);
                            loveMachine.setOffset(offsetX, offsetY, offsetZ);
                            loveMachine.setOffsetEnabled(offsetEnabled);
                            loveMachine.setChanged();

                            // 同步到客户端
                            level.sendBlockUpdated(blockPos, loveMachine.getBlockState(),
                                    loveMachine.getBlockState(), 3);
                        }
                    }
                }
            }
        });
        context.get().setPacketHandled(true);
    }
}
