#include <iostream>
#include <Ogre.h>
#include "Components.h"
#include "SunModule.h"
#include <tracy/Tracy.hpp>

namespace ECS
{
SunModule::SunModule(flecs::world &ecs)
{
    ZoneScoped;
    ecs.component<Sun>().add(flecs::Singleton);
	ecs.system<const EngineData, Sun>("UpdateSetupSun")
		.kind(flecs::OnUpdate)
		.each([](const EngineData &eng, Sun &sun) {
            ZoneScopedN("UpdateSetupSun");
            if (!sun.mSun) {
				Ogre::Light *light =
					eng.mScnMgr->createLight("Sun");
				sun.mSunNode = eng.mScnMgr->getRootSceneNode()
						       ->createChildSceneNode(
							       "SunPivot");
				sun.mSunGoal = eng.mScnMgr->getRootSceneNode()
						       ->createChildSceneNode(
							       "SunGoal");
				sun.mSunTarget =
					sun.mSunGoal->createChildSceneNode(
						"SunGoalTarget",
						Ogre::Vector3(100.0f, -400.0f,
							      -400.0f),
						Ogre::Quaternion::IDENTITY);
				sun.mSunNode->attachObject(light);
				light->setType(Ogre::Light::LT_DIRECTIONAL);
				light->setDiffuseColour(
					Ogre::ColourValue::White);
				light->setSpecularColour(
					Ogre::ColourValue(0.4, 0.4, 0.4));
				sun.mSunNode->setDirection(
					Ogre::Vector3(100.0f, -400.0f, -400.f));
				sun.mSun = light;
			}
			static const float sun_speed = 1.0f;
			float uangle = M_PI * 2.0f / 24.0f / 60.0f;
			sun.mSunNode->pitch(Ogre::Radian(uangle) * sun_speed *
					    eng.delta);
			if (sun.mSunNode->getOrientation()
				    .getPitch()
				    .valueRadians() > 0)
				eng.mScnMgr->setAmbientLight(Ogre::ColourValue(
					0.1f, 0.1f, 0.4f, 1.0f));
			else
				eng.mScnMgr->setAmbientLight(Ogre::ColourValue(
					0.2f, 0.2f, 0.2f, 1.0f));
		});
}
}
