use super::integer::{Planar64Vec3,Planar64Affine3};

pub type TextureCoordinate=[f32;2];
pub type Color4=[f32;4];

#[binrw::binrw]
#[brw(little)]
pub struct IndexedVertex{
	pub pos:u32,
	pub tex:u32,
	pub normal:u32,
	pub color:u32,
}

#[binrw::binrw]
#[brw(little)]
pub struct Polygon{
	pub count:u32,
	#[br(count=count)]
	pub vertices:Vec<u32>,
}
#[binrw::binrw]
#[brw(little)]
pub struct PolygonGroup{
	pub count:u32,
	#[br(count=count)]
	pub polys:Vec<Polygon>,
}
#[binrw::binrw]
#[brw(little)]
pub struct RenderConfig{
	pub texture:Option<u32>,
}
#[binrw::binrw]
#[brw(little)]
pub struct IndexedGraphicsGroup{
	pub count:u32,
	pub render:u32,
	#[br(count=count)]
	pub groups:Vec<u32>,
}
#[binrw::binrw]
#[brw(little)]
pub struct IndexedPhysicsGroup{
	pub count:u32,
	#[br(count=count)]
	pub groups:Vec<u32>,
}

#[binrw::binrw]
#[brw(little)]
pub struct MeshHeader{
	pub unique_pos:u32,
	pub unique_normal:u32,
	pub unique_tex:u32,
	pub unique_color:u32,
	pub unique_vertices:u32,
	pub polygon_groups:u32,
	pub graphics_groups:u32,
	pub physics_groups:u32,
}
#[binrw::binrw]
#[brw(little)]
pub struct Mesh{
	pub header:MeshHeader,
	#[br(count=header.unique_pos)]
	pub unique_pos:Vec<Planar64Vec3>,
	#[br(count=header.unique_normal)]
	pub unique_normal:Vec<Planar64Vec3>,
	#[br(count=header.unique_tex)]
	pub unique_tex:Vec<TextureCoordinate>,
	#[br(count=header.unique_color)]
	pub unique_color:Vec<Color4>,
	#[br(count=header.unique_vertices)]
	pub unique_vertices:Vec<IndexedVertex>,
	#[br(count=header.polygon_groups)]
	pub polygon_groups:Vec<PolygonGroup>,
	#[br(count=header.graphics_groups)]
	pub graphics_groups:Vec<IndexedGraphicsGroup>,
	#[br(count=header.physics_groups)]
	pub physics_groups:Vec<IndexedPhysicsGroup>,
}
impl Into<strafesnet_common::model::Mesh> for Mesh{
	fn into(self)->strafesnet_common::model::Mesh{
		strafesnet_common::model::Mesh{
			unique_pos:self.unique_pos.into_iter().map(strafesnet_common::integer::Planar64Vec3::raw_array).collect(),
			unique_normal:self.unique_normal.into_iter().map(strafesnet_common::integer::Planar64Vec3::raw_array).collect(),
			unique_tex:self.unique_tex.into_iter().map(strafesnet_common::model::TextureCoordinate::from_array).collect(),
			unique_color:self.unique_color.into_iter().map(strafesnet_common::model::Color4::from_array).collect(),
			unique_vertices:self.unique_vertices.into_iter().map(|vert|strafesnet_common::model::IndexedVertex{
				pos:strafesnet_common::model::PositionId::new(vert.pos),
				tex:strafesnet_common::model::TextureCoordinateId::new(vert.tex),
				normal:strafesnet_common::model::NormalId::new(vert.normal),
				color:strafesnet_common::model::ColorId::new(vert.color),
			}).collect(),
			polygon_groups:self.polygon_groups.into_iter().map(|group|
				strafesnet_common::model::PolygonGroup::PolygonList(
					strafesnet_common::model::PolygonList::new(
						group.polys.into_iter().map(|vert|
							vert.vertices.into_iter().map(strafesnet_common::model::VertexId::new).collect()
						).collect()
					)
				)
			).collect(),
			graphics_groups:self.graphics_groups.into_iter().map(|group|
				strafesnet_common::model::IndexedGraphicsGroup{
					render:strafesnet_common::model::RenderConfigId::new(group.render),
					groups:group.groups.into_iter().map(strafesnet_common::model::PolygonGroupId::new).collect(),
				}
			).collect(),
			physics_groups:self.physics_groups.into_iter().map(|group|
				strafesnet_common::model::IndexedPhysicsGroup{
					groups:group.groups.into_iter().map(strafesnet_common::model::PolygonGroupId::new).collect(),
				}
			).collect(),
		}
	}
}

#[binrw::binrw]
#[brw(little)]
pub struct Model{
	pub mesh:u32,
	pub attributes:u32,
	pub color:Color4,
	pub transform:Planar64Affine3,
}
impl Into<strafesnet_common::model::Model> for Model{
	fn into(self)->strafesnet_common::model::Model{
		let [_0,_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b]=self.transform;
		strafesnet_common::model::Model{
			mesh:strafesnet_common::model::MeshId::new(self.mesh),
			attributes:strafesnet_common::gameplay_attributes::CollisionAttributesId::new(self.attributes),
			color:strafesnet_common::model::Color4::from_array(self.color),
			transform:strafesnet_common::integer::Planar64Affine3::new(
				strafesnet_common::integer::Planar64Mat3::from_cols(
					strafesnet_common::integer::Planar64Vec3::raw_xyz(_0,_1,_2),
					strafesnet_common::integer::Planar64Vec3::raw_xyz(_3,_4,_5),
					strafesnet_common::integer::Planar64Vec3::raw_xyz(_6,_7,_8)
				),
				strafesnet_common::integer::Planar64Vec3::raw_xyz(_9,_a,_b)
			),
		}
	}
}