use crate::error::DatError;
use crate::util::{encode_base64_url, to_utf8};
use std::fmt::Display;

pub struct DatPayload {
    pub(crate) plain_bytes: Vec<u8>,
    pub(crate) secure_bytes: Vec<u8>,
}

pub struct DatStringPayload {
    pub plain: String,
    pub secure: String,
}

impl DatPayload {
    #[inline]
    pub fn to_string_payload(self) -> Result<DatStringPayload, DatError> {
        Ok(DatStringPayload {
            plain: to_utf8(self.plain_bytes)?,
            secure: to_utf8(self.secure_bytes)?,
        })
    }
}

impl Display for DatPayload {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} {}", encode_base64_url(&*self.plain_bytes), encode_base64_url(&*self.secure_bytes))
    }
}

impl Display for DatStringPayload {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} {}", self.plain, self.secure)
    }
}
