#include "pch.h"
#include "HSTraining.h"
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

inline const char* const BoolToString(bool b)
{
	return b ? "true" : "false";
}
// Name of the plugin to be shown on the f2 -> plugins list
std::string HSTraining::GetPluginName()
{
	return "HSTraining";
}


// Do ImGui rendering here
void HSTraining::Render()
{
	if (IUse == 0){
		setUpShot();
	}
	
	if (!isWindowOpen_)
	{
		cvarManager->executeCommand("togglemenu " + GetMenuName());
		
	}
	
	ImGuiWindowFlags windowFlags = 0;

	//ImGui::SetNextWindowSizeConstraints(ImVec2(55 + 250 + 55 + 250 + 80 + 100, 600), ImVec2(FLT_MAX, FLT_MAX));
	ImGui::SetNextWindowSizeConstraints(ImVec2(800, 400), ImVec2(FLT_MAX, FLT_MAX));
	if (!ImGui::Begin(menuTitle_.c_str(), &this ->isWindowOpen_, windowFlags))
	{
		// Early out if the window is collapsed, as an optimization.
		ImGui::End();
		return;
	}
	if (showSecondWindow)
	{
		auto InputIntClamped = [](const char* label, int& value, int min, int max, const char* tooltip) {
			static char buf[32] = {};
			snprintf(buf, sizeof(buf), "%d", value);
			ImGui::InputText(label, buf, sizeof(buf), ImGuiInputTextFlags_CharsDecimal);
			if (ImGui::IsItemDeactivatedAfterEdit()) {
				char* end;
				float v = strtof(buf, &end);
				if (*end == '\0') { // Valid float
					int rounded = static_cast<int>(std::ceil(v));
					if (rounded < min) rounded = min;
					if (rounded > max) rounded = max;
					value = rounded;
				}
			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("%s", tooltip);
			}
			};

		// Helper lambda for float input with range checking
		auto InputFloatClamped = [](const char* label, float& value, int min, int max, const string& tooltip) {
			static char buf[32] = {};
			snprintf(buf, sizeof(buf), "%.3f", value);
			ImGui::InputText(label, buf, sizeof(buf), ImGuiInputTextFlags_CharsDecimal);
			if (ImGui::IsItemDeactivatedAfterEdit()) {
				char* end;
				float v = strtof(buf, &end);
				if (*end == '\0') { // Valid float
					if (v < min) v = min;
					if (v > max) v = max;
					value = v;
				}
			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("%s", tooltip.c_str());
			}
			};
		if (ImGui::BeginTabBar("Create Shot", ImGuiTabBarFlags_None)) {
			if (ImGui::BeginTabItem("Ball")) {
				ImGui::TextWrapped("Here You can test the ball part of the shot and set which shot is modified.\n");

				CVarWrapper TrainingPackNumCvar = cvarManager->getCvar("TrainingPackNum");
				if (!TrainingPackNumCvar) { return; }
				int TrainingPackNum = TrainingPackNumCvar.getIntValue();

				if (!trainingPacks.empty() && TrainingPackNum >= 0 && TrainingPackNum < trainingPacks.size()) {
					ImGui::Text("Currently Selected Training Pack: %s", trainingPacks[TrainingPackNum].c_str());
				}
				if (openEditPopup) {
					ImGui::Text("Currently editing shot: %s", shotName.c_str());
				}
				if (ImGui::Checkbox("Enable textboxes", &sliderOrTextBox)) {
				}
				if (ImGui::IsItemHovered()) {
					ImGui::SetTooltip("Toggle Whether sliders or textboxes are used to input");
				}
				
				if (ImGui::Button("Moves The Ball")) {
					gameWrapper->Execute([this](GameWrapper* gw) { cvarManager->executeCommand("BallMove"); });
				}

				if (ImGui::IsItemHovered()) {
					ImGui::SetTooltip("Moves The Ball To a New Location");
				}
				if (sliderOrTextBox) {
					// Ball location (as float text boxes)
					ImGui::Text("Ball Location X Value");
					ImGui::Text("Range: [-4096, 4096]");
					InputFloatClamped("###BallX", length, -4096, 4096, "The X position of the ball is " + std::to_string(length));

					ImGui::Text("Ball Location Y Value");
					ImGui::Text("Range: [-5120, 5120]");
					InputFloatClamped("###BallY", width, -5120, 5120, "The Y position of the ball is " + std::to_string(width));

					ImGui::Text("Ball Location Z Value");
					ImGui::Text("Range: [0, 2044]");
					InputFloatClamped("###BallZ", height, 0, 2044, "The Z position of the ball is " + std::to_string(height));

					if (ImGui::Button("Adjust The Velocity Of The Ball")) {
						gameWrapper->Execute([this](GameWrapper* gw) { cvarManager->executeCommand("BallVelocity"); });
					}
					if (ImGui::IsItemHovered()) {
						ImGui::SetTooltip("Add the new velocity of the ball");
					}

					// Ball velocity (as float text boxes)
					ImGui::Text("Ball Velocity X Value");
					ImGui::Text("Range: [-6000, 6000]");
					InputFloatClamped("###BallVelX", speedX, -6000, 6000, "The X value of the velocity is " + std::to_string(speedX));

					ImGui::Text("Ball Velocity Y Value");
					ImGui::Text("Range: [-6000, 6000]");
					InputFloatClamped("###BallVelY", speedY, -6000, 6000, "The Y value of the velocity is " + std::to_string(speedY));

					ImGui::Text("Ball Velocity Z Value");
					ImGui::Text("Range: [-6000, 6000]");
					InputFloatClamped("###BallVelZ", speedZ, -6000, 6000, "The Z value of the velocity is " + std::to_string(speedZ));
				}
				else {
					// Ball Location X
					ImGui::Text("Ball Location X Value");
					ImGui::Text("Range: [-4096, 4096]");
					if (ImGui::SliderFloat("###BallX", &length, -4096.0, 4096.0)) {}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "The X Position Of The Ball Is " + std::to_string(length);
						ImGui::SetTooltip(hoverText.c_str());
					}

					// Ball Location Y
					ImGui::Text("Ball Location Y Value");
					ImGui::Text("Range: [-5120, 5120]");
					if (ImGui::SliderFloat("###BallY", &width, -5120.0, 5120.0)) {}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "The Y Position Of The Ball Is " + std::to_string(width);
						ImGui::SetTooltip(hoverText.c_str());
					}

					// Ball Location Z
					ImGui::Text("Ball Location Z Value");
					ImGui::Text("Range: [0, 2044]");
					if (ImGui::SliderFloat("###BallZ", &height, 0.0, 2044.0)) {}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "The Z Position Of The Ball Is " + std::to_string(height);
						ImGui::SetTooltip(hoverText.c_str());
					}

					if (ImGui::Button("Adjust The Velocity Of The Ball")) {
						gameWrapper->Execute([this](GameWrapper* gw) { cvarManager->executeCommand("BallVelocity"); });
					}
					if (ImGui::IsItemHovered()) {
						ImGui::SetTooltip("Add the new velocity of the ball");
					}

					// Ball Velocity X
					ImGui::Text("Ball Velocity X Value");
					ImGui::Text("Range: [-6000, 6000]");
					if (ImGui::SliderFloat("###BallVelX", &speedX, -6000.0, 6000.0)) {}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "The X Value Of The Velocity Is " + std::to_string(speedX);
						ImGui::SetTooltip(hoverText.c_str());
					}

					// Ball Velocity Y
					ImGui::Text("Ball Velocity Y Value");
					ImGui::Text("Range: [-6000, 6000]");
					if (ImGui::SliderFloat("###BallVelY", &speedY, -6000.0, 6000.0)) {}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "The Y Value Of The Velocity Is " + std::to_string(speedY);
						ImGui::SetTooltip(hoverText.c_str());
					}

					// Ball Velocity Z
					ImGui::Text("Ball Velocity Z Value");
					ImGui::Text("Range: [-6000, 6000]");
					if (ImGui::SliderFloat("###BallVelZ", &speedZ, -6000.0, 6000.0)) {}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "The Z Value Of The Velocity Is " + std::to_string(speedZ);
						ImGui::SetTooltip(hoverText.c_str());
					}
				}
				

				CVarWrapper shotNumberCvar = cvarManager->getCvar("shotNumber1");
				int shotNumber = shotNumberCvar.getIntValue();
				if (!shotNumberCvar) {
					return;
				}

				//deals with the speed of the ball
				ImGui::TextWrapped("Here You can pick the overall speed of the ball.");
				if (ImGui::Button("Speed of The Ball")) {
					gameWrapper->Execute([this](GameWrapper* gw) { cvarManager->executeCommand("BallSpeed"); });
				}

				if (ImGui::IsItemHovered()) {
					ImGui::SetTooltip("Changes the speed of the ball");
				}
				//by players choice what value should reset the speed of the ball
				CVarWrapper speedEnableCvar = cvarManager->getCvar("speed_enabled");
				if (!speedEnableCvar) {return;}
				int speedEnable = speedEnableCvar.getIntValue();
				speedEnableCvar.setValue(2);
				
				//value of the speed of the ball
				CVarWrapper speedBallCvar = cvarManager->getCvar("cool_speed");
				if (!speedBallCvar) { return; }
				int speedBall = speedBallCvar.getIntValue();
				
				if (sliderOrTextBox) {
					ImGui::Text("Set The Speed Of The Ball");
					ImGui::Text("Range: [2985, 4600]");
					string tempStr = "The speed of the ball will be " + std::to_string(speedBall);
					InputIntClamped("###SpeedBall", speedBall, 2985, 4600, tempStr.c_str());
					speedBallCvar.setValue(speedBall);
				}
				else {
					ImGui::Text("Set The Speed Of The Ball");
					ImGui::Text("Range: [2985, 4600]");
					if (ImGui::SliderInt("###SpeedBall", &speedBall, 2985, 4600)) {
						speedBallCvar.setValue(speedBall);
					}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "The speed of the ball will be " + std::to_string(speedBall);
						ImGui::SetTooltip(hoverText.c_str());
					}
				}

				static int i0 = 123;
				static float f0 = 0.001f;

				ImGui::InputText("Type Shot Name", newShotName, IM_ARRAYSIZE(newShotName));
				
				
				std::vector<std::string> allShotNames;
				allShotNames.reserve(ShotData.size());

				for (const auto& shot : ShotData) {
					allShotNames.push_back(shot.getShotName());
				}
				bool nameExists = std::find(allShotNames.begin(), allShotNames.end(), std::string(newShotName)) != allShotNames.end();
				std::string badChars = "/\\|*?\"<>";
				bool containsBadChar = std::string(newShotName).find_first_of(badChars) != std::string::npos;
				bool editingCurrentShot = false;
				if (editIndex < ShotData.size()) {
					editingCurrentShot = openEditPopup && ShotData[editIndex].getShotName() == newShotName;
				}
				 
				if (nameExists && !editingCurrentShot) {
					ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "A Shot with this name already exists!");
				}
				if ((newShotName[0] == '\0')) {
					ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Please Enter A Name");
				}
				
				if (containsBadChar) {
					ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Your Shot Name cannot contain the characters / , \\ , | , * , ? , \" , < , or >");
				}
				if (openEditPopup) {
					//buttons for loading and overiding a shot
					if (ImGui::Button("Edit Shot") && (!nameExists || editingCurrentShot) && !(newShotName[0] == '\0') && !containsBadChar && editIndex < ShotData.size()) {
						shotName = std::string(newShotName);
						ShotData[editIndex].setShot(height, length, width, speedX, speedY, speedZ, carX, carY, carZ, carYaw, shotName,speedBall, carPitch, carRoll);
						openEditPopup = false;
						loadTrainingPack(TrainingPackNum);
						CVarWrapper speedEnableCvar = cvarManager->getCvar("speed_enabled");
						if (!speedEnableCvar) { return; }
						speedEnableCvar.setValue(1);
						showSecondWindow = false;
						sliderOrTextBox = false;
					}
					ImGui::SameLine();
					if (ImGui::IsItemHovered()) {
						ImGui::SetTooltip("Edits the currently selected Shot in the currently selected Training Pack");
					}

					if (ImGui::Button("Cancel"))
					{
						loadShot();
						showSecondWindow = false;
						sliderOrTextBox = false;
					}
				}
				else {
					//buttons for loading and overiding a shot
					if (ImGui::Button("Save Shot") && !nameExists && !(newShotName[0] == '\0') && !containsBadChar) {
						shotName = std::string(newShotName);
						createNewShot(TrainingPackNum, height, length, width, speedX, speedY, speedZ, carX, carY, carZ, carYaw, shotName, carPitch, carRoll);
						CVarWrapper speedEnableCvar = cvarManager->getCvar("speed_enabled");
						speedEnableCvar.setValue(1);
						showSecondWindow = false;
						sliderOrTextBox = false;
					}
					ImGui::SameLine();
					if (ImGui::IsItemHovered()) {
						ImGui::SetTooltip("Saves a Shot to the selected Training Pack");
					}

					if (ImGui::Button("Cancel"))
					{
						loadShot();
						showSecondWindow = false;
						sliderOrTextBox = false;
					}
				}
				
				ImGui::EndTabItem();
			}
			if (ImGui::BeginTabItem("Car location")) {
				ImGui::TextWrapped("Here You can pick the location of your car");

				CVarWrapper carCvar = cvarManager->getCvar("car_enabled");
				if (!carCvar) {
					return;
				}
				bool carEnabled1 = carCvar.getBoolValue();

				if (ImGui::Checkbox("Enable Car Reset", &carEnabled1)) {
					carCvar.setValue(carEnabled1);
				}
				if (ImGui::IsItemHovered()) {
					ImGui::SetTooltip("Toggle Whether The Car Resets When A Shot Is Reset");
				}
				if (ImGui::Checkbox("Enable textboxes", &sliderOrTextBox)) {
				}
				if (ImGui::IsItemHovered()) {
					ImGui::SetTooltip("Toggle Whether sliders or textboxes are used to input");
				}
				//moving the car
				if (ImGui::Button("Moves The Car")) {
					carCvar.setValue(true);
					gameWrapper->Execute([this](GameWrapper* gw) { cvarManager->executeCommand("CarMove"); });
				}

				if (ImGui::IsItemHovered()) {
					ImGui::SetTooltip("Moves The Car To A New Location");
				}
				
				
				if (sliderOrTextBox) {
					
					// Car location (as integer text boxes)
					ImGui::Text("Car Location X Value");
					ImGui::Text("Range: [-4096, 4096]");
					InputFloatClamped("###CarX", carX, -4096, 4096, ("The X position of the car is " + std::to_string(carX)));

					ImGui::Text("Car Location Y Value");
					ImGui::Text("Range: [-5120, 5120]");
					InputFloatClamped("###CarY", carY, -5120, 5120, ("The Y position of the car is " + std::to_string(carY)));

					ImGui::Text("Car Location Z Value");
					ImGui::Text("Range: [10, 2024]");
					InputFloatClamped("###CarZ", carZ, 10, 2024, ("The Z position of the car is " + std::to_string(carZ)));

					// Car rotation (as integer text boxes with range display)
					ImGui::Text("Car Yaw Rotation Value");
					ImGui::Text("Range: [-32764, 32764]");
					InputIntClamped("###CarYaw", carYaw, -32764, 32764, ("This sets the rotation of the car on the Yaw axis to " + std::to_string(carYaw)).c_str());

					ImGui::Text("Car Pitch Rotation Value");
					ImGui::Text("Range: [-32764, 32764]");
					InputIntClamped("###CarPitch", carPitch, -32764, 32764, ("This sets the rotation of the car on the Pitch axis to " + std::to_string(carPitch)).c_str());

					ImGui::Text("Car Roll Rotation Value");
					ImGui::Text("Range: [-32764, 32764]");
					InputIntClamped("###CarRoll", carRoll, -32764, 32764, ("This sets the rotation of the car on the Roll axis to " + std::to_string(carRoll)).c_str());
				}
				else {
					//Car location sliders
					ImGui::Text("Car Location X Value");
					ImGui::Text("Range: [-4096, 4096]");
					if (ImGui::SliderFloat("###CarX", &carX, -4096.0, 4096.0)) {

					}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "The X postion of the car is " + std::to_string(carX);
						ImGui::SetTooltip(hoverText.c_str());
					}

					ImGui::Text("Car Location Y Value");
					ImGui::Text("Range: [-5120, 5120]");
					if (ImGui::SliderFloat("###CarY", &carY, -5120.0, 5120.0)) {

					}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "The Y postion of the car is " + std::to_string(carY);
						ImGui::SetTooltip(hoverText.c_str());
					}

					ImGui::Text("Car Location Z Value");
					ImGui::Text("Range: [10, 2024]");
					if (ImGui::SliderFloat("###CarZ", &carZ, 10.0, 2024.0)) {

					}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "The Z postion of the car is " + std::to_string(carZ);
						ImGui::SetTooltip(hoverText.c_str());
					}

					
					//rotation of the car sliders
					ImGui::Text("Car Yaw Rotation Value");
					ImGui::Text("Range: [-32764, 32764]");
					if (ImGui::SliderInt("###CarYaw", &carYaw, -32764, 32764)) {

					}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "This sets the rotation of the car on the Yaw axis to " + std::to_string(carYaw);
						ImGui::SetTooltip(hoverText.c_str());
					}
					ImGui::Text("Car Pitch Rotation Value");
					ImGui::Text("Range: [-32764, 32764]");
					if (ImGui::SliderInt("###CarPitch", &carPitch, -32764, 32764)) {

					}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "This sets the rotation of the car on the Pitch axis to " + std::to_string(carPitch);
						ImGui::SetTooltip(hoverText.c_str());
					}
					ImGui::Text("Car Roll Rotation Value");
					ImGui::Text("Range: [-32764, 32764]");
					if (ImGui::SliderInt("###CarRoll", &carRoll, -32764, 32764)) {

					}
					if (ImGui::IsItemHovered()) {
						std::string hoverText = "This sets the rotation of the car on the Roll axis to " + std::to_string(carRoll);
						ImGui::SetTooltip(hoverText.c_str());
					}
				}
				

				





				ImGui::EndTabItem();
			}
		}
		ImGui::End();
	}
	else {
		if (ImGui::BeginTabBar("Heatseeker Training", ImGuiTabBarFlags_None)) {

			if (ImGui::BeginTabItem("How To Use")) {
				CVarWrapper heatCvar = cvarManager->getCvar("heat_enabled");
				if (!heatCvar) {
					return;
				}
				bool HeatEnabled1 = heatCvar.getBoolValue();
				if (ImGui::Checkbox("Enable HeatSeakerTraining", &HeatEnabled1)) {
					heatCvar.setValue(HeatEnabled1);
					trainingActive = false;
				}
				if (ImGui::IsItemHovered()) {
					ImGui::SetTooltip("Toggle Whether The Plugin is on or off");
				}


				ImGui::TextUnformatted("\nHeatSeaker Training is set up into two Main Sections. That being creating the shots and selecting which\n\nshots you want to practice. Creating the shots are found under the tabs, Ball and Car Location. Selecting\n\nfrom the various shots you want to use as practice can be found under the Training tab. In order to \n\ncreate a shot first go to the Ball tab and change the sliders how you see fit. Then do the same under \n\nthe Car Location tab. You can test each aspect of the shot by pressing the various buttons. When you \n\nare all done go back to the Ball tab and select a shot from the dropdown menu, name it in the textbox \n\nand click overide. This will delete the shot data, that is stored in that specific shot so be careful to \n\nselect a shot you want to overide. X values of shots manipulate the object on the axis between the \n\nside walls. Y values of shots manipulate the object on the axis between the goals. Z values of \n\nshots manipulate the object on the axis between the celing and floor");

				ImGui::EndTabItem();
			}
			if (ImGui::BeginTabItem("Training Packs")) {
				ImGui::TextWrapped("Here you can switch between training packs.\n");

				CVarWrapper TrainingPackNumCvar = cvarManager->getCvar("TrainingPackNum");
				if (!TrainingPackNumCvar) { return; }
				int TrainingPackNum = TrainingPackNumCvar.getIntValue();

				static bool openRenamePopup = false;
				static bool openDeletePopup = false;
				static bool openAddPopup = false;
				static bool openCopyPopup = false;
				static int renameIndex = -1;
				static int deleteIndex = -1;
				static int copyIndex = -1;
				static char renameBuffer[128] = "";
				static char nameBuffer[128] = "";
				static char copyBuffer[128] = "";
				

				if (!trainingPacks.empty() && TrainingPackNum >= 0 && TrainingPackNum < trainingPacks.size()) {
					ImGui::Text("Selected: %s", trainingPacks[TrainingPackNum].c_str());
				}float child_height = 200.0f; // pixels high before scroll appears
				ImGui::BeginChild("TrainingPackList", ImVec2(0, child_height), true); // true = border
				float label_width = 200.0f;
				for (int i = 0; i < trainingPacks.size(); ++i) {
					ImGui::PushID(i);

					ImGui::PushItemWidth(label_width);
					bool is_selected = (TrainingPackNum == i);
					if (ImGui::Selectable(trainingPacks[i].c_str(), is_selected, 0, ImVec2(label_width, 0))) {
						TrainingPackNum = i;
						TrainingPackNumCvar.setValue(i);
						loadTrainingPack(i);
					}
					ImGui::PopItemWidth();

					ImGui::SameLine();
					ImGui::PushItemWidth(100.0f);
					if (ImGui::SmallButton("Rename")) {
						renameIndex = i;
						strncpy(renameBuffer, trainingPacks[i].c_str(), sizeof(renameBuffer));
						renameBuffer[sizeof(renameBuffer) - 1] = '\0';
						openRenamePopup = true;
					}
					ImGui::PopItemWidth();
					ImGui::SameLine();
					ImGui::PushItemWidth(100.0f);
					if (ImGui::SmallButton("Delete")) {
						deleteIndex = i;
						openDeletePopup = true;
					}
					ImGui::SameLine();
					ImGui::PushItemWidth(100.0f);
					if (ImGui::SmallButton("Copy")) {
						copyIndex = i;
						std::string newPackName = "Copied Pack Name";
						strncpy(copyBuffer, newPackName.c_str(), sizeof(copyBuffer));
						copyBuffer[sizeof(copyBuffer) - 1] = '\0';
						openCopyPopup = true;
					}
					ImGui::PopItemWidth();
					ImGui::PopID();
				}

				if (openCopyPopup) {
					ImGui::OpenPopup("Copy Training Pack");
				}

				if (openRenamePopup) {
					ImGui::OpenPopup("Rename Training Pack");
				}
				if (openDeletePopup) {
					ImGui::OpenPopup("Delete Training Pack");
				}
				//copy popup modal
				if (ImGui::BeginPopupModal("Copy Training Pack", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
					ImGui::Text("Enter a new name:");
					ImGui::InputText("##NameInput", copyBuffer, IM_ARRAYSIZE(copyBuffer));
					std::string badChars = "./\\|*?\"<>";
					bool containsBadChar = std::string(copyBuffer).find_first_of(badChars) != std::string::npos;
					bool nameExists = std::find(trainingPacks.begin(), trainingPacks.end(), std::string(copyBuffer)) != trainingPacks.end();

					if (nameExists) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "A training pack with this name already exists!");
					}
					if ((copyBuffer[0] == '\0')) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Please Enter A Name");
					}
					if (containsBadChar) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Your training pack name cannot contain the characters . , / , \\ , | , * , ? , \" , < , or >");
					}

					if (ImGui::Button("OK") && copyIndex >= 0 && !nameExists && !containsBadChar && !(copyBuffer[0] == '\0')) {
						copyTrainingPack(copyIndex, copyBuffer);
						openCopyPopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::SameLine();
					if (ImGui::Button("Cancel")) {
						openCopyPopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::EndPopup();
				}


				//Delete popup modal
				if (ImGui::BeginPopupModal("Delete Training Pack", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
					ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Are You Sure You Want To Delete This Pack. This is Permanent");
					ImGui::Text("Selected: %s", trainingPacks[deleteIndex].c_str());

					if (ImGui::Button("OK") && deleteIndex >= 0) {
						deleteTrainingPack(deleteIndex);
						openDeletePopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::SameLine();
					if (ImGui::Button("Cancel")) {
						openDeletePopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::EndPopup();
				}
				// Rename popup modal
				if (ImGui::BeginPopupModal("Rename Training Pack", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
					ImGui::Text("Enter a new name:");
					ImGui::InputText("##RenameInput", renameBuffer, IM_ARRAYSIZE(renameBuffer));

					bool nameExists = std::find(trainingPacks.begin(), trainingPacks.end(), std::string(renameBuffer)) != trainingPacks.end();
					std::string badChars = "./\\|*?\"<>";
					bool containsBadChar = std::string(renameBuffer).find_first_of(badChars) != std::string::npos;

					if (containsBadChar) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Your training pack name cannot contain the characters . , / , \\ , | , * , ? , \" , < , or >");
					}
					if ((renameBuffer[0] == '\0')) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Please Enter A Name");
					}
					if (nameExists && renameIndex >= 0 && trainingPacks[renameIndex] != renameBuffer ) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "A training pack with this name already exists!");
					}

					if (ImGui::Button("OK") && !nameExists && renameIndex >= 0 && !containsBadChar && !(renameBuffer[0] == '\0')) {
						renameTrainingPack(renameIndex, renameBuffer);
						openRenamePopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::SameLine();
					if (ImGui::Button("Cancel")) {
						openRenamePopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::EndPopup();

				}
				ImGui::EndChild();
				ImGui::Text("\n");
				if (ImGui::SmallButton("Add A New Pack")) {
					std::string newPackName = "New Pack Name";
					strncpy(nameBuffer, newPackName.c_str(), sizeof(nameBuffer));
					nameBuffer[sizeof(nameBuffer) - 1] = '\0';
					openAddPopup = true;
				}
				ImGui::SameLine();
				if (ImGui::SmallButton("Reload Packs")) {
					getTrainingPacks();
				}
				if (openAddPopup) {
					ImGui::OpenPopup("Add Training Pack");
				}
				//add a new Training pack modal
				if (ImGui::BeginPopupModal("Add Training Pack", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
					ImGui::Text("Enter a new name:");
					ImGui::InputText("##NameInput", nameBuffer, IM_ARRAYSIZE(nameBuffer));

					bool nameExists = std::find(trainingPacks.begin(), trainingPacks.end(), std::string(nameBuffer)) != trainingPacks.end();

					if (nameExists) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "A training pack with this name already exists!");
					}
					if (nameBuffer[0] == '\0') {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Please Enter A Name");
					}
					std::string badChars = "./\\|*?\"<>";
					bool containsBadChar = std::string(nameBuffer).find_first_of(badChars) != std::string::npos;
					if (containsBadChar) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Your training pack name cannot contain the characters . , / , \\ , | , * , ? , \" , < , or >");
					}
					if (ImGui::Button("OK") && !nameExists && !containsBadChar && !(nameBuffer[0] == '\0')) {
						createTrainingPack(nameBuffer);
						openAddPopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::SameLine();
					if (ImGui::Button("Cancel")) {
						openAddPopup = false;
						ImGui::CloseCurrentPopup();
					}
					ImGui::EndPopup();
				}
				ImGui::EndTabItem();
			}
			if (ImGui::BeginTabItem("Shots")) {
				CVarWrapper TrainingPackNumCvar = cvarManager->getCvar("TrainingPackNum");
				if (!TrainingPackNumCvar) { return; }
				int TrainingPackNum = TrainingPackNumCvar.getIntValue();

				CVarWrapper shotNumberCvar = cvarManager->getCvar("shotNumber1");
				if (!shotNumberCvar) { return; }
				int shotNumber = shotNumberCvar.getIntValue();
				

				static bool openRenamePopup = false;
				static bool openWarningPopup = false;
				static bool openDeletePopup = false;
				static bool openAddPopup = false;
				static bool openCopyPopup = false;
				static bool openTransferPopup = false;
				static int copyIndex = -1;
				static int renameIndex = -1;
				static int deleteIndex = -1;
				static int transferIndex = -1;
				static char renameBuffer[128] = "";
				static char nameBuffer[128] = "";
				static char copyBuffer[128] = "";
				static char transferBuffer[128] = "";
				static char shotName2[128] = "";


				if (!trainingPacks.empty() && TrainingPackNum >= 0 && TrainingPackNum < trainingPacks.size()) {
					ImGui::Text("Selected: %s", trainingPacks[TrainingPackNum].c_str());
				}
				float child_height = 200.0f; // pixels high before scroll appears
				ImGui::BeginChild("Shot List", ImVec2(0, child_height), true); // true = border
				float label_width = 200.0f;

				for (int i = 0; i < ShotData.size(); ++i) {
					ImGui::PushID(i);

					ImGui::PushItemWidth(label_width);
					bool is_selected1 = (shotNumber == i);
					if (ImGui::Selectable(ShotData[i].getShotName().c_str(), is_selected1, 0, ImVec2(label_width, 0))) {
						shotNumber = i;
						shotNumberCvar.setValue(shotNumber);
						trainingActive = false;
						randomizer = false;
						loadShot();
					}
					ImGui::PopItemWidth();

					ImGui::SameLine();
					ImGui::PushItemWidth(100.0f);
					if (ImGui::SmallButton("Rename")) {
						renameIndex = i;
						strncpy(renameBuffer, ShotData[i].getShotName().c_str(), sizeof(renameBuffer));
						renameBuffer[sizeof(renameBuffer) - 1] = '\0';
						openRenamePopup = true;
					}
					ImGui::PopItemWidth();
					ImGui::SameLine();
					ImGui::PushItemWidth(100.0f);
					if (ImGui::SmallButton("Delete")) {
						deleteIndex = i;
						openDeletePopup = true;
					}
					ImGui::SameLine();
					ImGui::PushItemWidth(100.0f);
					if (ImGui::SmallButton("Copy")) {
						copyIndex = i;
						std::string newShotName = "Copied Shot Name";
						if (ShotData.size() >= 60) {
							openWarningPopup = true;
						}
						strncpy(copyBuffer, newShotName.c_str(), sizeof(copyBuffer));
						copyBuffer[sizeof(copyBuffer) - 1] = '\0';
						openCopyPopup = true;
						
					}
					ImGui::PopItemWidth();
					ImGui::SameLine();
					ImGui::PushItemWidth(100.0f);
					if (ImGui::SmallButton("Transfer")) {
						transferIndex = i;
						TPackNum = 0;
						strncpy(shotName2, ShotData[i].getShotName().c_str(), sizeof(shotName2));
						strncpy(transferBuffer, ShotData[i].getShotName().c_str(), sizeof(transferBuffer));
						transferBuffer[sizeof(transferBuffer) - 1] = '\0';
						openTransferPopup = true;
					}
					ImGui::PopItemWidth();
					ImGui::SameLine();
					ImGui::PushItemWidth(100.0f);
					if (ImGui::SmallButton("Edit")){
						editIndex = i;
						if (editIndex >= 0 && editIndex < ShotData.size()) {
							height = ShotData[editIndex].getHeight();
							length = ShotData[editIndex].getLength();
							width = ShotData[editIndex].getWidth();
							speedX = ShotData[editIndex].getSpeedX();
							speedY = ShotData[editIndex].getSpeedY();
							speedZ = ShotData[editIndex].getSpeedZ();
							carX = ShotData[editIndex].getCarX();
							carY = ShotData[editIndex].getCarY();
							carZ = ShotData[editIndex].getCarZ();
							carYaw = ShotData[editIndex].getCarYaw();
							carPitch = ShotData[editIndex].getCarPitch();
							carRoll = ShotData[editIndex].getCarRoll();
							SSpeed = ShotData[editIndex].getBallSpeed();
							shotName = ShotData[editIndex].getShotName();
							CVarWrapper speedBallCvar = cvarManager->getCvar("cool_speed");
							if (!speedBallCvar) {
								return;
							}
							speedBallCvar.setValue(ShotData[editIndex].getBallSpeed());
							strcpy(newShotName, shotName.c_str());
							CVarWrapper speedEnableCvar = cvarManager->getCvar("speed_enabled");
							if (!speedEnableCvar) {
								return;
							}
							speedEnableCvar.setValue(2);
						}
						
						
						randomizer = false;
						trainingActive = false;
						HeatEnabled = true;
						carEnabled = false;
						openEditPopup = true;
						showSecondWindow = true;
						CVarWrapper carCvar = cvarManager->getCvar("car_enabled");
						if (!carCvar) {
							return;
						}
						carCvar.setValue(false);
					}
					ImGui::PopItemWidth();

					ImGui::PopID();
				}
				if (!openWarningPopup) {
					if (openRenamePopup) {
						ImGui::OpenPopup("Rename Shot");
					}
					if (openDeletePopup) {
						ImGui::OpenPopup("Delete Shot");
					}
					if (openTransferPopup) {
						ImGui::OpenPopup("Transfer Shot");
					}
					if (openCopyPopup) {
						ImGui::OpenPopup("Copy Shot");
					}
				}
				else {
					ImGui::OpenPopup("Warning");
				}
				
				//Warning popup modal
				if (ImGui::BeginPopupModal("Warning", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
					ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Adding more shots to this pack may cause lag issues.\nConsider making a new training pack.\nAre you sure you want to add more?");

					
					if (ImGui::Button("OK")) {
						
						if (!openRenamePopup &&
							!openDeletePopup &&
							!openTransferPopup &&
							!openCopyPopup &&
							!openEditPopup &&
							!showSecondWindow)
						{
							showSecondWindow = true;
						}
						if (openTransferPopup) {
							transferShot(TrainingPackNum, TPackNum, transferIndex, transferBuffer);
							loadTrainingPack(TrainingPackNum);
							openTransferPopup = false;
						}
						openWarningPopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::SameLine();
					if (ImGui::Button("Cancel")) {
						openRenamePopup = false;
						openDeletePopup = false;
						openTransferPopup = false;
						openCopyPopup = false;
						openWarningPopup = false;
						openEditPopup = false;
						showSecondWindow = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::EndPopup();
				}
				//Transfer popup modal
				if (ImGui::BeginPopupModal("Transfer Shot", NULL, ImGuiWindowFlags_NoResize))
				{
					static int nextPackNum = TPackNum;
					static int lastPackNum = -1;
					static std::vector<std::string> allShotNames;
					static bool pendingPackChange = false;

					// Set a consistent size for the modal
					ImGui::SetWindowSize(ImVec2(650, 400), ImGuiCond_Always);

					// Title text
					ImGui::TextWrapped("Select a training pack to transfer the shot named: %s", shotName2);

					// Pack list in a scrollable child
					ImGui::BeginChild("PackList", ImVec2(300, 200), true);
					for (int i = 0; i < trainingPacks.size(); ++i)
					{
						ImGui::PushID(i);
						bool is_selected = (nextPackNum == i);
						if (ImGui::Selectable(trainingPacks[i].c_str(), is_selected))
						{
							nextPackNum = i;
							pendingPackChange = true; // Mark for reload next frame
						}
						ImGui::PopID();
					}
					ImGui::EndChild();

					// Commit the selected pack AFTER the loop
					if (pendingPackChange)
					{
						TPackNum = nextPackNum;
						pendingPackChange = false;
					}

					// Reload shot names only when pack changes & not in same frame as click
					if (TPackNum != lastPackNum)
					{
						allShotNames = getAllShotNames(TPackNum);
						lastPackNum = TPackNum;
					}

					// Name input
					ImGui::Text("Enter a new name:");
					ImGui::InputText("##NameInput", transferBuffer, IM_ARRAYSIZE(transferBuffer));

					bool nameExists = std::find(allShotNames.begin(), allShotNames.end(),
						std::string(transferBuffer)) != allShotNames.end();

					if (nameExists && transferIndex >= 0 && TPackNum >= 0) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "A shot with this name already exists!");
					}
						

					if ((transferBuffer[0] == '\0') && transferIndex >= 0) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Please enter a name");
					}
						

					std::string badChars = "/\\|*?\"<>";
					bool containsBadChar = std::string(transferBuffer).find_first_of(badChars) != std::string::npos;
					if (containsBadChar) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Your shot name cannot contain the characters / , \\ , | , * , ? , \" , < , or >");
					}
					// OK button
					if (ImGui::Button("OK") && !nameExists && transferIndex >= 0 &&!(transferBuffer[0] == '\0')&& !containsBadChar)
					{
						if(allShotNames.size() >= 60){
							openWarningPopup = true;
						}
						else {
							transferShot(TrainingPackNum, TPackNum, transferIndex, transferBuffer);
							loadTrainingPack(TrainingPackNum);
							openTransferPopup = false;
							ImGui::CloseCurrentPopup();
						}
					}

					ImGui::SameLine();

					// Cancel button
					if (ImGui::Button("Cancel"))
					{
						openTransferPopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::EndPopup();
				}
				//copy popup modal
				if (ImGui::BeginPopupModal("Copy Shot", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
					ImGui::Text("Enter a new name:");
					ImGui::InputText("##NameInput", copyBuffer, IM_ARRAYSIZE(copyBuffer));

					std::vector<std::string> allShotNames;
					allShotNames.reserve(ShotData.size());

					for (const auto& shot : ShotData) {
						allShotNames.push_back(shot.getShotName());
					}
					bool nameExists = std::find(allShotNames.begin(), allShotNames.end(), std::string(copyBuffer)) != allShotNames.end();
					
					if (nameExists && copyIndex >= 0 ) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "A Shot with this name already exists!");
					}
					if ((copyBuffer[0] == '\0') && copyIndex >= 0 && allShotNames[copyIndex] != copyBuffer) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Please Enter A Name");
					}
					std::string badChars = "/\\|*?\"<>";
					bool containsBadChar = std::string(copyBuffer).find_first_of(badChars) != std::string::npos;
					if (containsBadChar) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Your shot name cannot contain the characters / , \\ , | , * , ? , \" , < , or >");
					}
					if (ImGui::Button("OK") && !nameExists && copyIndex >= 0 && !(copyBuffer[0] == '\0') &&!containsBadChar) {
						transferShot(TrainingPackNum, TrainingPackNum, copyIndex, copyBuffer);
						loadTrainingPack(TrainingPackNum);
						openCopyPopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::SameLine();
					if (ImGui::Button("Cancel")) {
						openCopyPopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::EndPopup();
				}


				//Delete popup modal
				if (ImGui::BeginPopupModal("Delete Shot", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
					ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Are You Sure You Want To Delete This Shot. This is Permanent");
					if (deleteIndex >= 0 && deleteIndex < ShotData.size()) {
						ImGui::Text("Selected: %s", ShotData[deleteIndex].getShotName().c_str());
					}
					

					if (ImGui::Button("OK") && deleteIndex >= 0 && deleteIndex < ShotData.size()) {
						ShotData[deleteIndex].deleteShotFile();
						loadTrainingPack(TrainingPackNum);
						openDeletePopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::SameLine();
					if (ImGui::Button("Cancel")) {
						openDeletePopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::EndPopup();
				}
				// Rename popup modal
				if (ImGui::BeginPopupModal("Rename Shot", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
					ImGui::Text("Enter a new name:");
					ImGui::InputText("##RenameInput", renameBuffer, IM_ARRAYSIZE(renameBuffer));
					std::vector<std::string> allShotNames;
					allShotNames.reserve(ShotData.size());

					for (const auto& shot : ShotData) {
						allShotNames.push_back(shot.getShotName());
					}
					bool nameExists = std::find(allShotNames.begin(), allShotNames.end(), std::string(renameBuffer)) != allShotNames.end();

					if (nameExists && renameIndex >= 0 && allShotNames[renameIndex] != renameBuffer && renameIndex < ShotData.size()) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "A Shot with this name already exists!");
					}
					if ((renameBuffer[0] == '\0') && renameIndex >= 0 && allShotNames[renameIndex] != renameBuffer && renameIndex < ShotData.size()) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Please Enter A Name");
					}
					std::string badChars = "/\\|*?\"<>";
					bool containsBadChar = std::string(renameBuffer).find_first_of(badChars) != std::string::npos;
					if (containsBadChar) {
						ImGui::TextColored(ImVec4(1, 0.4f, 0.4f, 1), "Your shot name cannot contain the characters / , \\ , | , * , ? , \" , < , or >");
					}
					if (ImGui::Button("OK") && !nameExists && renameIndex >= 0 && !(renameBuffer[0] == '\0') &&!containsBadChar && renameIndex<ShotData.size()) {
						ShotData[renameIndex].renameShot(renameBuffer);
						loadTrainingPack(TrainingPackNum);
						openRenamePopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::SameLine();
					if (ImGui::Button("Cancel")) {
						openRenamePopup = false;
						ImGui::CloseCurrentPopup();
					}

					ImGui::EndPopup();

				}

				ImGui::EndChild();
				if (ImGui::Button("Create New Shot"))
				{
					height = 0.0;
					length = 0.0;
					width = 0.0;
					speedX = 0.0;
					speedY = 0.0;
					speedZ = 0.0;
					carX = 0.0;
					carY = 0.0;
					carZ = 0.0;
					carYaw = 0;
					carPitch = 0;
					carRoll = 0;
					SSpeed = 3000;
					CVarWrapper speedBallCvar = cvarManager->getCvar("cool_speed");
					if (!speedBallCvar) { return; }
					speedBallCvar.setValue(3000);
					CVarWrapper speedEnableCvar = cvarManager->getCvar("speed_enabled");
					if (!speedEnableCvar) { return; }
					speedEnableCvar.setValue(2);
					shotName = "Insert Shot Name Here";
					strcpy(newShotName, shotName.c_str());
					randomizer = false;
					trainingActive = false;
					HeatEnabled = true;
					carEnabled = false;
					if (ShotData.size() >= 60) {
						openWarningPopup = true;
					}
					else {
						showSecondWindow = true;
					}
					openEditPopup = false;
					
				}

				ImGui::EndTabItem();

			}
			

			

		}//training tab
		if (ImGui::BeginTabItem("Training")) {
			ImGui::TextWrapped("Here You can pick which shots will show up in training.\n You can reset your shot by pressing the view or back button on the xbox controller. \n If you want to change this to a different button go into the settings tab.");
			CVarWrapper TrainingPackNumCvar = cvarManager->getCvar("TrainingPackNum");
			if (!TrainingPackNumCvar) { return; }
			int TrainingPackNum = TrainingPackNumCvar.getIntValue();

			if (!trainingPacks.empty() && TrainingPackNum >= 0 && TrainingPackNum < trainingPacks.size()) {
				ImGui::Text("Selected: %s", trainingPacks[TrainingPackNum].c_str());
			}
			if (ImGui::Checkbox("Activate Training", &trainingActive)) {

			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Toggle If Training is active or not");
			}
			ImGui::SameLine();
			CVarWrapper carCvar = cvarManager->getCvar("car_enabled");
			if (!carCvar) {
				return;
			}
			bool carEnabled1 = carCvar.getBoolValue();

			if (ImGui::Checkbox("Enable Car Reset", &carEnabled1)) {
				carCvar.setValue(carEnabled1);
			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Toggle Whether The Car Resets When A Shot Is Reset");
			}
			ImGui::SameLine();
			if (ImGui::Button("Select/Deselect All")) {
				if (HeatEnabled) {

					if (selectAll) {
						isShotActive.assign(ShotData.size(), false);
					}
					else {
						isShotActive.assign(ShotData.size(), true);
					}
					selectAll = !selectAll;
				}

			}

			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Selects all shots if clicked or deselects them if they are already all selected.");
			}

			

			char sh1[128];
			int flip = 0;

			for (int k = 0; k < ShotData.size(); k++) {
				strcpy(sh1, ShotData[k].getShotName().c_str());
				bool temp = isShotActive[k];
				ImGui::Checkbox(sh1, &temp);
				if (flip < shotCount / 10) {
					ImGui::SameLine();
					flip++;
				}
				else {
					flip = 0;
				}
				if (ImGui::IsItemHovered()) {
					ImGui::SetTooltip("Toggle This Shot");
				}
				isShotActive[k] = temp;
			}


			ImGui::EndTabItem();

		}//randomizer tab
		if (ImGui::BeginTabItem("Training Settings")) {
			ImGui::TextWrapped("Modifies Training Shots");

			if (ImGui::Checkbox("Activate modifiers", &randomizer)) {

			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Toggle If randomizer is active or not");
			}
			ImGui::SameLine();
			if (ImGui::Checkbox("Mirror Shots", &mirBall)) {

			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Toggle If mirroring your shot is active or not");
			}
			ImGui::SameLine();
			if (ImGui::Checkbox("Split Shot", &splitBall)) {

			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Toggles If mirroring the direction and the location of the shot is active or not");
			}
			ImGui::SameLine();
			if (ImGui::Checkbox("Mirror Car", &mirCar)) {

			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Toggle If mirroring your car is active or not");
			}
			ImGui::SameLine();
			if (ImGui::Checkbox("Split Car", &splitCar)) {

			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Toggles If mirroring the direction and the location of the car is active or not");
			}
			ImGui::SameLine();
			if (ImGui::Checkbox("Make Ceiling", &makeCeiling)) {

			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Toggle whether you will start your shot on the ceiling");
			}
			ImGui::SameLine();
			if (ImGui::Checkbox("Randomize spawns", &isRan)) {

			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Toggle whether it has a chance to spawn you on the celing.");
			}
			CVarWrapper carCvar = cvarManager->getCvar("car_enabled");
			if (!carCvar) {return;}
			bool carEnabled1 = carCvar.getBoolValue();
			ImGui::SameLine();
			if (ImGui::Checkbox("Enable Car Reset", &carEnabled1)) {
				carCvar.setValue(carEnabled1);
			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Toggle Whether The Car Resets When A Shot Is Reset");
			}
			
			//by players choice what value should reset the speed of the ball
			CVarWrapper speedEnableCvar = cvarManager->getCvar("speed_enabled");
			if (!speedEnableCvar) { return; }
			int speedEnable = speedEnableCvar.getIntValue();
			const char* listbox_items2[] = { "No Reset", "Reset from file", "Reset From Value Below" };
			if (ImGui::ListBox("", &speedEnable, listbox_items2, IM_ARRAYSIZE(listbox_items2), 3)) {
				speedEnableCvar.setValue(speedEnable);
			}
			//value of the speed of the ball
			CVarWrapper speedBallCvar = cvarManager->getCvar("cool_speed");
			if (!speedBallCvar) { return; }
			int speedBall = speedBallCvar.getIntValue();
			if (ImGui::SliderInt("Sets the Speed Of The Ball", &speedBall, 2985, 4600)) {
				speedBallCvar.setValue(speedBall);
			}
			if (ImGui::IsItemHovered()) {
				std::string hoverText = "The speed of the ball will be " + std::to_string(speedBall);
				ImGui::SetTooltip(hoverText.c_str());
			}

			ImGui::EndTabItem();

		}

		//settings tab
		if (ImGui::BeginTabItem("Settings")) {

			ImGui::Text("Current Controller Keybind: %s \nSelect Controller Keybind", controllerKeys[controllerKeybind].substr(10).c_str());



			// Prepare display names with substr(10)
			static std::vector<std::string> displayKeys;
			if (displayKeys.size() != controllerKeys.size())
			{
				displayKeys.clear();
				for (const auto& key : controllerKeys)
					displayKeys.push_back(key.substr(10)); // remove first 10 characters
			}


			// Searchable combo
			if (ImGui::SearchableCombo(
				"##controllerKeybind",               // invisible label
				&controllerKeybind,                // pointer to current selection index
				displayKeys,                     // vector of strings to display
				displayKeys[controllerKeybind].c_str(), // text shown on combo
				"Type to search...",             // hint text
				20                            // max items shown
			))
			{
				write(); // save the new selection
			}
			
			ImGui::Text("Current KeyBoard Keybind: %s \nSelect KeyBoard Keybind", KeyBoardKeys[KeyBoardKeybind].c_str());


			static std::vector<std::string> keyboardDisplayKeys;
			if (keyboardDisplayKeys.size() != KeyBoardKeys.size())
			{
				keyboardDisplayKeys.clear();
				for (const auto& key : KeyBoardKeys)
					keyboardDisplayKeys.push_back(key); // remove first 10 characters
			}
		
			// Searchable combo
			if (ImGui::SearchableCombo(
				"##keyboardKeybind",               // invisible label
				&KeyBoardKeybind,                // pointer to current selection index
				keyboardDisplayKeys,                     // vector of strings to display
				keyboardDisplayKeys[KeyBoardKeybind].c_str(), // text shown on combo
				"Type to search...",             // hint text
				20                            // max items shown
			))
			{
				write(); // save the new selection
			}
			ImGui::Text("Fixes the max seven goals in heatseeker bug thats been in the game forever. You can turn it off below.");
			if (ImGui::Checkbox("MaxScore = 100", &setMaxScore)) {

				if (gameWrapper->IsInOnlineGame()) {
				}
				else {
					ServerWrapper server = gameWrapper->GetCurrentGameState();
					if (server) {
						if (!setMaxScore) {
							//int yes = server.GetMaxScore();
							server.SetMaxScore(maxScore);
						}
					}
				}
			}
			if (ImGui::IsItemHovered()) {
				ImGui::SetTooltip("Toggle If max Score is set to 100 or not");
			}

			ImGui::EndTabItem();
		}
		//adds the change logs
		if (ImGui::BeginTabItem("Change Logs")) {

			ImGui::TextUnformatted("Version 5.0 \n-Made the UI a little better. \n-added Training Packs \n-shots are now made dynamically. \n-added a training modifiers section \n-fixed keybinds not always saving or being buggy \n-uncapped shot total but warnings are given at 60 \n-can now put the car on the celling or wall \n-fixed heatseeker max score being hardcapped at 7\n- minor fixes");



			ImGui::EndTabItem();
		}

		ImGui::EndTabBar();

		ImGui::End();
	}
	
	
	
	
}


// Name of the menu that is used to toggle the window.
std::string HSTraining::GetMenuName()
{
	
	return "HSTraining";
}

// Title to give the menu
std::string HSTraining::GetMenuTitle()
{
	return menuTitle_;
}

// Don't call this yourself, BM will call this function with a pointer to the current ImGui context
void HSTraining::SetImGuiContext(uintptr_t ctx)
{
	ImGui::SetCurrentContext(reinterpret_cast<ImGuiContext*>(ctx));
}

// Should events such as mouse clicks/key inputs be blocked so they won't reach the game
bool HSTraining::ShouldBlockInput()
{
	return ImGui::GetIO().WantCaptureMouse || ImGui::GetIO().WantCaptureKeyboard;
}

// Return true if window should be interactive
bool HSTraining::IsActiveOverlay()
{
	return true;
}

// Called when window is opened
void HSTraining::OnOpen()
{
	isWindowOpen_ = true;
	
}

// Called when window is closed
void HSTraining::OnClose()
{
	if (showSecondWindow) {

	}
	else {
		isWindowOpen_ = false;
	}
	
}

