#ifndef _APPLICATIONCLASS_H_
#define _APPLICATIONCLASS_H_


///////////////////////
// MY CLASS INCLUDES //
///////////////////////
#include "d_3d_class.h"
#include "camera_class.h"
#include "object.h"
#include "light_class.h"
#include <vector>
#include <filesystem>

#include "bitmap_class.h"
#include "sprite_class.h"
#include "timer_class.h"
#include "font_shader_class.h"
#include "font_class.h"
#include "text_class.h"
#include "fps_class.h"
#include "input_class.h"
#include "shader_manager_class.h"
#include "modellistclass.h"
#include "position_class.h"
#include "frustumclass.h"
#include "render_texture_class.h"
#include "display_plane_class.h"
#include "translate_shader_class.h"
#include "reflection_shader_class.h"
#include "physics.h"
#include "frustum.h"
#include "skybox.h"


#include <fstream>
#include <WICTextureLoader.h>
#include <comdef.h> // Pour _com_error
#include <chrono>
#include <thread>
#include <map>
#include <algorithm>
#include <DirectXMath.h>
#include <mutex>

#include "shadow_map.h"


/////////////
// GLOBALS //
/////////////
constexpr bool full_screen = false;
constexpr float screen_depth = 1000.0f;
constexpr float screen_near = 0.3f;

struct input
{
	bool key_left = false;
	bool key_right = false;
	bool key_up = false;
	bool key_down = false;
};

////////////////////////////////////////////////////////////////////////////////
// Class name: application_class
////////////////////////////////////////////////////////////////////////////////
class application_class
{
public:
	application_class();
	~application_class();
	virtual d_3d_class* get_direct_3d();
	void set_direct_3d(d_3d_class* direct_3d) { direct_3d_ = direct_3d; };
	
	render_texture_class* get_scene_texture() const { return scene_texture_; };
	render_texture_class* get_render_texture() const { return render_texture_; };
	render_texture_class* get_refraction_texture() const { return refraction_texture_; };
	render_texture_class* get_reflection_texture() const { return reflection_texture_; };

	int get_total_vertex_count() const;
	int get_total_triangle_count() const;
	int get_visible_triangle_count() const;

	void create_big_cube(int side_count);
	void process_terrain_generation();
	virtual bool initialize(int, int, HWND, bool is_vulkan);
	void shutdown();
	virtual bool frame(input_class*);
	void physics_thread_function();
	int get_physics_tick_rate() const { return physics_tick_rate_; };
	void set_physics_tick_rate(int physics_tick_rate) { physics_tick_rate_ = physics_tick_rate; };

	int get_screen_width() const;
	void set_screen_width(int screen_width);
	int get_screen_height() const;
	void set_screen_height(int screen_height);

	float get_speed() const { return speed_; };
	void set_speed(const float speed) { this->speed_ = speed; };

	void add_cube();
	void delete_kobject(int index);
	size_t get_cube_count() const { return cubes_.size(); };
	size_t get_terrain_cube_count() const { return terrain_chunk_.size(); };
	std::vector<object*> get_cubes() const { return cubes_; };
	std::vector<object*> get_terrain_cubes() const { return terrain_chunk_; };
	std::vector<object*> get_kobjects() const { return object_; };
	void set_kobjects(std::vector<object*> kobjects) { object_ = kobjects; };
	void add_kobject(std::wstring& filepath);
	void set_path(WCHAR* path) { path_ = path; };
	void set_w_folder(const std::filesystem::path& w_folder) { w_folder_ = w_folder; };
	std::filesystem::path get_w_folder() const { return w_folder_; };

	int get_object_id() const { return object_id_; };
	void set_object_id(int object_id) { object_id_ = object_id; };

	void generate_terrain();
	void delete_terrain();

	XMVECTOR get_light_position(int index);
	XMVECTOR get_light_color(int index);
	void set_light_position(int index, XMVECTOR position);
	void set_light_color(int index, XMVECTOR color);
	std::vector<light_class*> get_lights() const { return lights_; };
	light_class* get_sun_light() const { return sun_light_; };

	bool get_should_quit() const { return should_quit_; };
	void set_should_quit(const bool should_quit) { should_quit_ = should_quit; };

	void set_cel_shading(const bool enable) { enable_cel_shading_ = enable; };

	void set_vsync(bool vsync);
	bool get_vsync() const { return vsync_enabled_; };

	HWND get_hwnd() const;
	void set_hwnd(HWND hwnd);

	bool is_windowed() const;
	void set_windowed(bool windowed);

	void set_window_size(const ImVec2 size) { window_size_ = size; };
	ImVec2 get_window_size() const { return window_size_; };
	float get_aspect_ratio() const { return static_cast<float>(screen_width_) / static_cast<float>(screen_height_); };

	physics* get_physics() const { return physics_; };

	// ------------------------------------- //
	// --------------- Stats --------------- //
	// ------------------------------------- //

	int get_current_fps() const { return fps_ ? fps_->GetFps() : 0; };
	int get_min_fps() const { return fps_ ? fps_->GetMinFps() : 0; };
	int get_max_fps() const { return fps_ ? fps_->GetMaxFps() : 0; };
	float get_frame_time() const { return fps_ ? fps_->GetFrameTime() : 0.0f; };
	int get_draw_calls() const { return drawcalls_; };
	void increment_draw_call_count() { drawcalls_++; };
	void reset_draw_call_count() { drawcalls_ = 0; };

