//! Shapes file collection type.

use crate::{bin::rd_ofstable, err::*};
use super::{bmap, clut, fram, sequ};

/// Reads a `Collection`.
pub fn read(b: &[u8]) -> ResultS<Collection>
{
	read_data! {
		endian: BIG, buf: b, size: 544, start: 0, data {
			let version = u16[0];
			let cl_type = u16[2] enum CollectionType;
			let clr_num = u16[6] usize;
			let tab_num = u16[8] usize;
			let tab_ofs = u32[10] usize;
			let seq_num = u16[14] usize;
			let seq_ofs = u32[16] usize;
			let frm_num = u16[20] usize;
			let frm_ofs = u32[22] usize;
			let bmp_num = u16[26] usize;
			let bmp_ofs = u32[28] usize;
		}
	}

	if version != 3 {
		bail!("invalid collection definition");
	}

	let tabs = clut::read(b, tab_ofs, tab_num, clr_num)?;
	let bmps = rd_ofstable(b, bmp_ofs, bmp_num, bmap::read)?;
	let frms = rd_ofstable(b, frm_ofs, frm_num, fram::read)?;
	let seqs = rd_ofstable(b, seq_ofs, seq_num, sequ::read)?;

	Ok(Collection{ctyp: cl_type, tabs, bmps, frms, seqs})
}

/// A collection of color tables, bitmaps, frames and sequences.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Collection {
	/// The type of collection this is.
	pub ctyp: CollectionType,

	/// All of the color tables in this collection.
	pub tabs: Vec<clut::Clut>,

	/// All of the bitmaps in this collection.
	pub bmps: Vec<bmap::Bitmap>,

	/// All of the frames in this collection.
	pub frms: Vec<fram::Frame>,

	/// All of the sequences in this collection.
	pub seqs: Vec<sequ::Sequence>,
}

c_enum! {
	/// The type of a collection.
	#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
	pub enum CollectionType: u16 {
		Unused    = 0,
		Wall      = 1,
		Object    = 2,
		Interface = 3,
		Scenery   = 4,
	}
}

// EOF
