//! `Line` type.

use crate::durandal::{bin::OptU16, err::*};
use bitflags::bitflags;

/// Reads a `LINS` chunk.
pub fn read(b: &[u8]) -> ResultS<(Line, usize)>
{
   read_data! {
      endian: BIG, buf: b, size: 32, start: 0, data {
         let pnt_beg = u16[0];
         let pnt_end = u16[2];
         let flags   = u16[4] flag LineFlags;
         let side_fr = OptU16[12];
         let side_bk = OptU16[14];
         let poly_fr = OptU16[16];
         let poly_bk = OptU16[18];
      }
   }

   Ok((Line{flags, pnt_beg, pnt_end, side_fr, side_bk, poly_fr, poly_bk}, 32))
}

/// A line segment.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
pub struct Line
{
   pub flags:   LineFlags,
   pub pnt_beg: u16,
   pub pnt_end: u16,
   pub side_fr: OptU16,
   pub side_bk: OptU16,
   pub poly_fr: OptU16,
   pub poly_bk: OptU16,
}

bitflags! {
   /// Flags for `Line`.
   #[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
   pub struct LineFlags: u16
   {
      const TRANS_SIDE  = 1 << 9;
      const ELEV_VAR    = 1 << 10;
      const ELEVATION   = 1 << 11;
      const LANDSCAPE   = 1 << 12;
      const TRANSPARENT = 1 << 13;
      const SOLID       = 1 << 14;
   }
}

// EOF
