use cgmath::{Vector4, vec4};

use crate::{input::InputState, text::{create_text_object, update_text}, vulkan::{MeshHandle, PERF_COUNTER_SIZE, TextVertex, Vertex, VulkanRenderer, gameobject::{GameObject, GameObjectHandle, Updatable}, mesh::{CPUMesh, CPUVertexList}}};

use super::{GameState, TestGame};

pub struct FpsCounter {
    pub game_object: GameObjectHandle
}

impl FpsCounter {
    pub fn new(game: &mut TestGame, renderer: &mut VulkanRenderer) -> FpsCounter {
        let text_mesh = create_text_object(&mut game.game_state.brush, renderer, "", 30.);
        FpsCounter { game_object: game.add_game_object(renderer, text_mesh) }
    }
}

impl Updatable for FpsCounter {
    fn update(&mut self, _delta_time: f32, input: &InputState, game_state: &mut GameState, game_objects: &mut Vec<GameObject>, renderer: &mut VulkanRenderer) {
        if input.button_just_pressed("toggle_framerate") {
            let go = &mut game_objects[self.game_object];
            go.visible = !go.visible;
        }

        let update_duration = renderer.game_data.update_perf_counters.iter().sum::<u128>() / PERF_COUNTER_SIZE as u128;
        let render_duration = renderer.game_data.render_perf_counters.iter().sum::<u128>() / PERF_COUNTER_SIZE as u128;
        let other_duration = renderer.game_data.other_perf_counters.iter().sum::<u128>() / PERF_COUNTER_SIZE as u128;
        let other_variance = renderer.game_data.other_perf_counters.iter().fold(0, |acc, b| acc + (*b as i128 - other_duration as i128) * (*b as i128 - other_duration as i128)) / PERF_COUNTER_SIZE as i128;
        let text = format!("Update: {:.0}ms\nRender: {:.0}ms\nTotal: {:.0}ms (±{:.1})\nDelta: {:.0}ms", update_duration as f64 / 1000., render_duration as f64 / 1000., other_duration as f64 / 1000., f64::sqrt(other_variance as f64) / 1000., _delta_time * 1000.);
        update_text(self.game_object, &text, 30., renderer, &mut game_state.brush, game_objects);
    }
}

pub struct WorldQuad {
    pub game_object: GameObjectHandle,
}

impl WorldQuad {
    pub fn new(game: &mut TestGame, renderer: &mut VulkanRenderer) -> WorldQuad {
        let quad_verts = vec![
            Vertex { position: [0., 0., 0.], uv: [0., 0.], normal: [0., 1., 0.], tangent: [1., 0., 0., 1.], bone_index: [0, 0, 0, 0], bone_weight: [0., 0., 0., 0.] },
            Vertex { position: [1., 0., 0.], uv: [1., 0.], normal: [0., 1., 0.], tangent: [1., 0., 0., 1.], bone_index: [0, 0, 0, 0], bone_weight: [0., 0., 0., 0.] },
            Vertex { position: [0., 0., 1.], uv: [0., 1.], normal: [0., 1., 0.], tangent: [1., 0., 0., 1.], bone_index: [0, 0, 0, 0], bone_weight: [0., 0., 0., 0.] },
            Vertex { position: [1., 0., 1.], uv: [1., 1.], normal: [0., 1., 0.], tangent: [1., 0., 0., 1.], bone_index: [0, 0, 0, 0], bone_weight: [0., 0., 0., 0.] },
        ];
        let cpu_mesh = CPUMesh { vertices: CPUVertexList::Vertex3D(quad_verts), indices: vec![0, 2, 1, 1, 2, 3], local_texture_index: None, local_normal_map_index: None, name: None };
        let mesh_index = renderer.upload_mesh(cpu_mesh, None);
        let mesh_handle = MeshHandle {
            index: mesh_index,
            textures: vec![1, 0],
            original_path: None,
            pipeline_index: 0
        };
        let game_object = game.add_game_object(renderer, mesh_handle);
        WorldQuad { game_object }
    }
}

pub struct UiQuad {
    pub game_object: GameObjectHandle,
    pub background_color: Vector4<f32>,
}

impl UiQuad {
    pub fn new(game: &mut TestGame, renderer: &mut VulkanRenderer) -> UiQuad {
        let quad_verts = vec![
            TextVertex { position: [0., 0., 0.], uv: [0., 0.] },
            TextVertex { position: [0., 1., 0.], uv: [0., 1.] },
            TextVertex { position: [1., 0., 0.], uv: [1., 0.] },
            TextVertex { position: [1., 1., 0.], uv: [1., 1.] },
        ];
        let cpu_mesh = CPUMesh { vertices: CPUVertexList::VertexText(quad_verts), indices: vec![0, 2, 1, 1, 2, 3], local_texture_index: None, local_normal_map_index: None, name: None };
        let mesh_index = renderer.upload_mesh(cpu_mesh, None);
        let mesh_handle = MeshHandle {
            index: mesh_index,
            textures: vec![],
            original_path: None,
            pipeline_index: 1
        };
        let go = game.add_game_object(renderer, mesh_handle);
        UiQuad { game_object: go, background_color: vec4(0., 0., 255., 0.) }
    }
}

impl Updatable for UiQuad {
    fn update(&mut self, _delta_time: f32, _input: &InputState, _game_state: &mut GameState, _game_objects: &mut Vec<GameObject>, _renderer: &mut VulkanRenderer) {
        
    }
}