use std::collections::HashMap;
use crate::primitives;
use strafesnet_common::map;
use strafesnet_common::model;
use strafesnet_common::gameplay_modes;
use strafesnet_common::gameplay_style;
use strafesnet_common::gameplay_attributes as attr;
use strafesnet_common::integer::{self,vec3,Planar64,Planar64Vec3,Planar64Mat3,Planar64Affine3};
use strafesnet_common::model::RenderConfigId;
use strafesnet_common::updatable::Updatable;

fn class_is_a(class: &str, superclass: &str) -> bool {
	if class==superclass {
		return true
	}
	let class_descriptor=rbx_reflection_database::get().classes.get(class);
	if let Some(descriptor) = &class_descriptor {
		if let Some(class_super) = &descriptor.superclass {
			return class_is_a(&class_super, superclass)
		}
	}
	false
}
fn recursive_collect_superclass(objects: &mut std::vec::Vec<rbx_dom_weak::types::Ref>,dom: &rbx_dom_weak::WeakDom, instance: &rbx_dom_weak::Instance, superclass: &str){
	let mut stack=vec![instance];
	while let Some(item)=stack.pop(){
		for &referent in item.children(){
			if let Some(c)=dom.get_by_ref(referent){
				if class_is_a(c.class.as_str(),superclass){
					objects.push(c.referent());//copy ref
				}
				stack.push(c);
			}
		}
	}
}
fn planar64_affine3_from_roblox(cf:&rbx_dom_weak::types::CFrame,size:&rbx_dom_weak::types::Vector3)->Planar64Affine3{
	Planar64Affine3::new(
		Planar64Mat3::from_cols([
			vec3::try_from_f32_array([cf.orientation.x.x,cf.orientation.y.x,cf.orientation.z.x]).unwrap()
			*integer::try_from_f32(size.x/2.0).unwrap(),
			vec3::try_from_f32_array([cf.orientation.x.y,cf.orientation.y.y,cf.orientation.z.y]).unwrap()
			*integer::try_from_f32(size.y/2.0).unwrap(),
			vec3::try_from_f32_array([cf.orientation.x.z,cf.orientation.y.z,cf.orientation.z.z]).unwrap()
			*integer::try_from_f32(size.z/2.0).unwrap(),
			].map(|t|t.fix_1())),
		vec3::try_from_f32_array([cf.position.x,cf.position.y,cf.position.z]).unwrap()
	)
}
struct ModeBuilder{
	mode:gameplay_modes::Mode,
	final_stage_id_from_builder_stage_id:HashMap<gameplay_modes::StageId,gameplay_modes::StageId>,
}
#[derive(Default)]
struct ModesBuilder{
	modes:HashMap<gameplay_modes::ModeId,gameplay_modes::Mode>,
	stages:HashMap<gameplay_modes::ModeId,HashMap<gameplay_modes::StageId,gameplay_modes::Stage>>,
	mode_updates:Vec<(gameplay_modes::ModeId,gameplay_modes::ModeUpdate)>,
	stage_updates:Vec<(gameplay_modes::ModeId,gameplay_modes::StageId,gameplay_modes::StageUpdate)>,
}
impl ModesBuilder{
	fn build(mut self)->gameplay_modes::Modes{
		//collect modes and stages into contiguous arrays
		let mut unique_modes:Vec<(gameplay_modes::ModeId,gameplay_modes::Mode)>
		=self.modes.into_iter().collect();
		unique_modes.sort_by_key(|&(mode_id,_)|mode_id);
		let (mut modes,final_mode_id_from_builder_mode_id):(Vec<ModeBuilder>,HashMap<gameplay_modes::ModeId,gameplay_modes::ModeId>)
		=unique_modes.into_iter().enumerate()
		.map(|(final_mode_id,(builder_mode_id,mut mode))|{
			(
				ModeBuilder{
					final_stage_id_from_builder_stage_id:self.stages.remove(&builder_mode_id).map_or_else(||HashMap::new(),|stages|{
						let mut unique_stages:Vec<(gameplay_modes::StageId,gameplay_modes::Stage)>
						=stages.into_iter().collect();
						unique_stages.sort_by(|a,b|a.0.cmp(&b.0));
						unique_stages.into_iter().enumerate()
						.map(|(final_stage_id,(builder_stage_id,stage))|{
							mode.push_stage(stage);
							(builder_stage_id,gameplay_modes::StageId::new(final_stage_id as u32))
						}).collect()
					}),
					mode,
				},
				(
					builder_mode_id,
					gameplay_modes::ModeId::new(final_mode_id as u32)
				)
			)
		}).unzip();
		//TODO: failure messages or errors or something
		//push stage updates
		for (builder_mode_id,builder_stage_id,stage_update) in self.stage_updates{
			if let Some(final_mode_id)=final_mode_id_from_builder_mode_id.get(&builder_mode_id){
				if let Some(mode)=modes.get_mut(final_mode_id.get() as usize){
					if let Some(&final_stage_id)=mode.final_stage_id_from_builder_stage_id.get(&builder_stage_id){
						if let Some(stage)=mode.mode.get_stage_mut(final_stage_id){
							stage.update(stage_update);
						}
					}
				}
			}
		}
		//push mode updates
		for (builder_mode_id,mut mode_update) in self.mode_updates{
			if let Some(final_mode_id)=final_mode_id_from_builder_mode_id.get(&builder_mode_id){
				if let Some(mode)=modes.get_mut(final_mode_id.get() as usize){
					//map stage id on stage elements
					mode_update.map_stage_element_ids(|stage_id|
						//walk down one stage id at a time until a stage is found
						//TODO use better logic like BTreeMap::upper_bound instead of walking
						// final_stage_id_from_builder_stage_id.upper_bound(Bound::Included(&stage_id))
						// .value().copied().unwrap_or(gameplay_modes::StageId::FIRST)
						(0..=stage_id.get()).rev().find_map(|builder_stage_id|
							//map the stage element to that stage
							mode.final_stage_id_from_builder_stage_id.get(&gameplay_modes::StageId::new(builder_stage_id)).copied()
						).unwrap_or(gameplay_modes::StageId::FIRST)
					);
					mode.mode.update(mode_update);
				}
			}
		}
		gameplay_modes::Modes::new(modes.into_iter().map(|mode_builder|mode_builder.mode).collect())
	}
	fn insert_mode(&mut self,mode_id:gameplay_modes::ModeId,mode:gameplay_modes::Mode){
		assert!(self.modes.insert(mode_id,mode).is_none(),"Cannot replace existing mode");
	}
	fn insert_stage(&mut self,mode_id:gameplay_modes::ModeId,stage_id:gameplay_modes::StageId,stage:gameplay_modes::Stage){
		assert!(self.stages.entry(mode_id).or_insert(HashMap::new()).insert(stage_id,stage).is_none(),"Cannot replace existing stage");
	}
	fn push_mode_update(&mut self,mode_id:gameplay_modes::ModeId,mode_update:gameplay_modes::ModeUpdate){
		self.mode_updates.push((mode_id,mode_update));
	}
	fn push_stage_update(&mut self,mode_id:gameplay_modes::ModeId,stage_id:gameplay_modes::StageId,stage_update:gameplay_modes::StageUpdate){
		self.stage_updates.push((mode_id,stage_id,stage_update));
	}
}
fn get_attributes(name:&str,can_collide:bool,velocity:Planar64Vec3,model_id:model::ModelId,modes_builder:&mut ModesBuilder,wormhole_in_model_to_id:&mut HashMap<model::ModelId,u32>,wormhole_id_to_out_model:&mut HashMap<u32,model::ModelId>)->attr::CollisionAttributes{
	let mut general=attr::GeneralAttributes::default();
	let mut intersecting=attr::IntersectingAttributes::default();
	let mut contacting=attr::ContactingAttributes::default();
	let mut force_can_collide=can_collide;
	let mut force_intersecting=false;
	match name{
		"Water"=>{
			force_can_collide=false;
			//TODO: read stupid CustomPhysicalProperties
			intersecting.water=Some(attr::IntersectingWater{density:Planar64::ONE,viscosity:Planar64::ONE/10,velocity});
		},
		"Accelerator"=>{
			//although the new game supports collidable accelerators, this is a roblox compatability map loader
			force_can_collide=false;
			general.accelerator=Some(attr::Accelerator{acceleration:velocity});
		},
		// "UnorderedCheckpoint"=>general.teleport_behaviour=Some(model::TeleportBehaviour::StageElement(attr::StageElement{
		// 	mode_id:0,
		// 	stage_id:0,
		// 	force:false,
		// 	behaviour:model::StageElementBehaviour::Unordered
		// })),
		"SetVelocity"=>general.trajectory=Some(attr::SetTrajectory::Velocity(velocity)),
		"MapStart"=>{
			force_can_collide=false;
			force_intersecting=true;
			modes_builder.insert_mode(
				gameplay_modes::ModeId::MAIN,
				gameplay_modes::Mode::empty(
					gameplay_style::StyleModifiers::roblox_bhop(),
					model_id
				)
			);
		},
		"MapFinish"=>{
			force_can_collide=false;
			force_intersecting=true;
			modes_builder.push_mode_update(
				gameplay_modes::ModeId::MAIN,
				gameplay_modes::ModeUpdate::zone(
					model_id,
					gameplay_modes::Zone::Finish,
				),
			);
		},
		"MapAnticheat"=>{
			force_can_collide=false;
			force_intersecting=true;
			modes_builder.push_mode_update(
				gameplay_modes::ModeId::MAIN,
				gameplay_modes::ModeUpdate::zone(
					model_id,
					gameplay_modes::Zone::Anticheat,
				),
			);
		},
		"Platform"=>{
			modes_builder.push_mode_update(
				gameplay_modes::ModeId::MAIN,
				gameplay_modes::ModeUpdate::element(
					model_id,
					gameplay_modes::StageElement::new(gameplay_modes::StageId::FIRST,false,gameplay_modes::StageElementBehaviour::Platform,None),//roblox does not know which stage the platform belongs to
				),
			);
		},
		other=>{
			let regman=lazy_regex::regex!(r"^(BonusStart|WormholeOut)(\d+)$");
			if let Some(captures)=regman.captures(other){
				match &captures[1]{
					"BonusStart"=>{
						force_can_collide=false;
						force_intersecting=true;
						modes_builder.insert_mode(
							gameplay_modes::ModeId::new(captures[2].parse::<u32>().unwrap()),
							gameplay_modes::Mode::empty(
								gameplay_style::StyleModifiers::roblox_bhop(),
								model_id
							)
						);
					},
					"WormholeOut"=>{
						//the PhysicsModelId has to exist for it to be teleported to!
						force_intersecting=true;
						//this object is not special in strafe client, but the roblox mapping needs to be converted to model id
						assert!(wormhole_id_to_out_model.insert(captures[2].parse::<u32>().unwrap(),model_id).is_none(),"Cannot have multiple WormholeOut with same id");
					},
					_=>(),
				}
			}else if let Some(captures)=lazy_regex::regex!(r"^(Force)?(Spawn|SpawnAt|Trigger|Teleport|Platform)(\d+)$")
			.captures(other){
				force_intersecting=true;
				let stage_id=gameplay_modes::StageId::new(captures[3].parse::<u32>().unwrap());
				let stage_element=gameplay_modes::StageElement::new(
					//stage_id:
					stage_id,
					//force:
					match captures.get(1){
						Some(m)=>m.as_str()=="Force",
						None=>false,
					},
					//behaviour:
					match &captures[2]{
						"Spawn"=>{
							modes_builder.insert_stage(
								gameplay_modes::ModeId::MAIN,
								stage_id,
								gameplay_modes::Stage::empty(model_id),
							);
							//TODO: let denormalize handle this
							gameplay_modes::StageElementBehaviour::SpawnAt
						},
						"SpawnAt"=>gameplay_modes::StageElementBehaviour::SpawnAt,
						//cancollide false so you don't hit the side
						//NOT a decoration
						"Trigger"=>{force_can_collide=false;gameplay_modes::StageElementBehaviour::Trigger},
						"Teleport"=>{force_can_collide=false;gameplay_modes::StageElementBehaviour::Teleport},
						"Platform"=>gameplay_modes::StageElementBehaviour::Platform,
						_=>panic!("regex1[2] messed up bad"),
					},
					None
				);
				modes_builder.push_mode_update(
					gameplay_modes::ModeId::MAIN,
					gameplay_modes::ModeUpdate::element(
						model_id,
						stage_element,
					),
				);
			}else if let Some(captures)=lazy_regex::regex!(r"^(Jump|WormholeIn)(\d+)$")
			.captures(other){
				match &captures[1]{
					"Jump"=>modes_builder.push_mode_update(
						gameplay_modes::ModeId::MAIN,
						gameplay_modes::ModeUpdate::element(
							model_id,
							//jump_limit:
							gameplay_modes::StageElement::new(
								gameplay_modes::StageId::FIRST,
								false,
								gameplay_modes::StageElementBehaviour::Check,
								Some(captures[2].parse::<u8>().unwrap())
							)
						),
					),
					"WormholeIn"=>{
						force_can_collide=false;
						force_intersecting=true;
						assert!(wormhole_in_model_to_id.insert(model_id,captures[2].parse::<u32>().unwrap()).is_none(),"Impossible");
					},
					_=>panic!("regex2[1] messed up bad"),
				}
			}else if let Some(captures)=lazy_regex::regex!(r"^Bonus(Finish|Anticheat)(\d+)$")
			.captures(other){
				force_can_collide=false;
				force_intersecting=true;
				modes_builder.push_mode_update(
					gameplay_modes::ModeId::new(captures[2].parse::<u32>().unwrap()),
					gameplay_modes::ModeUpdate::zone(
						model_id,
						//zone:
						match &captures[1]{
							"Finish"=>gameplay_modes::Zone::Finish,
							"Anticheat"=>gameplay_modes::Zone::Anticheat,
							_=>panic!("regex3[1] messed up bad"),
						},
					),
				);
			}
			// else if let Some(captures)=lazy_regex::regex!(r"^Stage(\d+)OrderedCheckpoint(\d+)$")
			// .captures(other){
			// 	match &captures[1]{
			// 		"OrderedCheckpoint"=>modes_builder.push_stage_update(
			// 			gameplay_modes::ModeId::MAIN,
			// 			gameplay_modes::StageId::new(0),
			// 			gameplay_modes::StageUpdate::ordered_checkpoint(captures[2].parse::<u32>().unwrap()),
			// 		),
			// 		_=>panic!("regex3[1] messed up bad"),
			// 	}
			// }
		}
	}
	//need some way to skip this
	if velocity!=vec3::ZERO{
		general.booster=Some(attr::Booster::Velocity(velocity));
	}
	match force_can_collide{
		true=>{
			match name{
				"Bounce"=>contacting.contact_behaviour=Some(attr::ContactingBehaviour::Elastic(u32::MAX)),
				"Surf"=>contacting.contact_behaviour=Some(attr::ContactingBehaviour::Surf),
				"Ladder"=>contacting.contact_behaviour=Some(attr::ContactingBehaviour::Ladder(attr::ContactingLadder{sticky:true})),
				_=>(),
			}
			attr::CollisionAttributes::Contact(attr::ContactAttributes{contacting,general})
		},
		false=>if force_intersecting
		||general.any()
		||intersecting.any()
		{
			attr::CollisionAttributes::Intersect(attr::IntersectAttributes{intersecting,general})
		}else{
			attr::CollisionAttributes::Decoration
		},
	}
}

