#include "dragons.hpp"
#include "environment.hpp"
#include "key_positions_structure.hpp"
#include "interpolation.hpp"
#include "terrain.hpp"

void dragons::initialize_dragons()
{
	// Initialize the temporary mesh_drawable that will be inserted in the hierarchy
	mesh_drawable body;
	mesh_drawable mouth;
	mesh_drawable wing1;
	mesh_drawable wing2;
	mesh_drawable tailmiddle;
	mesh_drawable tailend;

	// Create the geometry of the meshes
	//   Note: this geometry must be set in their local coordinates with respect to their position in the hierarchy (and with respect to their animation)
	body.initialize_data_on_gpu(mesh_load_file_obj(project::path + "assets/dragon/dragonbody.obj"));
	wing1.initialize_data_on_gpu(mesh_load_file_obj(project::path + "assets/dragon/dragonwl.obj"));
	wing2.initialize_data_on_gpu(mesh_load_file_obj(project::path + "assets/dragon/dragonwr.obj"));
	mouth.initialize_data_on_gpu(mesh_load_file_obj(project::path + "assets/dragon/dragonmouth.obj"));
	tailmiddle.initialize_data_on_gpu(mesh_load_file_obj(project::path + "assets/dragon/dragontailend.obj"));
	tailend.initialize_data_on_gpu(mesh_load_file_obj(project::path + "assets/dragon/dragontailmiddle.obj"));

	// Scale the model
	//body.model.scaling = 0.2f;
	//wing1.model.scaling = 0.2f;
	//wing2.model.scaling = 0.2f;

	// Set the color of some elements
	body.material.color = { 0.6,0.15,0.1 };
	body.material.phong.specular = 0.0f;
	body.texture.load_and_initialize_texture_2d_on_gpu(project::path + "assets/dragon/dragon_scales.jpg",
		GL_REPEAT,
		GL_REPEAT);
	wing1.material.color = { 0.6,0.15,0.1 };
	wing1.material.phong.specular = 0.0f;
	wing1.texture.load_and_initialize_texture_2d_on_gpu(project::path + "assets/dragon/dragon_scales.jpg",
		GL_REPEAT,
		GL_REPEAT);
	wing2.material.color = { 0.6,0.15,0.1 };
	wing2.material.phong.specular = 0.0f;
	wing2.texture.load_and_initialize_texture_2d_on_gpu(project::path + "assets/dragon/dragon_scales.jpg",
		GL_REPEAT,
		GL_REPEAT);
	mouth.material.color = { 0.6,0.15,0.1 };
	mouth.material.phong.specular = 0.0f;
	mouth.texture.load_and_initialize_texture_2d_on_gpu(project::path + "assets/dragon/dragon_scales.jpg",
		GL_REPEAT,
		GL_REPEAT);
	tailmiddle.material.color = { 0.6,0.15,0.1 };
	tailmiddle.material.phong.specular = 0.0f;
	tailmiddle.texture.load_and_initialize_texture_2d_on_gpu(project::path + "assets/dragon/dragon_scales.jpg",
		GL_REPEAT,
		GL_REPEAT);
	tailend.material.color = { 0.6,0.15,0.1 };
	tailend.material.phong.specular = 0.0f;
	tailend.texture.load_and_initialize_texture_2d_on_gpu(project::path + "assets/dragon/dragon_scales.jpg",
		GL_REPEAT,
		GL_REPEAT);

	// Add the elements in the hierarchy
	//   The syntax is hierarchy.add(mesh_drawable, "name of the parent element", [optional: local translation in the hierarchy])
	//   Notes: 
	//     - An element must necessarily be added after its parent
	//     - The first element (without explicit name of its parent) is assumed to be the root.

	dragon_mesh.add(body, "Dragon base");
	dragon_mesh.add(wing1, "Dragon wing left", "Dragon base");
	dragon_mesh.add(wing2, "Dragon wing right", "Dragon base");
	dragon_mesh.add(mouth, "Dragon mouth", "Dragon base");
	dragon_mesh.add(tailmiddle, "Dragon tailmiddle", "Dragon base");
	dragon_mesh.add(tailend, "Dragon tailend", "Dragon tailmiddle");

	N = 0;
}

void dragons::update_mvt()
{
	for (int i = 0; i < N; i++) {
		if (!dragons_prop[i].isdead) dragons_prop[i].update_mvt();
	}
}


void dragons::add_dragon(vec3 new_pos, float _size, numarray<vec3> key_positions, numarray<float> key_times) {
	N++;
	dragons_prop.resize(N);

	dragons_prop[N - 1].pos = new_pos;
	dragons_prop[N - 1].isdead = false;
	dragons_prop[N - 1].size = _size;
	dragons_prop[N - 1].initialize_mvt(new_pos, key_positions, key_times);

}