//! `LightFunc` type.

use crate::{err::*, fixed::Fixed};

/// Reads a `LightFunc` object.
pub fn read(b: &[u8]) -> ResultS<LightFunc>
{
	read_data! {
		endian: BIG, buf: b, size: 14, start: 0, data {
			let ftype   = u16[0] enum LightFuncType;
			let prd_nrm = u16[2];
			let prd_dta = u16[4];
			let val_nrm = Fixed[6];
			let val_dta = Fixed[10];
		}
	}

	Ok(LightFunc{ftype, prd_nrm, prd_dta, val_nrm, val_dta})
}

/// Writes a `LightFunc` object.
pub fn write(v: &LightFunc) -> Vec<u8>
{
	let mut o = Vec::with_capacity(14);
	o.extend(&(v.ftype as u16).to_be_bytes());
	o.extend(&v.prd_nrm.to_be_bytes());
	o.extend(&v.prd_dta.to_be_bytes());
	o.extend(&v.val_nrm.to_be_bytes());
	o.extend(&v.val_dta.to_be_bytes());
	o
}

/// A light function.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LightFunc {
	pub ftype:   LightFuncType,
	pub prd_nrm: u16,
	pub prd_dta: u16,
	pub val_nrm: Fixed,
	pub val_dta: Fixed,
}

c_enum! {
	/// The type of function for a `LightFunc`.
	#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
	pub enum LightFuncType: u16 {
		Constant    = 0,
		Linear      = 1,
		Smooth      = 2,
		Flicker     = 3,
		Random      = 4,
		Fluorescent = 5,
	}
}

// EOF
