// THIS FILE IS AUTOGENERATED.
// Any changes to this file will be overwritten.
// For more information about how codegen works, see font-codegen/README.md

#[allow(unused_imports)]
use crate::codegen_prelude::*;

impl<'a> MinByteRange<'a> for Ltag<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.tag_ranges_byte_range().end
    }
    fn min_table_bytes(&self) -> &'a [u8] {
        let range = self.min_byte_range();
        self.data.as_bytes().get(range).unwrap_or_default()
    }
}

impl TopLevelTable for Ltag<'_> {
    /// `ltag`
    const TAG: Tag = Tag::new(b"ltag");
}

impl<'a> FontRead<'a> for Ltag<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        #[allow(clippy::absurd_extreme_comparisons)]
        if data.len() < Self::MIN_SIZE {
            return Err(ReadError::OutOfBounds);
        }
        Ok(Self { data })
    }
}

/// The [language tag](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6ltag.html) table.
#[derive(Clone)]
pub struct Ltag<'a> {
    data: FontData<'a>,
}

#[allow(clippy::needless_lifetimes)]
impl<'a> Ltag<'a> {
    pub const MIN_SIZE: usize = (u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
    basic_table_impls!(impl_the_methods);

    /// Table version; currently 1.
    pub fn version(&self) -> u32 {
        let range = self.version_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Table flags; currently none defined.
    pub fn flags(&self) -> u32 {
        let range = self.flags_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Number of language tags which follow.
    pub fn num_tags(&self) -> u32 {
        let range = self.num_tags_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Range of each tag's string.
    pub fn tag_ranges(&self) -> &'a [FTStringRange] {
        let range = self.tag_ranges_byte_range();
        self.data.read_array(range).ok().unwrap_or_default()
    }

    pub fn version_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + u32::RAW_BYTE_LEN
    }

    pub fn flags_byte_range(&self) -> Range<usize> {
        let start = self.version_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }

    pub fn num_tags_byte_range(&self) -> Range<usize> {
        let start = self.flags_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }

    pub fn tag_ranges_byte_range(&self) -> Range<usize> {
        let num_tags = self.num_tags();
        let start = self.num_tags_byte_range().end;
        start..start + (num_tags as usize).saturating_mul(FTStringRange::RAW_BYTE_LEN)
    }
}

const _: () = assert!(FontData::default_data_long_enough(Ltag::MIN_SIZE));

impl Default for Ltag<'_> {
    fn default() -> Self {
        Self {
            data: FontData::default_table_data(),
        }
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Ltag<'a> {
    fn type_name(&self) -> &str {
        "Ltag"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("version", self.version())),
            1usize => Some(Field::new("flags", self.flags())),
            2usize => Some(Field::new("num_tags", self.num_tags())),
            3usize => Some(Field::new(
                "tag_ranges",
                traversal::FieldType::array_of_records(
                    stringify!(FTStringRange),
                    self.tag_ranges(),
                    self.offset_data(),
                ),
            )),
            _ => None,
        }
    }
}

#[cfg(feature = "experimental_traverse")]
#[allow(clippy::needless_lifetimes)]
impl<'a> std::fmt::Debug for Ltag<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// Offset and length of string in `ltag` table.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct FTStringRange {
    /// Offset from the start of the table to the beginning of the string.
    pub offset: BigEndian<u16>,
    /// String length (in bytes).
    pub length: BigEndian<u16>,
}

impl FTStringRange {
    /// Offset from the start of the table to the beginning of the string.
    pub fn offset(&self) -> u16 {
        self.offset.get()
    }

    /// String length (in bytes).
    pub fn length(&self) -> u16 {
        self.length.get()
    }
}

impl FixedSize for FTStringRange {
    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeRecord<'a> for FTStringRange {
    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
        RecordResolver {
            name: "FTStringRange",
            get_field: Box::new(move |idx, _data| match idx {
                0usize => Some(Field::new("offset", self.offset())),
                1usize => Some(Field::new("length", self.length())),
                _ => None,
            }),
            data,
        }
    }
}
