#include "Keystrokes.h"
#include "Utils/ImGuiRenderUtils.h"
#include "Utils/FileUtils.h"
#include "Utils/ColorUtils.h"
#include "Utils/MathUtils.h"

#include "Mod/Interface.h"

auto Keystrokes::onKeyboard(int key, bool* held, bool* cancel) -> void {
	if (key == Client::optforward) forwardheld = *held;
	if (key == Client::optback) backheld = *held;
	if (key == Client::optleft) leftheld = *held;
	if (key == Client::optright) rightheld = *held;
	if (key == Client::optjump) jumpheld = *held;
}

auto Keystrokes::onImGuiRender() -> void {
	if (Client::screenName != _("hud_screen") && Client::screenName != _("chat_screen")) return;

	Interface* interfaceMod = Client::findMod<Interface>(_("Interface"));

	static float forwardanim = 0.f, forwardanim2 = 0.f, backanim = 0.f, backanim2 = 0.f, leftanim = 0.f, leftanim2 = 0.f, rightanim = 0.f, rightanim2 = 0.f, spaceanim = 0.f, spaceanim2 = 0.f;
	forwardanim = Utils::Math::animate(forwardheld ? 0.f : 1.f, forwardanim, Client::deltaTime * 10.f);
	forwardanim2 = Utils::Math::animate(forwardheld ? 1.f : 0.f, forwardanim2, Client::deltaTime * 10.f);

	backanim = Utils::Math::animate(backheld ? 0.f : 1.f, backanim, Client::deltaTime * 10.f);
	backanim2 = Utils::Math::animate(backheld ? 1.f : 0.f, backanim2, Client::deltaTime * 10.f);

	leftanim = Utils::Math::animate(leftheld ? 0.f : 1.f, leftanim, Client::deltaTime * 10.f);
	leftanim2 = Utils::Math::animate(leftheld ? 1.f : 0.f, leftanim2, Client::deltaTime * 10.f);

	rightanim = Utils::Math::animate(rightheld ? 0.f : 1.f, rightanim, Client::deltaTime * 10.f);
	rightanim2 = Utils::Math::animate(rightheld ? 1.f : 0.f, rightanim2, Client::deltaTime * 10.f);

	spaceanim = Utils::Math::animate(jumpheld ? 0.f : 1.f, spaceanim, Client::deltaTime * 10.f);
	spaceanim2 = Utils::Math::animate(jumpheld ? 1.f : 0.f, spaceanim2, Client::deltaTime * 10.f);


	ImVec4 backrect = ImVec4(pos.x - 25.f, pos.y - 25.f, pos.x + 25.f, pos.y + 25.f);
	ImVec4 leftrect = ImVec4(backrect.x - (50.f + 5.f), backrect.y, backrect.x - 5.f, backrect.w);
	ImVec4 rightrect = ImVec4(backrect.z + 5.f, backrect.y, backrect.z + (50.f + 5.f), backrect.w);
	ImVec4 forwardrect = ImVec4(backrect.x, backrect.y - (50.f + 5.f), backrect.z, backrect.y - 5.f);
	ImVec4 spacerect = ImVec4(backrect.x - (50.f + 5.f), backrect.w + 5.f, backrect.z + (50.f + 5.f), backrect.w + (30.f + 5.f));
	
	ImVec4 forwardrect_ = ImVec4(forwardrect.x + (25.f * forwardanim), forwardrect.y + (25.f * forwardanim), forwardrect.z - (25.f * forwardanim), forwardrect.w - (25.f * forwardanim));
	ImVec4 backrect_ = ImVec4(backrect.x + (25.f * backanim), backrect.y + (25.f * backanim), backrect.z - (25.f * backanim), backrect.w - (25.f * backanim));
	ImVec4 leftrect_ = ImVec4(leftrect.x + (25.f * leftanim), leftrect.y + (25.f * leftanim), leftrect.z - (25.f * leftanim), leftrect.w - (25.f * leftanim));
	ImVec4 rightrect_ = ImVec4(rightrect.x + (25.f * rightanim), rightrect.y + (25.f * rightanim), rightrect.z - (25.f * rightanim), rightrect.w - (25.f * rightanim));
	ImVec4 spacerect_ = ImVec4(spacerect.x + (50.f * spaceanim), spacerect.y + (15.f * spaceanim), spacerect.z - (50.f * spaceanim), spacerect.w - (15.f * spaceanim));

	Utils::ImGuiRender::setDrawList(ImGui::GetBackgroundDrawList());

	Utils::ImGuiRender::setColor(ImColor(0.f, 0.f, 0.f, 0.6f));
	Utils::ImGuiRender::setRound(10.f);
	Utils::ImGuiRender::setSize(20.f);
	Utils::ImGuiRender::setThickness(100.f);
	Utils::ImGuiRender::setBlurStreangth(blurs);

	Utils::ImGuiRender::fillBlurRectangle(backrect);
	Utils::ImGuiRender::fillRectangle(backrect);
	Utils::ImGuiRender::fillShadowRectangle(backrect);

	Utils::ImGuiRender::fillBlurRectangle(forwardrect);
	Utils::ImGuiRender::fillRectangle(forwardrect);
	Utils::ImGuiRender::fillShadowRectangle(forwardrect);
	
	Utils::ImGuiRender::fillBlurRectangle(rightrect);
	Utils::ImGuiRender::fillRectangle(rightrect);
	Utils::ImGuiRender::fillShadowRectangle(rightrect);
	
	Utils::ImGuiRender::fillBlurRectangle(leftrect);
	Utils::ImGuiRender::fillRectangle(leftrect);
	Utils::ImGuiRender::fillShadowRectangle(leftrect);
	
	Utils::ImGuiRender::fillBlurRectangle(spacerect);
	Utils::ImGuiRender::fillRectangle(spacerect);
	Utils::ImGuiRender::fillShadowRectangle(spacerect);

	Utils::ImGuiRender::setColor(ImColor(0.8f, 0.8f, 0.8f, 0.6f * forwardanim2));
	Utils::ImGuiRender::fillRectangle(forwardrect_);

	Utils::ImGuiRender::setColor(ImColor(0.8f, 0.8f, 0.8f, 0.6f * backanim2));
	Utils::ImGuiRender::fillRectangle(backrect_);

	Utils::ImGuiRender::setColor(ImColor(0.8f, 0.8f, 0.8f, 0.6f * leftanim2));
	Utils::ImGuiRender::fillRectangle(leftrect_);

	Utils::ImGuiRender::setColor(ImColor(0.8f, 0.8f, 0.8f, 0.6f * rightanim2));
	Utils::ImGuiRender::fillRectangle(rightrect_);

	Utils::ImGuiRender::setColor(ImColor(0.8f, 0.8f, 0.8f, 0.6f * spaceanim2));
	Utils::ImGuiRender::fillRectangle(spacerect_);



	Utils::ImGuiRender::setFont(ImGui::GetIO().Fonts->Fonts[0 + (4 * interfaceMod->font)]);
	Utils::ImGuiRender::setSize(1.6f);
	ImVec2 backpos = ImVec2(
		backrect.x + ((((backrect.z - backrect.x) - Utils::ImGuiRender::getTextSize(std::string(1, Client::optback)).x)) / 2),
		backrect.y + ((((backrect.w - backrect.y) - Utils::ImGuiRender::getTextSize(std::string(1, Client::optback)).y)) / 2)
	);
	ImVec2 leftpos = ImVec2(
		leftrect.x + ((((leftrect.z - leftrect.x) - Utils::ImGuiRender::getTextSize(std::string(1, Client::optleft)).x)) / 2),
		leftrect.y + ((((leftrect.w - leftrect.y) - Utils::ImGuiRender::getTextSize(std::string(1, Client::optleft)).y)) / 2)
	);
	ImVec2 rightpos = ImVec2(
		rightrect.x + ((((rightrect.z - rightrect.x) - Utils::ImGuiRender::getTextSize(std::string(1, Client::optright)).x)) / 2),
		rightrect.y + ((((rightrect.w - rightrect.y) - Utils::ImGuiRender::getTextSize(std::string(1, Client::optright)).y)) / 2)
	);
	ImVec2 forwardpos = ImVec2(
		forwardrect.x + ((((forwardrect.z - forwardrect.x) - Utils::ImGuiRender::getTextSize(std::string(1, Client::optforward)).x)) / 2),
		forwardrect.y + ((((forwardrect.w - forwardrect.y) - Utils::ImGuiRender::getTextSize(std::string(1, Client::optforward)).y)) / 2)
	);
	ImVec4 spacepos = ImVec4(
		spacerect.x + 35.f,
		spacerect.y + 13.5f,
		spacerect.z - 35.f,
		spacerect.w - 13.5f
	);


	Utils::ImGuiRender::setColor(Utils::Color::getColor(4.f, 80, 1.f));
	Utils::ImGuiRender::drawText(std::string(1, Client::optback), backpos);

	Utils::ImGuiRender::setColor(Utils::Color::getColor(4.f, 40, 1.f));
	Utils::ImGuiRender::drawText(std::string(1, Client::optleft), leftpos);

	Utils::ImGuiRender::setColor(Utils::Color::getColor(4.f, 120, 1.f));
	Utils::ImGuiRender::drawText(std::string(1, Client::optright), rightpos);

	Utils::ImGuiRender::setColor(Utils::Color::getColor(4.f, 0, 1.f));
	Utils::ImGuiRender::drawText(std::string(1, Client::optforward), forwardpos);

	Utils::ImGuiRender::setRound(5.f);
	Utils::ImGuiRender::setColor(Utils::Color::getColor(4.f, 160, 1.f));
	Utils::ImGuiRender::fillRectangle(spacepos);
}
