use pumpkin_data::packet::clientbound::LOGIN_LOGIN_COMPRESSION;
use pumpkin_macros::java_packet;

use crate::ClientPacket;
use crate::VarInt;
use crate::ser::NetworkWriteExt;
use pumpkin_util::version::JavaMinecraftVersion;

/// Sent by the server to enable network compression for all subsequent packets.
///
/// Once this packet is sent, both the server and the client must compress any
/// packet with a size equal to or greater than the specified threshold.
#[java_packet(LOGIN_LOGIN_COMPRESSION)]
pub struct CSetCompression {
    /// The packet size threshold (in bytes) at which compression is applied.
    ///
    /// Packets smaller than this are sent uncompressed. A negative threshold
    /// typically disables compression.
    pub threshold: VarInt,
}

impl CSetCompression {
    #[must_use]
    pub const fn new(threshold: VarInt) -> Self {
        Self { threshold }
    }
}

impl ClientPacket for CSetCompression {
    fn write_packet_data(
        &self,
        mut write: impl std::io::Write,
        _version: &JavaMinecraftVersion,
    ) -> Result<(), crate::ser::WritingError> {
        write.write_var_int(&self.threshold)?;
        Ok(())
    }
}
