use std::time::Instant;

use cgmath::{Euler, Quaternion};
use legion::world::SubWorld;
use legion::IntoQuery;
use legion::*;
use nalgebra::Quaternion as naQuaternion;
use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodySet};
use rapier3d::geometry::{BroadPhase, ColliderSet, NarrowPhase};
use rapier3d::pipeline::PhysicsPipeline;

use crate::camera::{Camera, CameraController};
use crate::components::{Collider, LoopState, Mesh, Physics, Position};
use imgui::FontSource;

pub struct PhysicsState {
    pub(in crate::physics) gravity: rapier3d::math::Vector<f32>,
    pub(in crate::physics) integration_parameters: IntegrationParameters,
    pub(in crate::physics) broad_phase: BroadPhase,
    pub(in crate::physics) narrow_phase: NarrowPhase,
    pub(in crate::physics) bodies: RigidBodySet,
    pub(in crate::physics) colliders: ColliderSet,
    pub(in crate::physics) joints: JointSet,
}

impl PhysicsState {
    pub fn build(gravity: rapier3d::math::Vector<f32>) -> (PhysicsPipeline, PhysicsState) {
        (
            PhysicsPipeline::new(),
            PhysicsState {
                gravity,
                integration_parameters: IntegrationParameters::default(),
                broad_phase: BroadPhase::new(),
                narrow_phase: NarrowPhase::new(),
                bodies: RigidBodySet::new(),
                colliders: ColliderSet::new(),
                joints: JointSet::new(),
            },
        )
    }
}
