//! The Ping plugin allows sending and receiving simple 'pings', with an optional message. Usually these are displayed as notifications.
use serde::{Deserialize, Serialize};

#[cfg(not(feature = "std"))]
use alloc::string::String;

use crate::packet::{NetworkPacket, NetworkPacketBody};

/// This packet is a ping request.
///
/// <https://invent.kde.org/network/kdeconnect-meta/blob/master/protocol.md#kdeconnectping>
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PingPacket {
    /// An optional message.
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default)]
    pub message: Option<String>,
}

impl NetworkPacket {
    /// Make a [`NetworkPacket`] with a [`PingPacket`] payload.
    pub fn ping<T: Into<String>>(message: T) -> Self {
        NetworkPacket::new(NetworkPacketBody::Ping(PingPacket {
            message: Some(message.into()),
        }))
    }

    /// Make a [`NetworkPacket`] with a [`PingPacket`] payload and no message.
    pub fn ping_empty_msg() -> Self {
        NetworkPacket::new(NetworkPacketBody::Ping(PingPacket { message: None }))
    }
}
