#include "main_code.h"

#ifdef TARGET_ANDROID
#include "android_api.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#include "include/Engine.h"

#include "main_code.h"


void TMyApplication::InnerInit()
{
	
	*Console<<"Inner init go!\n";

#ifdef TARGET_ANDROID
	ResourceManager->PathToResources = "";
#endif
#ifdef TARGET_WIN32
#ifdef NDEBUG
	ResourceManager->PathToResources = "resources/";
#else
	ResourceManager->PathToResources = "../../../assets/";
#endif
#endif
#ifdef TARGET_IOS
	ResourceManager->PathToResources = "assets/";
#endif

	ResourceManager->TexList.AddTexture(CONST_CONSOLE_TEX_NAME);

	ResourceManager->ShaderManager.AddShader("DefaultShader", "gui_transparent.vertex", "gui_transparent.fragment");
    
    Renderer->PushShader("DefaultShader");

	ResourceManager->FontManager.AddFont("droid_sans14", "droid_sans14_font_bitmap.bmp32", "droid_sans14_font_charmap.txt");
	ResourceManager->FontManager.PushFont("droid_sans14");

	ResourceManager->SoundManager.LoadMusic("level1ogg.ogg");
	
    ResourceManager->SoundManager.LoadSound("shot.wav");
	
	//Renderer->MovePhi(pi/6);

	Renderer->CameraMover = TPitCamera();

	//boost::get<TPanoramicCamera>(Renderer->CameraMover).MoveDist(3.5f);
	
	ResourceManager->LightManager.SetLightDirection(vec3(-1,0,-1));
	ResourceManager->LightManager.SetLightColor(vec4(1,0,0,1));

	ResourceManager->TexList.AddTexture("bt_box_yellow.bmp");
	ResourceManager->TexList.AddTexture("tube.png");
	ResourceManager->FlexModelManager.LoadModelFromXml("model.xml");
	ResourceManager->FlexModelManager.LoadModelFromXml("pit.xml");
	ResourceManager->FlexModelManager.LoadModelFromXml("tube1.xml");

	FlexModel = ResourceManager->FlexModelManager.InstanciateModel("pit.xml");

	FlexModel.MoveModel(vec3(0, -40.f, 0));
	FlexModel.ScaleModel(0.03f);
	FlexModel.MoveModel(vec3(0, 0.f, -3.5f));
	FlexModel.RefreshBuffer();

	//TubeModelArr.resize(20);
	TubeModelArr.resize(2);

	for (size_t i = 0; i < TubeModelArr.size(); ++i)
	{
		TubeModelArr[i] = ResourceManager->FlexModelManager.InstanciateModel("tube1.xml");
		TubeModelArr[i].MoveModel(vec3(0, 0, -250.f*i));
		TubeModelArr[i].ScaleModel(10.f);
		TubeModelArr[i].RefreshBuffer();
		//TubeModelArr[i].PushModelDataTriangleList();
	}

	//Uncomment to start music playing
	//ResourceManager->SoundManager.PlayMusic("level1ogg.ogg");
	
	Velocity = 10.f;

	*Console<<"Inner init end!\n";
}

void TMyApplication::InnerDeinit()
{
}


void TMyApplication::InnerDraw()
{
    
	glClearColor(0,0,0,1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	Renderer->PushPerspectiveProjectionMatrix(pi/6, Renderer->GetMatrixWidth() / Renderer->GetMatrixHeight(), 1.f, 4500.f);

	Renderer->PushMatrix();

    Renderer->SetGLCamView();
	

	for (size_t i = 0; i < TubeModelArr.size(); ++i)
	{
		TubeModelArr[i].Draw();
	}

	Renderer->PopMatrix();

	Renderer->PopProjectionMatrix();

	FlexModel.Draw();

	
}


void TMyApplication::InnerUpdate(cardinal dt)
{
	vec3 camShift = boost::get<TPitCamera>(Renderer->CameraMover).CamShift;

	vec3 camVec = boost::get<TPitCamera>(Renderer->CameraMover).CamVec;

	camShift += camVec * (static_cast<float>(dt)*Velocity);

	boost::get<TPitCamera>(Renderer->CameraMover).CamShift = camShift;

	if (Velocity != 0)
	{

		vec3 a = boost::get<TPitCamera>(Renderer->CameraMover).CamShift;
		vec3 b = a + 100.f * boost::get<TPitCamera>(Renderer->CameraMover).CamVec;

		if (a != b)
		{

			for (size_t i = 0; i < TubeModelArr.size(); ++i)
			{
				TDataTriangleList& dataTriangleList = TubeModelArr[i].GetDataTriangleList();

				for (size_t j = 0; j < dataTriangleList.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB].size(); j+=3)
				{
					TSmpTriangle smpTriangle; 

					smpTriangle.p[0] = dataTriangleList.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB][j];
					smpTriangle.p[1] = dataTriangleList.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB][j + 1];
					smpTriangle.p[2] = dataTriangleList.Vec3CoordArr[CONST_STRING_POSITION_ATTRIB][j + 2];

					smpTriangle.v = smpTriangle.p[1] - smpTriangle.p[0];
					smpTriangle.w = smpTriangle.p[2] - smpTriangle.p[1];

					smpTriangle.n = CrossProduct(smpTriangle.v, smpTriangle.w);

					if (LineCouldCrossTriangle(a, b, smpTriangle))
					{
						if (LineCrossTriangle(a, b, smpTriangle) == 1)
						{
							Velocity = 0;
						}
					}
				}
			}
		}

	}
}


void TMyApplication::InnerOnMove(vec2 shift)
{
	
	float len = Length(shift);

	//vec3 rotation = Normalize(vec3(shift, 0)); //Non-inverse
	
	vec3 rotation = -Normalize(vec3(shift, 0)); //Inverse

	rotation.v[0] = -rotation.v[0];

	vec3 z = vec3(0,0,-1);

	vec4 startQuat = boost::get<TPitCamera>(Renderer->CameraMover).CameraQuat;

	vec3 rotate_axis = Normalize(CrossProduct(rotation, z));

	rotate_axis = startQuat * vec4(rotate_axis) * InverseQuat(startQuat);
	
	float half_alpha = len * 0.01f / 2.f;

	vec4 quat = vec4(rotate_axis.v[0] * sin(half_alpha), rotate_axis.v[1] * sin(half_alpha), rotate_axis.v[2] * sin(half_alpha), cos(half_alpha));

	boost::get<TPitCamera>(Renderer->CameraMover).RotateByQuat(quat);

	boost::get<TPitCamera>(Renderer->CameraMover).CalcCamVec();
}

void TMyApplication::OnMouseWheel(short int delta)
{
	Velocity = 10.f;
	
	boost::get<TPitCamera>(Renderer->CameraMover).CamShift = ZeroVec3;

}