#include "ExploitsLogging.hpp"
#include "../imgui_helper.hpp"
#include "SetManager.hpp"
#include "../imgui_custom.hpp"
#include "../Menu.hpp"

#include <Windows.h>
#include <fstream>
#include <sstream>

#include <Globals.hpp>

namespace Functions_t {
	namespace Game {
		namespace VRCCoreFavoriteListModel {
			void Add(VRC_Core_FavoriteListModel_o* list, Unity::System_String* contentId, System_Action_o* OnSuccess = nullptr, System_Action_string__o* onFailure = nullptr);
		}
	}
}

ExploitsLogging::ExploitsLogging() : Module(obf("Logging"), obf("")) {
	SaveCurrentFavorites = &SetManager::i().add(new Set(false, obf("SaveCurrentFavorites"), getName())).getBVal();
	logSelfEvents = &SetManager::i().add(new Set(false, obf("logSelfEvents"), getName())).getBVal();
	logEvent1 = &SetManager::i().add(new Set(false, obf("logEvent1"), getName())).getBVal();
	logEvent8 = &SetManager::i().add(new Set(false, obf("logEvent8"), getName())).getBVal();
	logEvent12 = &SetManager::i().add(new Set(false, obf("logEvent12"), getName())).getBVal();
	logEvent13 = &SetManager::i().add(new Set(false, obf("logEvent13"), getName())).getBVal();
	logEvent17 = &SetManager::i().add(new Set(false, obf("logEvent17"), getName())).getBVal();
	logEvent33 = &SetManager::i().add(new Set(false, obf("logEvent33"), getName())).getBVal();
	logEvent60 = &SetManager::i().add(new Set(false, obf("logEvent60"), getName())).getBVal();
	logEvent71 = &SetManager::i().add(new Set(false, obf("logEvent71"), getName())).getBVal();
	logEvent72 = &SetManager::i().add(new Set(false, obf("logEvent72"), getName())).getBVal();
	logEvent202 = &SetManager::i().add(new Set(false, obf("logEvent202"), getName())).getBVal();
}

void ExploitsLogging::renderImGui() {
	ImGui::Checkbox_(obf("Save Current Favorite Avatars").c_str(), SaveCurrentFavorites);
	ImGui::SameLine();
	if (ImGui::Button(obf("Load").c_str())) {
		std::ifstream inputFile(obf("C:\\KAKEData\\FavAvatarIDs.txt").c_str());
		if (inputFile.is_open())
		{
			std::string avatarID;
			while (std::getline(inputFile, avatarID))
			{
				std::stringstream buffer;
				buffer << obf("Read Avatar ID: ") << avatarID;
				Console::i().logInfo(buffer.str());

				if (CurrentAvatarFavoritesList != nullptr)
				{
					Functions_t::Game::VRCCoreFavoriteListModel::Add(CurrentAvatarFavoritesList, IL2CPP::String::New(avatarID.c_str())); // spoof call = ban?
					// auto FavsList = (Unity::CComponent*)CurrentAvatarFavoritesList;
					// FavsList->CallMethodSafe<void*>("Add", IL2CPP::String::New(avatarID.c_str()));
					// FavsList->CallMethod<void*>("Add", IL2CPP::String::New(avatarID.c_str())); // calling it this way causes a crash
				}
			}
			inputFile.close();
		}
		else
		{
			Console::i().logError(obf("[!] Error Reading Avatar File"));
		}
	}

	//ImGui::Checkbox_(obf("Log Self Events").c_str(), logSelfEvents);
	//ImGui::Checkbox_(obf("Log Event 1").c_str(), logEvent1);
	//ImGui::Checkbox_(obf("Log Event 8").c_str(), logEvent8);
	//ImGui::Checkbox_(obf("Log Event 12").c_str(), logEvent12);
	//ImGui::Checkbox_(obf("Log Event 13").c_str(), logEvent13);
	//ImGui::Checkbox_(obf("Log Event 17").c_str(), logEvent17);
	//ImGui::Checkbox_(obf("Log Event 33").c_str(), logEvent33);
	//ImGui::Checkbox_(obf("Log Event 60").c_str(), logEvent60);
	//ImGui::Checkbox_(obf("Log Event 71").c_str(), logEvent71);
	//ImGui::Checkbox_(obf("Log Event 72").c_str(), logEvent72);
	//ImGui::Checkbox_(obf("Log Event 202").c_str(), logEvent202);
}