//! `Light` type.

use super::pnts;
use crate::{bin::OptU16,
            err::*,
            fixed::{Angle, Fixed, Unit},
            xfer::TransferMode};

/// Reads a `medi` chunk.
pub fn read(b: &[u8]) -> ResultS<(Media, usize)>
{
	read_data! {
		endian: BIG, buf: b, size: 32, start: 0, data {
			let mtype   = u16[0] enum MediaType;
			let flags   = u8[3] flag MediaFlags;
			let control = u16[4];
			let dir     = Angle[6];
			let mag     = Unit[8];
			let hei_lo  = Unit[10];
			let hei_hi  = Unit[12];
			let orig    = pnts::read_o[14; 4];
			let hei_nrm = Unit[18];
			let min_lt  = Fixed[20];
			let texture = OptU16[24];
			let xfer    = u16[26] enum TransferMode;
		}
	}

	Ok((Media{mtype, flags, control, dir, mag, hei_lo, hei_hi, orig, hei_nrm,
	          min_lt, texture, xfer}, 32))
}

/// A media, as in a part of a polygon which goes up the middle of the wall.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Media {
	pub mtype:   MediaType,
	pub flags:   MediaFlags,
	pub control: u16,
	pub dir:     Angle,
	pub mag:     Unit,
	pub hei_lo:  Unit,
	pub hei_hi:  Unit,
	pub orig:    pnts::Point,
	pub hei_nrm: Unit,
	pub min_lt:  Fixed,
	pub texture: OptU16,
	pub xfer:    TransferMode,
}

c_enum! {
	/// The liquid type of a `Media`.
	#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
	pub enum MediaType: u16 {
		Water  = 0,
		Lava   = 1,
		Goo    = 2,
		Sewage = 3,
		Jjaro  = 4,
	}
}

c_bitfield! {
	/// Flags for `Media`.
	#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
	pub struct MediaFlags: u8 {
		OBSTRUCT = 0,
	}
}

// EOF
