use super::traits::AsCPacketView;
use crate::{
    define::*,
    helper::packet::{helper::get_info_relas, packet_final::CPacketViewRela},
};

///
/// ```compile_fail
/// use th_rs::define::TH_ESStreamInfo;
/// use th_rs::helper::packet::packet_view::CPacketView;
/// let info = TH_ESStreamInfo::default();
/// let data: Vec<u8> = Vec::new();
/// let view = CPacketView::new(data.as_slice(), &info);
/// drop(data);// compile FAIL!
/// let _ = view.as_slice().as_ptr();
/// ```
pub struct CPacketView<'a> {
    data: &'a [u8],
    info: &'a TH_ESStreamInfo,
    rela_packet: Vec<CPacketViewRela<'a>>,
}
impl<'a> CPacketView<'a> {
    pub fn new(data: &'a [u8], info: &'a TH_ESStreamInfo) -> Self {
        Self {
            data,
            info,
            rela_packet: get_info_relas(info),
        }
    }

    pub fn info(&self) -> &TH_ESStreamInfo {
        self.info
    }
    pub fn relas(&self) -> &[CPacketViewRela<'a>] {
        &self.rela_packet
    }

    pub fn as_slice(&self) -> &[u8] {
        self.data
    }
}

impl<'a, T> From<&'a T> for CPacketView<'a>
where
    T: AsCPacketView,
{
    fn from(value: &'a T) -> Self {
        CPacketView::new(value.get_data(), value.get_info())
    }
}
