use binrw::BinReaderExt;

pub mod file;
pub mod map;
pub mod bot;
pub mod demo;

pub enum Error{
	UnexpectedFourCC,
	Header(file::Error),
	Map(map::Error),
	Bot(bot::Error),
	Demo(demo::Error),
}

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

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

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

	#[test]
	fn test() {
	}
}