#include "PlayerControllerEditor.hpp"
#include <imgui.h>

bool PlayerControllerEditor::renderComponent(flecs::entity entity,
					     PlayerControllerComponent &pc)
{
	(void)entity;
	bool modified = false;

	if (ImGui::CollapsingHeader("Player Controller",
				    ImGuiTreeNodeFlags_DefaultOpen)) {
		ImGui::Indent();

		const char *modes[] = { "Third Person", "First Person" };
		int mode = pc.cameraMode;
		if (ImGui::Combo("Camera Mode", &mode, modes, 2)) {
			pc.cameraMode = mode;
			modified = true;
		}

		char nameBuf[256];
		snprintf(nameBuf, sizeof(nameBuf), "%s",
			 pc.targetCharacterName.c_str());
		if (ImGui::InputText("Target Character Name", nameBuf,
				     sizeof(nameBuf))) {
			pc.targetCharacterName = nameBuf;
			modified = true;
		}

		char boneBuf[256];
		snprintf(boneBuf, sizeof(boneBuf), "%s",
			 pc.fpsBoneName.c_str());
		if (ImGui::InputText("FPS Bone Name", boneBuf,
				     sizeof(boneBuf))) {
			pc.fpsBoneName = boneBuf;
			modified = true;
		}

		if (ImGui::DragFloat("TPS Distance", &pc.tpsDistance, 0.1f,
				     0.5f, 20.0f))
			modified = true;
		if (ImGui::DragFloat("TPS Height", &pc.tpsHeight, 0.1f,
				     0.0f, 10.0f))
			modified = true;
		if (ImGui::DragFloat("Mouse Sensitivity", &pc.mouseSensitivity,
				     0.01f, 0.01f, 2.0f))
			modified = true;

		char smBuf[256];
		snprintf(smBuf, sizeof(smBuf), "%s",
			 pc.locomotionStateMachine.c_str());
		if (ImGui::InputText("Locomotion SM", smBuf, sizeof(smBuf))) {
			pc.locomotionStateMachine = smBuf;
			modified = true;
		}

		char idleBuf[256];
		snprintf(idleBuf, sizeof(idleBuf), "%s",
			 pc.idleState.c_str());
		if (ImGui::InputText("Idle State", idleBuf, sizeof(idleBuf))) {
			pc.idleState = idleBuf;
			modified = true;
		}

		char walkBuf[256];
		snprintf(walkBuf, sizeof(walkBuf), "%s",
			 pc.walkState.c_str());
		if (ImGui::InputText("Walk State", walkBuf, sizeof(walkBuf))) {
			pc.walkState = walkBuf;
			modified = true;
		}

		char runBuf[256];
		snprintf(runBuf, sizeof(runBuf), "%s",
			 pc.runState.c_str());
		if (ImGui::InputText("Run State", runBuf, sizeof(runBuf))) {
			pc.runState = runBuf;
			modified = true;
		}

		ImGui::Separator();
		ImGui::Text("Swim States (used when in water)");

		char swimIdleBuf[256];
		snprintf(swimIdleBuf, sizeof(swimIdleBuf), "%s",
			 pc.swimIdleState.c_str());
		if (ImGui::InputText("Swim Idle State", swimIdleBuf,
				     sizeof(swimIdleBuf))) {
			pc.swimIdleState = swimIdleBuf;
			modified = true;
		}

		char swimBuf[256];
		snprintf(swimBuf, sizeof(swimBuf), "%s",
			 pc.swimState.c_str());
		if (ImGui::InputText("Swim State", swimBuf, sizeof(swimBuf))) {
			pc.swimState = swimBuf;
			modified = true;
		}

		char swimFastBuf[256];
		snprintf(swimFastBuf, sizeof(swimFastBuf), "%s",
			 pc.swimFastState.c_str());
		if (ImGui::InputText("Swim Fast State", swimFastBuf,
				     sizeof(swimFastBuf))) {
			pc.swimFastState = swimFastBuf;
			modified = true;
		}

		ImGui::Separator();
		ImGui::Text("Actuator Interaction");

		if (ImGui::DragFloat("Actuator Distance", &pc.actuatorDistance,
				     0.5f, 1.0f, 200.0f))
			modified = true;
		if (ImGui::DragFloat("Actuator Cooldown", &pc.actuatorCooldown,
				     0.1f, 0.0f, 10.0f))
			modified = true;
		if (ImGui::DragFloat("Distant Circle Radius",
				     &pc.distantCircleRadius, 0.5f, 1.0f,
				     64.0f))
			modified = true;
		if (ImGui::DragFloat("Near Circle Radius",
				     &pc.nearCircleRadius, 0.5f, 1.0f,
				     64.0f))
			modified = true;
		if (ImGui::DragFloat("Label Font Size",
				     &pc.actuatorLabelFontSize, 0.5f, 8.0f,
				     32.0f))
			modified = true;
		float color[3] = { pc.actuatorColor.x, pc.actuatorColor.y,
				   pc.actuatorColor.z };
		if (ImGui::ColorEdit3("Actuator Color", color)) {
			pc.actuatorColor = Ogre::Vector3(color[0], color[1],
							 color[2]);
			modified = true;
		}

		ImGui::Unindent();
	}

	return modified;
}
