#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"

#ifndef TARGET_IOS
std::shared_ptr<TMyApplication> App = std::make_shared<TMyApplication>();
#endif

bool animPaused = false;


struct TOnClickTest
{
	void operator()()
	{

		//ResourceManager->ScriptManager.RunScript("5+5;");

		if (animPaused)
		{
			//animPaused = false;
			//ResourceManager->HalibutAnimationManager.StartAnimation("test_anim");
			//ResourceManager->HalibutAnimationManager.ShowAnimation("test_anim");
			//ResourceManager->SoundManager.PlayMusic("finalogg.ogg");

			//App->Match3Controller.Match3Field->StartAnimateChip(4,1);

			//App->Match3Controller.Match3Field->HighlightMatch3();
		}
		else
		{
			//animPaused = true;
			//ResourceManager->HalibutAnimationManager.StopAnimation("test_anim");
			//ResourceManager->HalibutAnimationManager.HideAnimation("test_anim");
			//ResourceManager->SoundManager.StopMusic("finalogg.ogg");
			//App->Match3Controller.Match3Field->StopAnimateChip(4,1);
			//App->Match3Controller.Match3Field->HighlightMatch3();
		}

	}
};



//What to do on init
void TMyApplication::InnerInit()
{
	#ifdef TARGET_ANDROID
	SE::ST::PathToResources = "";
	#endif
	#ifdef TARGET_WIN32
		#ifdef DEBUG
	SE::ST::PathToResources = "../../assets/";
		#else
	SE::ST::PathToResources = "res/";
		#endif
	#endif
	#ifdef TARGET_IOS
	SE::ST::PathToResources = "assets/";
	#endif

	if (SE::Console != nullptr) {
		*SE::Console << "APP INIT\n";
	}
	
	SE::ResourceManager->ShaderManager.AddShader("DefaultShader", "shader1vertex.txt", "shader1fragment.txt");
	SE::ResourceManager->ShaderManager.AddShader("FrameShader", "frameshader_vertex.txt", "frameshader_fragment.txt");
	SE::ResourceManager->ShaderManager.AddShader("BrickShader", "brickshader_vertex.txt", "brickshader_fragment.txt");
	SE::Renderer->PushShader("DefaultShader");

	const std::string CONST_LOADING_BACKGROUND_BLACK = "loading_background_black";
	const std::string CONST_LOADING_TEXTURE = "loading";
	const std::string CONST_LOGO_SMALL_TEXTURE = "logo_small";

	SE::ResourceManager->TexList.AddTexture(CONST_LOADING_BACKGROUND_BLACK + ".png", CONST_LOADING_BACKGROUND_BLACK);
	SE::ResourceManager->TexList.AddTexture(CONST_LOADING_TEXTURE + ".png", CONST_LOADING_TEXTURE);
	SE::ResourceManager->TexList.AddTexture(CONST_LOGO_SMALL_TEXTURE + ".png", CONST_LOGO_SMALL_TEXTURE);

	SE::ResourceManager->TexList.AddTexture("console_bkg.bmp");

	SE::ResourceManager->FrameManager.AddFrameRenderBuffer("LevelBuffer", 512, 512);

	Inited = true;

	SE::Renderer->SetOrthoProjection();

	SE::Renderer->SetFullScreenViewport();


	auto px = SE::FileToPropertyTree("shaders.xml");
	SE::ResourceManager->ShaderManager.Serialize(*px);

	px = SE::FileToPropertyTree("textures.xml");
	SE::ResourceManager->TexList.Serialize(*px);

	//this cause exception
	//px = SE::FileToPropertyTree("fonts.xml");
	//SE::ResourceManager->FontManager.Serialize(*px);
	//SE::ResourceManager->FontManager.PushFont("droid_sans14");





	Match3Controller.Match3Field = std::make_shared<TMatch3Field>(Match3Controller);

}

//What to do on deinit
void TMyApplication::InnerDeinit()
{
	Inited = false;
}

//What to do on draw
void TMyApplication::InnerDraw()
{
	auto &list = Match3Controller.Match3Field.get()->TriangleListVector;

	for (auto j = list.begin(); j != list.end(); ++j)
	{
		SE::TRenderParamsSetter paramSetter(j->first);
		SE::Renderer->DrawTriangleList(j->second);
		SE::CheckGlError("TMyApplication::InnerDraw()");
	}
}

//What to do on update. timer means how many ms passed since last update
void TMyApplication::InnerUpdate(size_t timer)
{
	Match3Controller.Match3Field.get()->Update(timer);
}

void TMyApplication::InnerOnTapDown(Eigen::Vector2f p) {
	if (!Match3Controller.Match3Field.get()->CheckClick(p)) {
		return;
	}
	Match3Controller.Match3Field.get()->OnTapDown(p);
}

void TMyApplication::InnerOnTapUp(Eigen::Vector2f p) {
	if (!Match3Controller.Match3Field.get()->CheckClick(p)) {
		return;
	}
	Match3Controller.Match3Field.get()->OnTapUp(p);
}

void TMyApplication::InnerOnMove(Eigen::Vector2f pos, Eigen::Vector2f shift) {
//	if (!Match3Controller.Match3Field.get()->CheckClick(pos)) {
//		return;
//	}
//	Match3Controller.Match3Field.get()->OnMove(pos);
}