#include "pch.h"
#include "HSTraining.h"
#include <iostream>
#include <fstream>




void HSTraining::read() {
	fs::path bakkesmodPath = gameWrapper->GetBakkesModPath();
	std::string BMpath = normalizePathSeparators(bakkesmodPath.string());
	std::string cfgFile = BMpath + "/cfg/HSTraining.cfg";
	bool shouldRewrite = false;
	
	if (fs::exists(cfgFile)) {
		std::ifstream inFile(cfgFile);
		std::string line;
		std::getline(inFile, line);
		if (line.find("bind") != std::string::npos) {
			// Split the line into words
			size_t firstSpace = line.find(' ');
			size_t secondSpace = line.find(' ', firstSpace + 1);
			if (firstSpace != std::string::npos && secondSpace != std::string::npos) {
				std::string key = line.substr(firstSpace + 1, secondSpace - firstSpace - 1);

				// Find the index in the array
				auto it = std::find(controllerKeys.begin(), controllerKeys.end(), key);
				if (it != controllerKeys.end()) {
					controllerKeybind = std::distance(controllerKeys.begin(), it);

				}
				else {
					controllerKeybind = 11;
					shouldRewrite = true;
				}
			}
		}
		std::getline(inFile, line);
		if (line.find("bind") != std::string::npos) {
			// Split the line into words
			size_t firstSpace = line.find(' ');
			size_t secondSpace = line.find(' ', firstSpace + 1);
			if (firstSpace != std::string::npos && secondSpace != std::string::npos) {
				std::string key = line.substr(firstSpace + 1, secondSpace - firstSpace - 1);

				// Find the index in the array
				auto it = std::find(KeyBoardKeys.begin(), KeyBoardKeys.end(), key);
				if (it != KeyBoardKeys.end()) {
					KeyBoardKeybind = std::distance(KeyBoardKeys.begin(), it);
				}
				else {
					KeyBoardKeybind = 42;
					shouldRewrite = true;
				}
			}
		}
		if (!shouldRewrite) {
			return;
		}
	}
	else {
		controllerKeybind = 11;
		KeyBoardKeybind = 42;
	}
	write();
	

}


void HSTraining::write() {

	
	fs::path bakkesmodPath = gameWrapper->GetBakkesModPath();
	std::string BMpath = normalizePathSeparators(bakkesmodPath.string());
	std::string cfgFile = BMpath + "/cfg/HSTraining.cfg";
	if (fs::exists(cfgFile)) {
		std::ifstream inFile(cfgFile);
		std::string line;
		std::getline(inFile, line);
		if (line.find("bind") != std::string::npos) {
			// Split the line into words
			size_t firstSpace = line.find(' ');
			size_t secondSpace = line.find(' ', firstSpace + 1);
			if (firstSpace != std::string::npos && secondSpace != std::string::npos) {
				std::string key = line.substr(firstSpace + 1, secondSpace - firstSpace - 1);

				// Find the index in the array
				auto it = std::find(controllerKeys.begin(), controllerKeys.end(), key);
				if (it != controllerKeys.end()) {
					int index = std::distance(controllerKeys.begin(), it);
					cvarManager->executeCommand("unbind " + controllerKeys[index]);
				}
			}
		}
		std::getline(inFile, line);
		if (line.find("bind") != std::string::npos) {
			// Split the line into words
			size_t firstSpace = line.find(' ');
			size_t secondSpace = line.find(' ', firstSpace + 1);
			if (firstSpace != std::string::npos && secondSpace != std::string::npos) {
				std::string key = line.substr(firstSpace + 1, secondSpace - firstSpace - 1);

				// Find the index in the array
				auto it = std::find(KeyBoardKeys.begin(), KeyBoardKeys.end(), key);
				if (it != KeyBoardKeys.end()) {
					int index = std::distance(KeyBoardKeys.begin(), it);
					cvarManager->executeCommand("unbind " + KeyBoardKeys[index]);
				}
			}
		}
	}
	std::ofstream fout(cfgFile, std::ios::trunc);
	fout << "bind " << controllerKeys[controllerKeybind] << " \"controlInput\"" << std::endl;
	fout << "bind " << KeyBoardKeys[KeyBoardKeybind] << " \"controlInput\"" << std::endl;
	/*fout << "bind XboxTypeS_DPad_Up \"HitHigh\"" << std::endl;
	fout << "bind XboxTypeS_DPad_Left \"HitAtYou\"" << std::endl;
	fout << "bind XboxTypeS_DPad_Down \"HitTowardsOrange\"" << std::endl;
	fout << "bind XboxTypeS_DPad_Right \"ScoreGoal\"" << std::endl;*/
	fout.close();
	cvarManager->loadCfg("HSTraining.cfg");

}
