//! `Terminal` type.

use super::{trmf, trmg};
use crate::{bin, err::*, text};

/// Reads a `term` chunk.
pub fn read(b: &[u8]) -> ResultS<(Terminal, usize)>
{
	read_data! {
		endian: BIG, buf: b, size: 10, start: 0, data {
			let end     = u16[0] usize;
			let encoded = u16[2];
			let lines   = u16[4];
			let group_n = u16[6] usize;
			let face_n  = u16[8] usize;
		}
	}

	let (i_grp, x) = bin::rd_array_num(&b[10..], group_n, trmg::read)?;
	let (faces, y) = bin::rd_array_num(&b[10 + x..], face_n, trmf::read)?;

	let text = ok!(b.get(10 + x + y..end), "not enough data")?;
	let text = if encoded != 0 {
		text::fuck_string(text)
	} else {
		text.to_vec()
	};

	let mut groups = Vec::with_capacity(group_n);

	for grp in &i_grp {
		let flags = grp.flags;
		let ttype = grp.ttype;
		let lines = grp.lines;
		let beg   = grp.beg;
		let len   = grp.len;
		let text = ok!(text.get(beg..beg + len), "bad offset")?;
		let text = text::mac_roman_cstr(text);

		groups.push(trmg::Group{flags, ttype, lines, text});
	}

	Ok((Terminal{lines, groups, faces}, end))
}

/// A terminal definition, with collections of groups and faces.
#[cfg_attr(feature = "serde_obj", derive(serde::Serialize))]
#[derive(Debug, Eq, PartialEq)]
pub struct Terminal {
	pub lines:  u16,
	pub groups: Vec<trmg::Group>,
	pub faces:  Vec<trmf::Face>,
}

// EOF
