use binrw::BinReaderExt;

mod newtypes;

mod file;
pub mod map;
pub mod bot;

#[derive(Debug)]
pub enum Error{
	UnexpectedFourCC,
	Header(file::Error),
	Map(map::Error),
	Bot(bot::Error),
}
impl std::fmt::Display for Error{
	fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
		write!(f,"{self:?}")
	}
}
impl std::error::Error for Error{}

pub enum SNF{
	Map(map::StreamableMap),
	Bot(bot::StreamableBot),
}

pub fn read_snf<R:BinReaderExt>(mut input:R)->Result<(file::Header,SNF),Error>{
	let file:file::Header=input.read_le().map_err(|e|Error::Header(file::Error::InvalidHeader(e)))?;
	let snf=match file.fourcc(){
		file::FourCC::Map=>SNF::Map(map::StreamableMap::new(input).map_err(Error::Map)?),
		file::FourCC::Bot=>SNF::Bot(bot::StreamableBot::new(input).map_err(Error::Bot)?),
	};
	Ok((file,snf))
}
pub fn read_map<R:BinReaderExt>(mut input:R)->Result<(file::Header,map::StreamableMap),Error>{
	let file:file::Header=input.read_le().map_err(|e|Error::Header(file::Error::InvalidHeader(e)))?;
	let map=match file.fourcc(){
		file::FourCC::Map=>map::StreamableMap::new(input).map_err(Error::Map)?,
		_=>return Err(Error::UnexpectedFourCC)
	};
	Ok((file,map))
}
pub fn read_bot<R:BinReaderExt>(mut input:R)->Result<(file::Header,bot::StreamableBot),Error>{
	let file:file::Header=input.read_le().map_err(|e|Error::Header(file::Error::InvalidHeader(e)))?;
	let bot=match file.fourcc(){
		file::FourCC::Bot=>bot::StreamableBot::new(input).map_err(Error::Bot)?,
		_=>return Err(Error::UnexpectedFourCC)
	};
	Ok((file,bot))
}

#[cfg(test)]
mod tests {
	//use super::*;

	#[test]
	fn test() {
	}
}