	// ----------------------------------- //
	// ------------- Culling ------------- //
	// ----------------------------------- //

	frustum get_frustum() const { return frustum_culling_; };
	void set_frustum(const frustum& frustum) { frustum_culling_ = frustum; };

	void construct_frustum();
	int get_render_count() const { return render_count_; };
	void set_render_count(const int render_count) { render_count_ = render_count; };
	float get_frustum_tolerance() const { return frustum_culling_tolerance_; };
	void set_frustum_tolerance(const float frustum_tolerance) { frustum_culling_tolerance_ = frustum_tolerance; };

	bool get_can_fixed_update() const { return can_fixed_update_; };
	void set_can_fixed_update(bool can_fixed_update) { can_fixed_update_ = can_fixed_update; };

	ID3D11ShaderResourceView* get_back_buffer_srv() const {return back_buffer_srv_;};
	
private:
	bool render(float, float, float, float, float);
	bool render_physics(bool key_left, bool key_right, bool key_up, bool key_down, float delta_time);
	bool update_mouse_strings(int, int, bool);
	bool update_fps();
	bool update_render_count_string(int);
	bool render_scene_to_texture(float);
	bool render_refraction_to_texture();
	bool render_reflection_to_texture();
	bool render_pass(const std::vector<std::reference_wrapper<std::vector<object*>>>& RenderQueues, XMFLOAT4* diffuse, XMFLOAT4* position, XMFLOAT4* ambient, XMMATRIX view, XMMATRIX projection);

	void update_skybox_position();
	
public :
	std::vector<ID3D11ShaderResourceView*> textures;

private :

	// Thread de culling
	std::thread culling_thread_;
	std::atomic<bool> culling_active_;
	std::mutex objects_mutex_;
	void culling_thread_function();


	std::mutex terrain_mutex_;
	std::vector<std::tuple<float, float, float, std::string, int>> terrain_generation_data_;
	bool terrain_generation_ready_;
	int next_terrain_object_id_;
	
	// ------------------------------------- //
	// ------------- DIRECT3D -------------- //
	// ------------------------------------- //

	d_3d_class* direct_3d_;
	IDXGISwapChain* swap_chain_;
	model_class* model_,* ground_model_, * wall_model_, * bath_model_, * water_model_;
	ModelListClass* model_list_;
	bool vsync_enabled_ = true;

	HWND hwnd_;
	bool windowed_;

	// ------------------------------------- //
	// ------------- RENDERING ------------- //
	// ------------------------------------- //

	XMMATRIX base_view_matrix_;
	render_texture_class* render_texture_, * refraction_texture_, * reflection_texture_;
	render_texture_class* scene_texture_;
	display_plane_class* display_plane_;
	int screen_width_, screen_height_;
	camera_class* camera_;
	camera_class* sun_camera_;
	camera_class* active_camera_;
	position_class* position_;
	int drawcalls_;

	shadow_map* shadow_map_;
	ID3D11ShaderResourceView* shadow_map_srv_;

	// ------------------------------------ //
	// ------------- OBJECTS -------------- //
	// ------------------------------------ //

	object* selected_object_;
	std::vector<object*> cubes_;
	std::vector<object*> terrain_chunk_;
	float speed_ = 0.1f; // speed for the demo spinning object
	std::vector<object*> object_;
	std::vector<object*> imported_object_;
	int object_id_ = 0;
	std::vector<std::reference_wrapper<std::vector<object*>>> render_queues_;
	std::vector<object*> skybox_;

	// ----------------------------------- //
	// ------------- LIGHTS -------------- //
	// ----------------------------------- //

	light_class* m_light_;
	std::vector<light_class*> lights_;
	int num_lights_;
	light_class* sun_light_;
	XMFLOAT3 true_light_position_;
	model_class* light_model_;

	// ----------------------------------- //
	// ------------- SHADERS ------------- //
	// ----------------------------------- //

	shader_manager_class* shader_manager_;
	font_shader_class* font_shader_;
	bitmap_class* bitmap_;
	sprite_class* sprite_;
	bool enable_cel_shading_;

	// ----------------------------------- //
	// ------------ VARIABLES ------------ //
	// ----------------------------------- //

	float water_height_, water_translation_;
	wchar_t* path_;
	std::filesystem::path w_folder_;

	// ------------------------------------------------- //
	// ------------- FPS AND INFO ON SCREEN ------------ //
	// ------------------------------------------------- //

	timer_class* timer_;
	text_class* mouse_strings_;
	text_class* render_count_string_;
	font_class* font_;
	fps_class* fps_;
	text_class* fps_string_;
	int previous_fps_;

	// ------------------------------------------------- //
	// ------------------- OTHER ----------------------- //
	// ------------------------------------------------- //

	bool should_quit_;
	physics* physics_;
	float gravity_;
	XMVECTOR previous_position_;
	ImVec2 window_size_;
	int physics_tick_rate_ = 50;
	bool can_fixed_update_ = false;
	std::thread physics_thread_;

	ID3D11Texture2D* back_buffer_texture_;
	ID3D11ShaderResourceView* back_buffer_srv_;

	// ------------------------------------------------- //
	// ------------------- Culling --------------------- //
	// ------------------------------------------------- //

	frustum frustum_culling_;
	int render_count_;
	float frustum_culling_tolerance_ = 5.f;

	// ------------------------------------------------- //
	// -------------------- Input ---------------------- //
	// ------------------------------------------------- //

	input inputs_;
	bool tab_was_pressed_;
};

#endif