#include "DeerStudio/AngelScriptEngine.h"
#include "DeerStudio/AngelScriptEngine/ErrorHandle.h"
#include "angelscript.h"
#include "imgui.h"

namespace Deer {
	namespace AngelScriptEngine {
		Panel::Panel(asITypeInfo* _info)
		    : Service(_info) {

			renderFunction = type->GetMethodByDecl("void render()");
			menuBarFunction = type->GetMethodByDecl("void menuBar()");
		}

		void Panel::render() {
			// IMPLEMENT REMOVE PADDING
			if (menuBarFunction) {
				ImGui::Begin(type->GetName(), (bool*)0, ImGuiWindowFlags_MenuBar);

				if (ImGui::BeginMenuBar()) {
					AS_CHECK(scriptContext->Prepare(menuBarFunction));
					AS_CHECK(scriptContext->SetObject(object));
					AS_CHECK(scriptContext->Execute());

					AS_CHECK(scriptContext->Unprepare());

					ImGui::EndMenuBar();
				}
			} else {
				ImGui::Begin(type->GetName());
			}

			// This is to make sure that right click activates the window
			if (ImGui::IsWindowHovered(ImGuiHoveredFlags_AllowWhenBlockedByActiveItem) &&
			    (ImGui::IsMouseClicked(ImGuiMouseButton_Right) ||
			     ImGui::IsMouseClicked(ImGuiMouseButton_Middle))) {
				ImGui::SetWindowFocus();
			}

			AS_CHECK(scriptContext->Prepare(renderFunction));
			AS_CHECK(scriptContext->SetObject(object));
			AS_CHECK(scriptContext->Execute());

			AS_CHECK(scriptContext->Unprepare());

			ImGui::End();
		}

		Panel::~Panel() {}
	} // namespace AngelScriptEngine
} // namespace Deer