#[derive(Clone,Copy,PartialEq)]
struct RobloxTextureTransform{
	offset_u:f32,
	offset_v:f32,
	scale_u:f32,
	scale_v:f32,
}
impl std::cmp::Eq for RobloxTextureTransform{}//????
impl std::default::Default for RobloxTextureTransform{
    fn default()->Self{
        Self{offset_u:0.0,offset_v:0.0,scale_u:1.0,scale_v:1.0}
    }
}
impl std::hash::Hash for RobloxTextureTransform{
	fn hash<H:std::hash::Hasher>(&self,state:&mut H) {
		self.offset_u.to_ne_bytes().hash(state);
		self.offset_v.to_ne_bytes().hash(state);
		self.scale_u.to_ne_bytes().hash(state);
		self.scale_v.to_ne_bytes().hash(state);
	}
}
#[derive(Clone,PartialEq)]
struct RobloxFaceTextureDescription{
	render:RenderConfigId,
	color:glam::Vec4,
	transform:RobloxTextureTransform,
}
impl std::cmp::Eq for RobloxFaceTextureDescription{}//????
impl std::hash::Hash for RobloxFaceTextureDescription{
	fn hash<H:std::hash::Hasher>(&self,state:&mut H){
		self.render.hash(state);
		self.transform.hash(state);
        for &el in self.color.as_ref().iter(){
            el.to_ne_bytes().hash(state);
        }
    }
}
impl RobloxFaceTextureDescription{
	fn to_face_description(&self)->primitives::FaceDescription{
		primitives::FaceDescription{
			render:self.render,
			transform:glam::Affine2::from_translation(
				glam::vec2(self.transform.offset_u,self.transform.offset_v)
			)
			*glam::Affine2::from_scale(
				glam::vec2(self.transform.scale_u,self.transform.scale_v)
			),
			color:self.color,
		}
	}
}
type RobloxPartDescription=[Option<RobloxFaceTextureDescription>;6];
type RobloxWedgeDescription=[Option<RobloxFaceTextureDescription>;5];
type RobloxCornerWedgeDescription=[Option<RobloxFaceTextureDescription>;5];
#[derive(Clone,Eq,Hash,PartialEq)]
enum RobloxBasePartDescription{
	Sphere(RobloxPartDescription),
	Part(RobloxPartDescription),
	Cylinder(RobloxPartDescription),
	Wedge(RobloxWedgeDescription),
	CornerWedge(RobloxCornerWedgeDescription),
}
enum Shape{
	Primitive(primitives::Primitives),
	MeshPart,
}
enum MeshAvailability{
	Immediate,
	Deferred(RenderConfigId),
}
struct DeferredModelDeferredAttributes{
	render:RenderConfigId,
	model:ModelDeferredAttributes,
}
struct ModelDeferredAttributes{
	mesh:model::MeshId,
	deferred_attributes:GetAttributesArgs,
	color:model::Color4,//transparency is in here
	transform:Planar64Affine3,
}
struct ModelOwnedAttributes{
	mesh:model::MeshId,
	attributes:attr::CollisionAttributes,
	color:model::Color4,//transparency is in here
	transform:Planar64Affine3,
}
struct GetAttributesArgs{
	name:Box<str>,
	can_collide:bool,
	velocity:Planar64Vec3,
}
pub fn convert<AcquireRenderConfigId,AcquireMeshId>(
	dom:&rbx_dom_weak::WeakDom,
	mut acquire_render_config_id:AcquireRenderConfigId,
	mut acquire_mesh_id:AcquireMeshId,
)->PartialMap1
where
	AcquireRenderConfigId:FnMut(Option<&str>)->model::RenderConfigId,
	AcquireMeshId:FnMut(&str)->model::MeshId,
{

	let mut deferred_models_deferred_attributes=Vec::new();
	let mut primitive_models_deferred_attributes=Vec::new();
	let mut primitive_meshes=Vec::new();
	let mut mesh_id_from_description=HashMap::new();

	//just going to leave it like this for now instead of reworking the data structures for this whole thing
	let textureless_render_group=acquire_render_config_id(None);

	let mut object_refs=Vec::new();
	let mut temp_objects=Vec::new();
	recursive_collect_superclass(&mut object_refs, &dom, dom.root(),"BasePart");
	for object_ref in object_refs {
		if let Some(object)=dom.get_by_ref(object_ref){
			if let (
					Some(rbx_dom_weak::types::Variant::CFrame(cf)),
					Some(rbx_dom_weak::types::Variant::Vector3(size)),
					Some(rbx_dom_weak::types::Variant::Vector3(velocity)),
					Some(rbx_dom_weak::types::Variant::Float32(transparency)),
					Some(rbx_dom_weak::types::Variant::Color3uint8(color3)),
					Some(rbx_dom_weak::types::Variant::Bool(can_collide)),
				) = (
					object.properties.get("CFrame"),
					object.properties.get("Size"),
					object.properties.get("Velocity"),
					object.properties.get("Transparency"),
					object.properties.get("Color"),
					object.properties.get("CanCollide"),
				)
			{
				let model_transform=planar64_affine3_from_roblox(cf,size);

				if model_transform.matrix3.det().is_zero(){
					let mut parent_ref=object.parent();
					let mut full_path=object.name.clone();
					while let Some(parent)=dom.get_by_ref(parent_ref){
						full_path=format!("{}.{}",parent.name,full_path);
						parent_ref=parent.parent();
					}
					println!("Zero determinant CFrame at location {}",full_path);
					println!("matrix3:{}",model_transform.matrix3);
					continue;
				}

				//at this point a new model is going to be generated for sure.
				let model_id=model::ModelId::new(primitive_models_deferred_attributes.len() as u32);

				//TODO: also detect "CylinderMesh" etc here
				let shape=match object.class.as_str(){
					"Part"=>if let Some(rbx_dom_weak::types::Variant::Enum(shape))=object.properties.get("Shape"){
						Shape::Primitive(match shape.to_u32(){
							0=>primitives::Primitives::Sphere,
							1=>primitives::Primitives::Cube,
							2=>primitives::Primitives::Cylinder,
							3=>primitives::Primitives::Wedge,
							4=>primitives::Primitives::CornerWedge,
							other=>panic!("Funky roblox PartType={};",other),
						})
					}else{
						panic!("Part has no Shape!");
					},
					"TrussPart"=>Shape::Primitive(primitives::Primitives::Cube),
					"WedgePart"=>Shape::Primitive(primitives::Primitives::Wedge),
					"CornerWedgePart"=>Shape::Primitive(primitives::Primitives::CornerWedge),
					"MeshPart"=>Shape::MeshPart,
					_=>{
						println!("Unsupported BasePart ClassName={}; defaulting to cube",object.class);
						Shape::Primitive(primitives::Primitives::Cube)
					}
				};

				let (availability,mesh_id)=match shape{
					Shape::Primitive(primitive_shape)=>{
						//TODO: TAB TAB
				//use the biggest one and cut it down later...
				let mut part_texture_description:RobloxPartDescription=[None,None,None,None,None,None];
				temp_objects.clear();
				recursive_collect_superclass(&mut temp_objects, &dom, object,"Decal");
				for &decal_ref in &temp_objects{
					if let Some(decal)=dom.get_by_ref(decal_ref){
						if let (
							Some(rbx_dom_weak::types::Variant::Content(content)),
							Some(rbx_dom_weak::types::Variant::Enum(normalid)),
							Some(rbx_dom_weak::types::Variant::Color3(decal_color3)),
							Some(rbx_dom_weak::types::Variant::Float32(decal_transparency)),
						) = (
							decal.properties.get("Texture"),
							decal.properties.get("Face"),
							decal.properties.get("Color3"),
							decal.properties.get("Transparency"),
						) {
							let render_id=acquire_render_config_id(Some(content.as_ref()));
								let normal_id=normalid.to_u32();
								if normal_id<6{
									let (roblox_texture_color,roblox_texture_transform)=if decal.class=="Texture"{
										//generate tranform
										if let (
												Some(rbx_dom_weak::types::Variant::Float32(ox)),
												Some(rbx_dom_weak::types::Variant::Float32(oy)),
												Some(rbx_dom_weak::types::Variant::Float32(sx)),
												Some(rbx_dom_weak::types::Variant::Float32(sy)),
											) = (
												decal.properties.get("OffsetStudsU"),
												decal.properties.get("OffsetStudsV"),
												decal.properties.get("StudsPerTileU"),
												decal.properties.get("StudsPerTileV"),
											)
										{
											let (size_u,size_v)=match normal_id{
												0=>(size.z,size.y),//right
												1=>(size.x,size.z),//top
												2=>(size.x,size.y),//back
												3=>(size.z,size.y),//left
												4=>(size.x,size.z),//bottom
												5=>(size.x,size.y),//front
												_=>unreachable!(),
											};
											(
												glam::vec4(decal_color3.r,decal_color3.g,decal_color3.b,1.0-*decal_transparency),
												RobloxTextureTransform{
													offset_u:*ox/(*sx),offset_v:*oy/(*sy),
													scale_u:size_u/(*sx),scale_v:size_v/(*sy),
												}
											)
										}else{
											(glam::Vec4::ONE,RobloxTextureTransform::default())
										}
									}else{
										(glam::Vec4::ONE,RobloxTextureTransform::default())
									};
									part_texture_description[normal_id as usize]=Some(RobloxFaceTextureDescription{
										render:render_id,
										color:roblox_texture_color,
										transform:roblox_texture_transform,
									});
								}else{
									println!("NormalId={} unsupported for shape={:?}",normal_id,primitive_shape);
								}
						}
					}
				}
				//obscure rust syntax "slice pattern"
				let [
					f0,//Cube::Right
					f1,//Cube::Top
					f2,//Cube::Back
					f3,//Cube::Left
					f4,//Cube::Bottom
					f5,//Cube::Front
				]=part_texture_description;
				let basepart_description=match primitive_shape{
					primitives::Primitives::Sphere=>RobloxBasePartDescription::Sphere([f0,f1,f2,f3,f4,f5]),
					primitives::Primitives::Cube=>RobloxBasePartDescription::Part([f0,f1,f2,f3,f4,f5]),
					primitives::Primitives::Cylinder=>RobloxBasePartDescription::Cylinder([f0,f1,f2,f3,f4,f5]),
					//use front face texture first and use top face texture as a fallback
					primitives::Primitives::Wedge=>RobloxBasePartDescription::Wedge([
						f0,//Cube::Right->Wedge::Right
						if f5.is_some(){f5}else{f1},//Cube::Front|Cube::Top->Wedge::TopFront
						f2,//Cube::Back->Wedge::Back
						f3,//Cube::Left->Wedge::Left
						f4,//Cube::Bottom->Wedge::Bottom
					]),
					//TODO: fix Left+Back texture coordinates to match roblox when not overwridden by Top
					primitives::Primitives::CornerWedge=>RobloxBasePartDescription::CornerWedge([
						f0,//Cube::Right->CornerWedge::Right
						if f2.is_some(){f2}else{f1.clone()},//Cube::Back|Cube::Top->CornerWedge::TopBack
						if f3.is_some(){f3}else{f1},//Cube::Left|Cube::Top->CornerWedge::TopLeft
						f4,//Cube::Bottom->CornerWedge::Bottom
						f5,//Cube::Front->CornerWedge::Front
					]),
				};
				//make new model if unit cube has not been created before
				let mesh_id=if let Some(&mesh_id)=mesh_id_from_description.get(&basepart_description){
					//push to existing texture model
					mesh_id
				}else{
					let mesh_id=model::MeshId::new(primitive_meshes.len() as u32);
					mesh_id_from_description.insert(basepart_description.clone(),mesh_id);//borrow checker going crazy
					let mesh=match basepart_description{
						RobloxBasePartDescription::Sphere(part_texture_description)
						|RobloxBasePartDescription::Cylinder(part_texture_description)
						|RobloxBasePartDescription::Part(part_texture_description)=>{
							let mut cube_face_description=primitives::CubeFaceDescription::default();
							for (face_id,roblox_face_description) in part_texture_description.iter().enumerate(){
								cube_face_description.insert(
								match face_id{
									0=>primitives::CubeFace::Right,
									1=>primitives::CubeFace::Top,
									2=>primitives::CubeFace::Back,
									3=>primitives::CubeFace::Left,
									4=>primitives::CubeFace::Bottom,
									5=>primitives::CubeFace::Front,
									_=>unreachable!(),
								},
								match roblox_face_description{
									Some(roblox_texture_transform)=>roblox_texture_transform.to_face_description(),
									None=>primitives::FaceDescription::new_with_render_id(textureless_render_group),
								});
							}
							primitives::generate_partial_unit_cube(cube_face_description)
						},
						RobloxBasePartDescription::Wedge(wedge_texture_description)=>{
							let mut wedge_face_description=primitives::WedgeFaceDescription::default();
							for (face_id,roblox_face_description) in wedge_texture_description.iter().enumerate(){
								wedge_face_description.insert(
								match face_id{
									0=>primitives::WedgeFace::Right,
									1=>primitives::WedgeFace::TopFront,
									2=>primitives::WedgeFace::Back,
									3=>primitives::WedgeFace::Left,
									4=>primitives::WedgeFace::Bottom,
									_=>unreachable!(),
								},
								match roblox_face_description{
									Some(roblox_texture_transform)=>roblox_texture_transform.to_face_description(),
									None=>primitives::FaceDescription::new_with_render_id(textureless_render_group),
								});
							}
							primitives::generate_partial_unit_wedge(wedge_face_description)
						},
						RobloxBasePartDescription::CornerWedge(cornerwedge_texture_description)=>{
							let mut cornerwedge_face_description=primitives::CornerWedgeFaceDescription::default();
							for (face_id,roblox_face_description) in cornerwedge_texture_description.iter().enumerate(){
								cornerwedge_face_description.insert(
								match face_id{
									0=>primitives::CornerWedgeFace::Right,
									1=>primitives::CornerWedgeFace::TopBack,
									2=>primitives::CornerWedgeFace::TopLeft,
									3=>primitives::CornerWedgeFace::Bottom,
									4=>primitives::CornerWedgeFace::Front,
									_=>unreachable!(),
								},
								match roblox_face_description{
									Some(roblox_texture_transform)=>roblox_texture_transform.to_face_description(),
									None=>primitives::FaceDescription::new_with_render_id(textureless_render_group),
								});
							}
							primitives::generate_partial_unit_cornerwedge(cornerwedge_face_description)
						},
					};
					primitive_meshes.push(mesh);
					mesh_id
				};
				(MeshAvailability::Immediate,mesh_id)
					},
					Shape::MeshPart=>if let (
						Some(rbx_dom_weak::types::Variant::Content(mesh_asset_id)),
						Some(rbx_dom_weak::types::Variant::Content(texture_asset_id)),
					)=(
						object.properties.get("MeshId"),
						object.properties.get("TextureID"),
					){
						(
							MeshAvailability::Deferred(acquire_render_config_id(Some(texture_asset_id.as_ref()))),
							acquire_mesh_id(mesh_asset_id.as_ref()),
						)
					}else{
						panic!("Mesh has no Mesh or Texture");
					},
				};
				let model_deferred_attributes=ModelDeferredAttributes{
					mesh:mesh_id,
					transform:model_transform,
					color:glam::vec4(color3.r as f32/255f32, color3.g as f32/255f32, color3.b as f32/255f32, 1.0-*transparency),
					deferred_attributes:GetAttributesArgs{
						name:object.name.as_str().into(),
						can_collide:*can_collide,
						velocity:vec3::try_from_f32_array([velocity.x,velocity.y,velocity.z]).unwrap(),
					},
				};
				match availability{
					MeshAvailability::Immediate=>primitive_models_deferred_attributes.push(model_deferred_attributes),
					MeshAvailability::Deferred(render)=>deferred_models_deferred_attributes.push(DeferredModelDeferredAttributes{
						render,
						model:model_deferred_attributes
					}),
				}
			}
		}
	}
	PartialMap1{
		primitive_meshes,
		primitive_models_deferred_attributes,
		deferred_models_deferred_attributes,
	}
}
struct MeshWithAabb{
	mesh:model::Mesh,
	aabb:strafesnet_common::aabb::Aabb,
}
pub struct PartialMap1{
	primitive_meshes:Vec<model::Mesh>,
	primitive_models_deferred_attributes:Vec<ModelDeferredAttributes>,
	deferred_models_deferred_attributes:Vec<DeferredModelDeferredAttributes>,
}
impl PartialMap1{
	pub fn add_meshpart_meshes_and_calculate_attributes(
		mut self,
		meshpart_meshes:impl IntoIterator<Item=(model::MeshId,crate::data::RobloxMeshBytes)>,
	)->PartialMap2{
		//calculate attributes
		let mut modes_builder=ModesBuilder::default();
		let mut unique_attributes=Vec::new();
		let mut attributes_id_from_attributes=HashMap::new();

		let mut wormhole_in_model_to_id=HashMap::new();
		let mut wormhole_id_to_out_model=HashMap::new();

		//decode roblox meshes
		//generate mesh_id_map based on meshes that failed to load
		let loaded_meshes:HashMap<model::MeshId,MeshWithAabb>=
		meshpart_meshes.into_iter().flat_map(|(old_mesh_id,roblox_mesh_bytes)|
			match crate::mesh::convert(roblox_mesh_bytes){
				Ok(mesh)=>{
					let mut aabb=strafesnet_common::aabb::Aabb::default();
					for &pos in &mesh.unique_pos{
						aabb.grow(pos);
					}
					Some((old_mesh_id,MeshWithAabb{
						mesh,
						aabb,
					}))
				},
				Err(e)=>{
					println!("Error converting mesh: {e:?}");
					None
				},
			}
		).collect();

		let mut mesh_id_from_render_config_id=HashMap::new();
		//ignore meshes that fail to load completely for now
		let mut acquire_mesh_id_from_render_config_id=|old_mesh_id,render|{
			loaded_meshes.get(&old_mesh_id).map(|mesh_with_aabb|(
				*mesh_id_from_render_config_id.entry(old_mesh_id).or_insert_with(||HashMap::new())
				.entry(render).or_insert_with(||{
					let mesh_id=model::MeshId::new(self.primitive_meshes.len() as u32);
					let mut mesh_clone=mesh_with_aabb.mesh.clone();
					//add a render group lool
					mesh_clone.graphics_groups.push(model::IndexedGraphicsGroup{
						render,
						//the lowest lod is highest quality
						groups:vec![model::PolygonGroupId::new(0)]
					});
					self.primitive_meshes.push(mesh_clone);
					mesh_id
				}),
				&mesh_with_aabb.aabb,
			))
		};
		//now that the meshes are loaded, these models can be generated
		let models_owned_attributes:Vec<ModelOwnedAttributes>=
		self.deferred_models_deferred_attributes.into_iter().flat_map(|deferred_model_deferred_attributes|{
			//meshes need to be cloned from loaded_meshes with a new id when they are used with a new render_id
			//insert into primitive_meshes
			let (mesh,aabb)=acquire_mesh_id_from_render_config_id(
				deferred_model_deferred_attributes.model.mesh,
				deferred_model_deferred_attributes.render
			)?;
			let size=aabb.size();
			Some(ModelDeferredAttributes{
				mesh,
				deferred_attributes:deferred_model_deferred_attributes.model.deferred_attributes,
				color:deferred_model_deferred_attributes.model.color,
				transform:Planar64Affine3::new(
					Planar64Mat3::from_cols([
						(deferred_model_deferred_attributes.model.transform.matrix3.x_axis*2/size.x).divide().fix_1(),
						(deferred_model_deferred_attributes.model.transform.matrix3.y_axis*2/size.y).divide().fix_1(),
						(deferred_model_deferred_attributes.model.transform.matrix3.z_axis*2/size.z).divide().fix_1()
					]),
					deferred_model_deferred_attributes.model.transform.translation
				),
			})
		}).chain(self.primitive_models_deferred_attributes.into_iter())
		.enumerate().map(|(model_id,model_deferred_attributes)|{
			let model_id=model::ModelId::new(model_id as u32);
			ModelOwnedAttributes{
				mesh:model_deferred_attributes.mesh,
				attributes:get_attributes(
					&model_deferred_attributes.deferred_attributes.name,
					model_deferred_attributes.deferred_attributes.can_collide,
					model_deferred_attributes.deferred_attributes.velocity,
					model_id,
					&mut modes_builder,
					&mut wormhole_in_model_to_id,
					&mut wormhole_id_to_out_model,
				),
				color:model_deferred_attributes.color,
				transform:model_deferred_attributes.transform,
			}
		}).collect();
		let models=models_owned_attributes.into_iter().enumerate().map(|(model_id,mut model_owned_attributes)|{
		//TODO: TAB
		let model_id=model::ModelId::new(model_id as u32);
		//update attributes with wormhole id
		//TODO: errors/prints
		if let Some(wormhole_id)=wormhole_in_model_to_id.get(&model_id){
			if let Some(&wormhole_out_model_id)=wormhole_id_to_out_model.get(wormhole_id){
				match &mut model_owned_attributes.attributes{
					attr::CollisionAttributes::Contact(attr::ContactAttributes{contacting:_,general})
					|attr::CollisionAttributes::Intersect(attr::IntersectAttributes{intersecting:_,general})
					=>general.wormhole=Some(attr::Wormhole{destination_model:wormhole_out_model_id}),
					attr::CollisionAttributes::Decoration=>println!("Not a wormhole"),
				}
			}
		}

		//index the attributes
		let attributes_id=if let Some(&attributes_id)=attributes_id_from_attributes.get(&model_owned_attributes.attributes){
			attributes_id
		}else{
			let attributes_id=attr::CollisionAttributesId::new(unique_attributes.len() as u32);
			attributes_id_from_attributes.insert(model_owned_attributes.attributes.clone(),attributes_id);
			unique_attributes.push(model_owned_attributes.attributes);
			attributes_id
		};
		model::Model{
			mesh:model_owned_attributes.mesh,
			transform:model_owned_attributes.transform,
			color:model_owned_attributes.color,
			attributes:attributes_id,
		}
	}).collect();
	PartialMap2{
		meshes:self.primitive_meshes,
		models,
		modes:modes_builder.build(),
		attributes:unique_attributes,
	}
	}
}

