/*
 * Copyright (c) 2026 盐灯水母
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.remains_cave.network;

import com.remains_cave.dungeon.tomb.ChallengeType;
import net.minecraft.network.RegistryFriendlyByteBuf;
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 org.jetbrains.annotations.NotNull;

public record ChallengeDataPacket(
        String roomId,
        ChallengeType challengeType,
        int wave,
        float monsterProgress,
        float timeProgress,
        float waveProgress,
        int phase
) implements CustomPacketPayload {
    public static final ResourceLocation ID = ResourceLocation.parse("remains_cave:challenge_data");
    public static final Type<ChallengeDataPacket> TYPE = new Type<>(ID);

    public static final StreamCodec<RegistryFriendlyByteBuf, ChallengeDataPacket> CODEC = new StreamCodec<>() {
        @Override
        public void encode(RegistryFriendlyByteBuf buf, ChallengeDataPacket packet) {
            ByteBufCodecs.STRING_UTF8.encode(buf, packet.roomId());
            ChallengeType.STREAM_CODEC.encode(buf, packet.challengeType());
            ByteBufCodecs.INT.encode(buf, packet.wave());
            ByteBufCodecs.FLOAT.encode(buf, packet.monsterProgress());
            ByteBufCodecs.FLOAT.encode(buf, packet.timeProgress());
            ByteBufCodecs.FLOAT.encode(buf, packet.waveProgress());
            ByteBufCodecs.INT.encode(buf, packet.phase());
        }

        @Override
        public ChallengeDataPacket decode(RegistryFriendlyByteBuf buf) {
            return new ChallengeDataPacket(
                    ByteBufCodecs.STRING_UTF8.decode(buf),
                    ChallengeType.STREAM_CODEC.decode(buf),
                    ByteBufCodecs.INT.decode(buf),
                    ByteBufCodecs.FLOAT.decode(buf),
                    ByteBufCodecs.FLOAT.decode(buf),
                    ByteBufCodecs.FLOAT.decode(buf),
                    ByteBufCodecs.INT.decode(buf)
            );
        }
    };

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