#![allow(clippy::unit_arg)]

use maraiah::{err::*, file::*, image::*, machdr, map, shp, snd, sound::*};
use std::{collections::HashSet, fs, io, slice::from_ref};

/*
fn open(path: &str) -> ResultS<io::BufReader>
{
	let fp = fs::File::open(path)?;
	Ok(io::BufReader::new(fp))
}

fn file_read<T, F>(path: &str, f: F) -> ResultS<T>
	where F: FnOnce(&[u8]) -> ResultS<T>
{
	let mm = open(path)?;
	let bp = &mm[machdr::try_mac_header(&mm)..];

	f(bp)
}

fn exists(path: String) -> Result<(), String>
{
	match std::fs::metadata(path) {
		Ok(_) => Ok(()),
		Err(e) => Err(e.to_string()),
	}
}

fn each_value<F>(opt: &clap::ArgMatches<'_>,
                 name: &str,
                 mut f: F) -> ResultS<()>
	where F: FnMut(&str) -> ResultS<()>
{
	if let Some(values) = opt.values_of(name) {
		for value in values {
			f(value)?;
		}
	}

	Ok(())
}

fn dbg_info(data: impl std::fmt::Debug)
{
	println!("{:#?}", data);
}

fn dump_map(opt: &clap::ArgMatches<'_>, f: &str) -> ResultS<()>
{
	let mut cnks = HashSet::new();

	if let Some(opt_cnks) = opt.values_of("chunks") {
		for typ in opt_cnks {
			cnks.insert(typ);
		}
	}

	Ok(())
}

fn dump_shp(opt: &clap::ArgMatches<'_>, f: &str) -> ResultS<()>
{
	unimplemented!();
}

fn dump_snd(opt: &clap::ArgMatches<'_>, f: &str) -> ResultS<()>
{
	unimplemented!();
}

fn sub_data_c(_opt: &clap::ArgMatches<'_>) -> ResultS<()>
{
	unimplemented!();
}

fn sub_dump_c(opt: &clap::ArgMatches<'_>) -> ResultS<()>
{
	each_value(opt, "map", |f| dump_map(opt, f))?;
	each_value(opt, "shp", |f| dump_shp(opt, f))?;
	each_value(opt, "snd", |f| dump_snd(opt, f))?;

	Ok(())
}

fn sub_info_c(opt: &clap::ArgMatches<'_>) -> ResultS<()>
{
	each_value(opt, "map", |f| Ok(dbg_info(file_read(f, map::read)?)))?;
	each_value(opt, "shp", |f| Ok(dbg_info(file_read(f, shp::read)?)))?;
	each_value(opt, "snd", |f| Ok(dbg_info(file_read(f, snd::read)?)))?;

	Ok(())
}
*/

fn main() -> ResultS<()>
{
	use std::io::prelude::*;

	let inp = include_bytes!("../tests/data/map/Test.in");
	let mut rd = std::io::BufReader::new(&inp[..]);

	let mp = map::head::read(&mut rd).unwrap();
	let en = map::entr::read_all(&mp).unwrap();
	let ed = map::data::read_all(mp.head(), &en).unwrap();

	write!(&mut std::fs::File::create("dicks.txt").unwrap(), "{:#?}", ed);

	use clap::clap_app;

	let sub_data = clap_app! {
		@subcommand data =>
			(about: "Dumps data into a discrete folder/YAML format")
	};

	let sub_dump = clap_app! {
		@subcommand dump =>
			(about: "Dumps particular parts of data")
			(@arg chunks: -c --chunks [name]... "Dumps named chunks from an entry")
			(@group files =>
				(@attributes +required +multiple)
				(@arg map: -m --map [file]... {exists} "Loads Map files")
				(@arg shp: -s --shp [file]... {exists} "Loads Shapes files")
				(@arg snd: -n --snd [file]... {exists} "Loads Sounds files"))
	};

	let sub_info = clap_app! {
		@subcommand info =>
			(about: "Outputs debug info")
			(@group files =>
				(@attributes +required +multiple)
				(@arg map: -m --map [file]... {exists} "Loads Map files")
				(@arg shp: -s --shp [file]... {exists} "Loads Shapes files")
				(@arg snd: -n --snd [file]... {exists} "Loads Sounds files"))
	};

	let opt = clap_app! {
		(env!("CARGO_PKG_NAME")) =>
			(version:    maraiah::meta::version())
			(author:     maraiah::meta::authors().replace(':', ", "))
			(about:      maraiah::meta::description())
			(setting:    clap::AppSettings::SubcommandRequiredElseHelp)
			(subcommand: sub_data)
			(subcommand: sub_dump)
			(subcommand: sub_info)
	};

	let opt = opt.get_matches();

	match opt.subcommand() {
		("data", Some(opt)) => sub_data_c(opt)?,
		("dump", Some(opt)) => sub_dump_c(opt)?,
		("info", Some(opt)) => sub_info_c(opt)?,
		_ => unreachable!(),
	}
	*/

		Ok(())
}

// EOF
