/*
 * 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/.
 */

#include "AppFolders.hpp"
#include "PlatformFolders.hpp"

namespace OpenVulkano
{
#ifdef __APPLE__
	AppFolders AppFolders::INSTANCE = AppFolders("");
#else
	AppFolders AppFolders::INSTANCE = AppFolders("openVulkano");
#endif

	void AppFolders::Init(std::string_view appName)
	{
#ifndef __APPLE__
		INSTANCE = AppFolders(appName);
#endif
	}

	void AppFolders::Init(const std::filesystem::path& dir, std::optional<std::string_view> appName)
	{
#ifndef __APPLE__
		INSTANCE = AppFolders(dir, appName);
#endif
	}

	AppFolders::AppFolders(std::string_view appName)
			: appDataHome(PlatformFolders::GetDataHomeDir() / appName)
			, appConfigHome(PlatformFolders::GetConfigHomeDir() / appName)
			, appState(PlatformFolders::GetStateDir() / appName)
			, appDataCache(PlatformFolders::GetCacheDir() / appName)
			, appTemp(PlatformFolders::GetTempDir() / appName)
	{
	}

	AppFolders::AppFolders(const std::filesystem::path& dir, std::optional<std::string_view> appName)
			: appDataHome(dir / appName.value_or(""))
			, appConfigHome(dir / appName.value_or(""))
			, appState(dir / appName.value_or("") / "state")
			, appDataCache(dir / appName.value_or("") / "cache")
			, appTemp(dir / appName.value_or("") / "tmp")
	{
	}

	const std::filesystem::path& AppFolders::GetAppDataHomeDir()
	{
#ifndef __APPLE__
		std::filesystem::create_directories(INSTANCE.appDataHome);
#endif
		return INSTANCE.appDataHome;
	}

	const std::filesystem::path& AppFolders::GetAppConfigHomeDir()
	{
#ifndef __APPLE__
		std::filesystem::create_directories(INSTANCE.appConfigHome);
#endif
		return INSTANCE.appConfigHome;
	}

	const std::filesystem::path& AppFolders::GetAppStateDir()
	{
#ifndef __APPLE__
		std::filesystem::create_directories(INSTANCE.appState);
#endif
		return INSTANCE.appState;
	}

	const std::filesystem::path& AppFolders::GetAppCacheDir()
	{
#ifndef __APPLE__
		std::filesystem::create_directories(INSTANCE.appDataCache);
#endif
		return INSTANCE.appDataCache;
	}

	const std::filesystem::path& AppFolders::GetAppTempDir()
	{
#ifndef __APPLE__
		std::filesystem::create_directories(INSTANCE.appTemp);
#endif
		return INSTANCE.appTemp;
	}
}
