/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#pragma once

#include <cstdint>
#include <algorithm>
#include <array>
#include "Base/Event.hpp"
#undef max

namespace OpenVulkano
{
	class EngineConfiguration
	{
		public:
		[[nodiscard]] static EngineConfiguration* GetEngineConfiguration();

		void SetNumThreads(uint32_t numThreads) { m_numThreads = numThreads; }
		[[nodiscard]] uint32_t GetNumThreads() const { return std::max(static_cast<uint32_t>(1), m_numThreads); }
		
		void SetFrameBufferClearColor(std::array<float, 4> frameBufferClearColor) { m_frameBufferClearColor = frameBufferClearColor; }
		[[nodiscard]] const std::array<float, 4>& GetFrameBufferClearColor() const { return m_frameBufferClearColor; }
		
		[[nodiscard]] bool GetPreferFramebufferFormatSRGB() const { return m_preferFramebufferFormatSRGB; }
		void SetPreferFramebufferFormatSRGB(bool sRGB) { m_preferFramebufferFormatSRGB = sRGB; }

		[[nodiscard]] bool GetLazyRendering() const { return m_lazyRendering; }
		void SetLazyRendering(bool lazyRender) { m_lazyRendering = lazyRender; }

		[[nodiscard]] bool GetVSync() const { return m_vSync; }
		void SetVSync(bool vSync) { m_vSync = vSync; }

		[[nodiscard]] int32_t GetFpsCap() const { return m_fpsCap; }
		void SetFpsCap(int32_t fpsCap)
		{
			if (m_fpsCap != fpsCap)
			{
				m_fpsCap = fpsCap;
				OnFpsCapChanged(fpsCap);
			}
		}

		[[nodiscard]] uint32_t GetPrefferedSwapChainImageCount() const { return m_preferredImageCount; }
		void SetPrefferedSwapChainImageCount(uint32_t preferredImageCount) { m_preferredImageCount = preferredImageCount; }
	public:
		Event<int32_t> OnFpsCapChanged;

		private:
		EngineConfiguration();
		~EngineConfiguration() = default;

		uint32_t m_numThreads = 1;
		std::array<float, 4> m_frameBufferClearColor = { 0.39f, 0.58f, 0.93f, 1.0f };
		bool m_preferFramebufferFormatSRGB = true;
		bool m_lazyRendering = false;
		bool m_vSync = false;
		int32_t m_fpsCap = -1; // -1 = no fps cap. 0 = fps cap if vsync, no cap otherwise. > 0 = set fps cap
		#ifdef __APPLE__
				uint32_t m_preferredImageCount = 3;
		#else
				uint32_t m_preferredImageCount = 2;
		#endif

	};
}