//! `Line` type.

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

/// 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(Clone, 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,
}

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

// EOF