pub struct PartialMap2{
	meshes:Vec<model::Mesh>,
	models:Vec<model::Model>,
	modes:gameplay_modes::Modes,
	attributes:Vec<strafesnet_common::gameplay_attributes::CollisionAttributes>,
}
impl PartialMap2{
	pub fn add_render_configs_and_textures(
		self,
		render_configs:impl IntoIterator<Item=(model::RenderConfigId,model::RenderConfig)>,
		textures:impl IntoIterator<Item=(model::TextureId,Vec<u8>)>,
	)->map::CompleteMap{
		let (textures,texture_id_map):(Vec<Vec<u8>>,HashMap<model::TextureId,model::TextureId>)
		=textures.into_iter().enumerate().map(|(new_texture_id,(old_texture_id,texture))|{
			(texture,(old_texture_id,model::TextureId::new(new_texture_id as u32)))
		}).unzip();
		let render_configs=render_configs.into_iter().map(|(render_config_id,mut render_config)|{
			//this may generate duplicate no-texture render configs but idc
			render_config.texture=render_config.texture.and_then(|texture_id|
				texture_id_map.get(&texture_id).copied()
			);
			render_config
		}).collect();
		map::CompleteMap{
			modes:self.modes,
			attributes:self.attributes,
			meshes:self.meshes,
			models:self.models,
			//the roblox legacy texture thing always works
			textures,
			render_configs,
		}
	}
}
