#include <ShlObj_core.h>
#include <unordered_map>
#include "configs.h"
#include "../cheats/misc/logs.h"
#include "..\nSkinz\Skins prewiew.h"


std::vector <std::string> files;
std::vector <std::string> scripts;

auto selected_script = 0;


std::string get_config_dir()
{
	std::string folder;
	static TCHAR path[MAX_PATH];

	if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, NULL, path)))
		folder = std::string(path) + crypt_str("\\DoubleTap.Space\\");

	CreateDirectory(folder.c_str(), NULL);
	return folder;
}

void load_config()
{
	if (cfg_manager->files.empty())
		return;

	cfg_manager->load(cfg_manager->files.at(g_cfg.selected_config), false);
	//c_lua::get().unload_all_scripts();

	//for (auto& script : g_cfg.scripts.scripts)
		//c_lua::get().load_script(c_lua::get().get_script_id(script));

	//scripts = c_lua::get().scripts;

	//if (selected_script >= scripts.size())
		//selected_script = scripts.size() - 1; //-V103

	//for (auto& current : scripts)
	//{
		//if (current.size() >= 5 && current.at(current.size() - 1) == 'c')
			//current.erase(current.size() - 5, 5);
		//else if (current.size() >= 4)
			//current.erase(current.size() - 4, 4);
	//}

	for (auto i = 0; i < g_cfg.skins.skinChanger.size(); ++i)
		all_skins[i] = nullptr;

	//g_cfg.scripts.scripts.clear();

	cfg_manager->load(cfg_manager->files.at(g_cfg.selected_config), true);
	cfg_manager->config_files();

	eventlogs::get().addnew(crypt_str("Loaded ") + files.at(g_cfg.selected_config) + crypt_str(" config"), Color::Yellow, false);
}

void save_config()
{
	if (cfg_manager->files.empty())
		return;

	//g_cfg.scripts.scripts.clear();

	//for (auto i = 0; i < c_lua::get().scripts.size(); ++i)
	//{
		//auto script = c_lua::get().scripts.at(i);

		//if (c_lua::get().loaded.at(i))
			//g_cfg.scripts.scripts.emplace_back(script);
	//}

	cfg_manager->save(cfg_manager->files.at(g_cfg.selected_config));
	cfg_manager->config_files();

	eventlogs::get().addnew(crypt_str("Saved ") + files.at(g_cfg.selected_config) + crypt_str(" config"), Color::Yellow, false);
}

void remove_config()
{
	if (cfg_manager->files.empty())
		return;

	eventlogs::get().addnew(crypt_str("Removed ") + files.at(g_cfg.selected_config) + crypt_str(" config"), Color::Yellow, false);

	cfg_manager->remove(cfg_manager->files.at(g_cfg.selected_config));
	cfg_manager->config_files();

	files = cfg_manager->files;

	if (g_cfg.selected_config >= files.size())
		g_cfg.selected_config = files.size() - 1; //-V103

	for (auto& current : files)
		if (current.size() > 2)
			current.erase(current.size() - 3, 3);
}

void add_config()
{
	auto empty = true;

	for (auto current : g_cfg.new_config_name)
	{
		if (current != ' ')
		{
			empty = false;
			break;
		}
	}

	if (empty)
		g_cfg.new_config_name = crypt_str("config");

	eventlogs::get().addnew(crypt_str("Added ") + g_cfg.new_config_name + crypt_str(" config"), Color::Yellow, false);

	if (g_cfg.new_config_name.find(crypt_str(".cfg")) == std::string::npos)
		g_cfg.new_config_name += crypt_str(".cfg");

	cfg_manager->save(g_cfg.new_config_name);
	cfg_manager->config_files();

	g_cfg.selected_config = cfg_manager->files.size() - 1; //-V103
	files = cfg_manager->files;

	for (auto& current : files)
		if (current.size() > 2)
			current.erase(current.size() - 3, 3);
}
