// 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 Vvar<'a> {
    fn min_byte_range(&self) -> Range<usize> {
        0..self.v_org_mapping_offset_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 Vvar<'_> {
    /// `VVAR`
    const TAG: Tag = Tag::new(b"VVAR");
}

impl<'a> FontRead<'a> for Vvar<'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 [VVAR (Vertical Metrics Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/vvar) table
#[derive(Clone)]
pub struct Vvar<'a> {
    data: FontData<'a>,
}

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

    /// Major version number of the horizontal metrics variations table — set to 1.
    /// Minor version number of the horizontal metrics variations table — set to 0.
    pub fn version(&self) -> MajorMinor {
        let range = self.version_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Offset in bytes from the start of this table to the item variation store table.
    pub fn item_variation_store_offset(&self) -> Offset32 {
        let range = self.item_variation_store_offset_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Attempt to resolve [`item_variation_store_offset`][Self::item_variation_store_offset].
    pub fn item_variation_store(&self) -> Result<ItemVariationStore<'a>, ReadError> {
        let data = self.data;
        self.item_variation_store_offset().resolve(data)
    }

    /// Offset in bytes from the start of this table to the delta-set index mapping for advance heights (may be NULL).
    pub fn advance_height_mapping_offset(&self) -> Nullable<Offset32> {
        let range = self.advance_height_mapping_offset_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Attempt to resolve [`advance_height_mapping_offset`][Self::advance_height_mapping_offset].
    pub fn advance_height_mapping(&self) -> Option<Result<DeltaSetIndexMap<'a>, ReadError>> {
        let data = self.data;
        self.advance_height_mapping_offset().resolve(data)
    }

    /// Offset in bytes from the start of this table to the delta-set index mapping for top side bearings (may be NULL).
    pub fn tsb_mapping_offset(&self) -> Nullable<Offset32> {
        let range = self.tsb_mapping_offset_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Attempt to resolve [`tsb_mapping_offset`][Self::tsb_mapping_offset].
    pub fn tsb_mapping(&self) -> Option<Result<DeltaSetIndexMap<'a>, ReadError>> {
        let data = self.data;
        self.tsb_mapping_offset().resolve(data)
    }

    /// Offset in bytes from the start of this table to the delta-set index mapping for bottom side bearings (may be NULL).
    pub fn bsb_mapping_offset(&self) -> Nullable<Offset32> {
        let range = self.bsb_mapping_offset_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Attempt to resolve [`bsb_mapping_offset`][Self::bsb_mapping_offset].
    pub fn bsb_mapping(&self) -> Option<Result<DeltaSetIndexMap<'a>, ReadError>> {
        let data = self.data;
        self.bsb_mapping_offset().resolve(data)
    }

    /// Offset in bytes from the start of this table to the delta-set index mapping for Y coordinates of vertical origins (may be NULL).
    pub fn v_org_mapping_offset(&self) -> Nullable<Offset32> {
        let range = self.v_org_mapping_offset_byte_range();
        self.data.read_at(range.start).ok().unwrap()
    }

    /// Attempt to resolve [`v_org_mapping_offset`][Self::v_org_mapping_offset].
    pub fn v_org_mapping(&self) -> Option<Result<DeltaSetIndexMap<'a>, ReadError>> {
        let data = self.data;
        self.v_org_mapping_offset().resolve(data)
    }

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

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

    pub fn advance_height_mapping_offset_byte_range(&self) -> Range<usize> {
        let start = self.item_variation_store_offset_byte_range().end;
        start..start + Offset32::RAW_BYTE_LEN
    }

    pub fn tsb_mapping_offset_byte_range(&self) -> Range<usize> {
        let start = self.advance_height_mapping_offset_byte_range().end;
        start..start + Offset32::RAW_BYTE_LEN
    }

    pub fn bsb_mapping_offset_byte_range(&self) -> Range<usize> {
        let start = self.tsb_mapping_offset_byte_range().end;
        start..start + Offset32::RAW_BYTE_LEN
    }

    pub fn v_org_mapping_offset_byte_range(&self) -> Range<usize> {
        let start = self.bsb_mapping_offset_byte_range().end;
        start..start + Offset32::RAW_BYTE_LEN
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Vvar<'a> {
    fn type_name(&self) -> &str {
        "Vvar"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("version", self.version())),
            1usize => Some(Field::new(
                "item_variation_store_offset",
                FieldType::offset(
                    self.item_variation_store_offset(),
                    self.item_variation_store(),
                ),
            )),
            2usize => Some(Field::new(
                "advance_height_mapping_offset",
                FieldType::offset(
                    self.advance_height_mapping_offset(),
                    self.advance_height_mapping(),
                ),
            )),
            3usize => Some(Field::new(
                "tsb_mapping_offset",
                FieldType::offset(self.tsb_mapping_offset(), self.tsb_mapping()),
            )),
            4usize => Some(Field::new(
                "bsb_mapping_offset",
                FieldType::offset(self.bsb_mapping_offset(), self.bsb_mapping()),
            )),
            5usize => Some(Field::new(
                "v_org_mapping_offset",
                FieldType::offset(self.v_org_mapping_offset(), self.v_org_mapping()),
            )),
            _ => None,
        }
    }
}

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