#include "SquirrelStage.h"

#include "SquirrelClassEx.h"
extern SquirrelClassEx* TObject;
extern Cursor* Mouse_Object;

extern std::map<std::uint64_t, int>UI_HOVER_ZORDER;
extern std::map<std::uint64_t, UiFrameWork*>UI_HOVER_OBJECT;



void SquirrelStage::OnEvent(Event* evt) {
	if (evt->IsType<KeyEvent>())
	{
		if (evt->IsType<KeyDownEvent>())
		{
			KeyCode key = dynamic_cast<KeyDownEvent*>(evt)->code;
			SquirrelClassEx::RunUpdateScript("KeyDownEventCallBack", (int)key);
		}
		else if (evt->IsType<KeyUpEvent>())
		{
			KeyCode key = dynamic_cast<KeyUpEvent*>(evt)->code;
			SquirrelClassEx::RunUpdateScript("KeyUpEventCallBack", (int)key);
		}
		else if (evt->IsType<KeyCharEvent>())
		{
			char ch = dynamic_cast<KeyCharEvent*>(evt)->value;
			SquirrelClassEx::RunUpdateScript("KeyCharEventCallBack", (int)ch);
		}
		else if (evt->IsType<IMEInputEvent>())
		{
			const auto& str = dynamic_cast<IMEInputEvent*>(evt)->value;
			const auto utf8_str = strings::WideToUTF8(strings::NarrowToWide(str));
			SquirrelClassEx::RunUpdateScript("KeyIMEInputEventCallBack", utf8_str.c_str());
		}
	}
}

void SquirrelStage::InitKeyInPut()
{
	AddListener<KeyDownEvent>(Closure(this, &SquirrelStage::OnEvent));
	AddListener<KeyUpEvent>(Closure(this, &SquirrelStage::OnEvent));
	AddListener<KeyCharEvent>(Closure(this, &SquirrelStage::OnEvent));
	AddListener<IMEInputEvent>(Closure(this, &SquirrelStage::OnEvent));
}

void SquirrelStage::PushBaseUi()
{
	//创建鼠标UI图层
	LayerActorPtr MouseUiLayer = new LayerActor();
	MouseUiLayer->SetName("Scene_Mouse_UI_Layer");
	//设置鼠标图层层级最高
	MouseUiLayer->SetZOrder(100000);
	SquirrelClassEx::PushLayerActorPtrToMap(MouseUiLayer);
	this->AddChild(MouseUiLayer);

	//创建默认图层
	LayerActorPtr DefaultLayer = new LayerActor();
	DefaultLayer->SetName(this->GetName() + "_Default_Layer");
	SquirrelClassEx::PushLayerActorPtrToMap(DefaultLayer);
	this->AddChild(DefaultLayer);
}

void SquirrelStage::OnEnter()
{
	//将鼠标对象从父场景移除
	Mouse_Object->RemoveFromParent();

	ActorPtr MouseUiLayer = this->GetChild("Scene_Mouse_UI_Layer");

	//将鼠标对象设置为当前场景子对象
	MouseUiLayer->AddChild(Mouse_Object);

	//场景进入
	if(this->GetName().find("GameMap") == std::string::npos)TObject->RunSceneScript(this->GetName() + "_Enter", this);
	//全局进入
	TObject->RunSceneScript("Game_Enter", this);
}

void SquirrelStage::OnExit()
{
	//清空UI类Map
	UI_HOVER_ZORDER.clear();
	UI_HOVER_OBJECT.clear();

	//将鼠标对象从父场景移除
	Mouse_Object->RemoveFromParent();
	//场景退出
	if (this->GetName().find("GameMap") == std::string::npos)TObject->RunSceneScript(this->GetName() + "_Exit", this);
	//全局退出
	TObject->RunSceneScript("Game_Exit", this);
}


void SquirrelStage::OnUpdate(Duration dt)
{
	static float TimeFlag = 0.0;

	TimeFlag += dt.GetSeconds();

	TObject->RunSceneScript("Game_Proc", this, dt.GetSeconds());

	if (TimeFlag >= 0.00694) {
		//场景Proc
		if (this->GetName().find("GameMap") == std::string::npos)TObject->RunSceneScript(this->GetName() + "_Proc", this, TimeFlag);


		if (Director::GetInstance().GetCurrentStage()->GetHashName() == this->GetHashName() && this->GetName().find("LoadingGame") == std::string::npos) {
			//全局Proc
			TObject->RunSceneScript("Game_Proc_144", this, TimeFlag);
			//判断是否需要重载
			if (FILE* file = fopen("Yosin_Game_Reloading.Sign", "r")) {
				SquirrelClassEx::ReloadingScript();
				//OnExit();
				//OnEnter();
				fclose(file);
				remove("Yosin_Game_Reloading.Sign");
			}
		}

		TimeFlag = 0.0;
	}

}



