use binrw::{BinReaderExt, binrw};

pub enum Error{
	InvalidHeader,
	InvalidNodeId(u64),
	InvalidRegion(binrw::Error),
	File(crate::file::Error),
}

/* block types

BLOCK_MAP_HEADER:
DefaultStyleInfo style_info
//bvh goes here
u64 num_nodes
//node 0 parent node is implied to be None
for node_id in 1..num_nodes{
	u64 parent_node
}

//NOTE: alternate realities are not necessary.
//portals/wormholes simply use an in-model and and out-model.
//skyboxes are inverted models with a special shader.

//ideally spacial blocks are sorted from distance to start zone
//texture blocks are inserted before the first spacial block they are used in
u64 num_spacial_blocks
for spacial_block_id in 0..num_spacial_blocks{
	u64 node_id
	u64 block_id //data block
	Aabb block_extents
}
//if the map file references external resources, num_resources = 0
u64 num_resources
for resource_id in 0..num_resources{
	u64 block_id
	u128 resource_id
}

BLOCK_MAP_RESOURCE:
Resource resource_type
//an individual one of the following:
	- model (IndexedModel)
	- shader (compiled SPIR-V)
	- image (JpegXL)
	- sound (Opus)
	- video (AV1)
	- animation (Trey thing)

BLOCK_MAP_REGION:
u64 num_models
for model_id in 0..num_models{
	u128 model_resource_uuid
	ModelInstance mode_instance
}

*/


//error hiding mock code
mod physics{
	pub struct StyleModifiers{}
}
mod model{
	pub struct IndexedModel{}
	#[super::binrw]
	#[brw(little)]
	pub struct ModelInstance{}
}
mod image{
	pub struct Image{}
}

//serious code

struct ModelUuid(u128);
struct ImageUuid(u128);
pub struct BvhNodeId(u64);
struct BvhNode{
	//
}
#[binrw]
#[brw(little)]
struct Region{
	#[bw(try_calc(u32::try_from(models.len())))]
	model_count:u32,
	#[br(count=model_count)]
	models:Vec<model::ModelInstance>,
}

pub struct StreamableMap<R:BinReaderExt>{
	file:crate::file::File<R>,
	style:physics::StyleModifiers,//probably should move this out of physics
	bvh:BvhNode,
	node_id_to_block_id:Vec<crate::file::BlockId>,
	//do not need this?  return only new data with load_node
	resource_model:std::collections::HashMap<ModelUuid,model::IndexedModel>,
	resource_image:std::collections::HashMap<ImageUuid,image::Image>,
}
impl<R:BinReaderExt> StreamableMap<R>{
	pub(crate) fn new(file:crate::file::File<R>)->Result<Self,Error>{
		Err(Error::InvalidHeader)
	}
	pub fn load_node(&mut self,node_id:BvhNodeId)->Result<Vec<model::ModelInstance>,Error>{
		//load region from disk
		//parse the models and determine what resources need to be loaded
		//load resources into self.resources
		//return Region
		let block_id=*self.node_id_to_block_id.get(node_id.0 as usize).ok_or(Error::InvalidNodeId(node_id.0))?;
		let mut block=self.file.take_block(block_id).map_err(|e|Error::File(e))?;
		let region:Region=block.read_le().map_err(|e|Error::InvalidRegion(e))?;
		Ok(region.models)
	}
}