#ifndef SUN_EDITOR_HPP
#define SUN_EDITOR_HPP
#pragma once

#include "ComponentEditor.hpp"
#include "../components/Sun.hpp"

/**
 * Editor for Sun component
 */
class SunEditor : public ComponentEditor<SunComponent> {
public:
	bool renderComponent(flecs::entity entity,
			     SunComponent &component) override
	{
		bool changed = false;

		ImGui::Text("Sun / Time of Day");
		ImGui::Separator();

		if (ImGui::Checkbox("Enabled", &component.enabled)) {
			changed = true;
			component.markDirty();
		}

		if (ImGui::SliderFloat("Time of Day", &component.timeOfDay, 0.0f,
				       24.0f, "%.2f h")) {
			changed = true;
		}

		if (ImGui::SliderFloat("Time Speed",
				       &component.timeSpeed, 0.0f, 2.0f,
				       "%.3f h/s")) {
			changed = true;
		}

		if (ImGui::SliderFloat("Intensity", &component.intensity, 0.0f,
				       3.0f, "%.2f")) {
			changed = true;
			component.markDirty();
		}

		if (ImGui::SliderFloat("Orbit Tilt", &component.orbitTilt, -45.0f,
				       45.0f, "%.1f deg")) {
			changed = true;
		}

		if (ImGui::Checkbox("Cast Shadows", &component.castShadows)) {
			changed = true;
			component.markDirty();
		}

		ImGui::Separator();
		ImGui::Text("Colors");

		float sunColor[3] = { component.sunColor.r, component.sunColor.g,
				      component.sunColor.b };
		if (ImGui::ColorEdit3("Sun Color", sunColor)) {
			component.sunColor = Ogre::ColourValue(sunColor[0], sunColor[1],
						       sunColor[2]);
			changed = true;
		}

		float moonColor[3] = { component.moonColor.r, component.moonColor.g,
				       component.moonColor.b };
		if (ImGui::ColorEdit3("Moon Color", moonColor)) {
			component.moonColor = Ogre::ColourValue(moonColor[0], moonColor[1],
							moonColor[2]);
			changed = true;
		}

		float ambDay[3] = { component.ambientDay.r, component.ambientDay.g,
				    component.ambientDay.b };
		if (ImGui::ColorEdit3("Ambient Day", ambDay)) {
			component.ambientDay = Ogre::ColourValue(ambDay[0], ambDay[1],
							 ambDay[2]);
			changed = true;
		}

		float ambNight[3] = { component.ambientNight.r,
				      component.ambientNight.g,
				      component.ambientNight.b };
		if (ImGui::ColorEdit3("Ambient Night", ambNight)) {
			component.ambientNight = Ogre::ColourValue(ambNight[0], ambNight[1],
							   ambNight[2]);
			changed = true;
		}

		float ambSunrise[3] = { component.ambientSunrise.r,
					component.ambientSunrise.g,
					component.ambientSunrise.b };
		if (ImGui::ColorEdit3("Ambient Sunrise", ambSunrise)) {
			component.ambientSunrise = Ogre::ColourValue(
				ambSunrise[0], ambSunrise[1], ambSunrise[2]);
			changed = true;
		}

		float ambSunset[3] = { component.ambientSunset.r,
				       component.ambientSunset.g,
				       component.ambientSunset.b };
		if (ImGui::ColorEdit3("Ambient Sunset", ambSunset)) {
			component.ambientSunset = Ogre::ColourValue(
				ambSunset[0], ambSunset[1], ambSunset[2]);
			changed = true;
		}

		ImGui::Separator();
		ImGui::Text("Visualization");

		if (ImGui::Checkbox("Show Sun Sphere", &component.showSunSphere)) {
			changed = true;
			component.markDirty();
		}
		if (ImGui::Checkbox("Show Moon Sphere", &component.showMoonSphere)) {
			changed = true;
			component.markDirty();
		}
		if (ImGui::SliderFloat("Sun Sphere Size", &component.sunSphereSize,
				       0.5f, 20.0f, "%.1f")) {
			changed = true;
			component.markDirty();
		}
		if (ImGui::SliderFloat("Moon Sphere Size", &component.moonSphereSize,
				       0.5f, 20.0f, "%.1f")) {
			changed = true;
			component.markDirty();
		}

		ImGui::Separator();
		ImGui::PushID("Sun");
		if (ImGui::Button("Reset to Defaults")) {
			component = SunComponent();
			changed = true;
			component.markDirty();
		}
		ImGui::PopID();

		return changed;
	}

	const char *getName() const override
	{
		return "Sun";
	}
};

#endif // SUN_EDITOR_HPP
