#include "SessionInfo.h"
#include "Utils/ImGuiRenderUtils.h"
#include "Utils/MathUtils.h"
#include "Utils/ColorUtils.h"

#include "Interface.h"

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

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

	switch (style) {
	case 0: {
		ImVec4 rect = ImVec4(pos.x - 120.f, pos.y - 50.f, pos.x + 140.f, pos.y + 50.f);
		Utils::ImGuiRender::setDrawList(ImGui::GetBackgroundDrawList());
		Utils::ImGuiRender::setColor(ImColor(0.f, 0.f, 0.f, 0.5f));
		Utils::ImGuiRender::setRound(20.f);

		if (blurs != 0.f) {
			Utils::ImGuiRender::setBlurStreangth(blurs);
			Utils::ImGuiRender::fillBlurRectangle(rect);
		}


		Utils::ImGuiRender::fillRectangle(rect);

		{
			Utils::ImGuiRender::setSize(50.f);
			Utils::ImGuiRender::setThickness(100.f);
			Utils::ImGuiRender::fillShadowRectangle(rect);
		}

		Utils::ImGuiRender::setColor(Utils::Color::getColor(3.f, 0, 1.f));
		if (outline) {
			Utils::ImGuiRender::setThickness(0.5f);
			Utils::ImGuiRender::drawRectangle(rect);
		}
		Utils::ImGuiRender::setFont(ImGui::GetIO().Fonts->Fonts[0 + (4 * interfaceMod->font)]);
		Utils::ImGuiRender::setSize(1.4f);
		ImVec2 size = Utils::ImGuiRender::getTextSize(_("Session Stats"));
		ImVec2 pos = ImVec2(rect.x + (((rect.z - rect.x) - size.x) / 2) + 5.f, rect.y + 5.f);

		Utils::ImGuiRender::drawText(_("Session Stats"), pos);

		std::string times = _("");
		int seconds = 0, minutes = 0, hours = 0;

		seconds = Client::playtime % 60;
		minutes = (Client::playtime / 60) % 60;
		hours = Client::playtime / 3600;

		if (seconds != 0)
			times = std::to_string(seconds) + _(" seconds");

		if (minutes != 0)
			times = std::to_string(minutes) + _(" minutes ") + times;

		if (hours != 0)
			times = std::to_string(hours) + _(" hours ") + times;

		Utils::ImGuiRender::setSize(1.1f);
		ImVec2 timesize = Utils::ImGuiRender::getTextSize(times);


		ImVec2 timepos = ImVec2(
			rect.x + (((rect.z - rect.x) - timesize.x) / 2) + 5.f,
			rect.y + 5.f + (size.y + 7.5f)
		);


		Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));
		Utils::ImGuiRender::drawText(times, timepos);



		std::string infos = "";

		int kills = Client::kills;
		int wins = Client::wins;
		std::string spacing = _("    ");
		infos = _("Kills: ") + std::to_string(kills) + spacing + _("Wins: ") + std::to_string(wins);
		ImVec2 infosize = Utils::ImGuiRender::getTextSize(infos);


		ImVec2 infopos = ImVec2(
			rect.x + (((rect.z - rect.x) - infosize.x) / 2) + 5.f,
			rect.y + 5.f + ((size.y + 7.5f) + (timesize.y + 2.5f))
		);


		Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));
		Utils::ImGuiRender::drawText(infos, infopos);

		break;
	}
	case 1: {
		ImVec4 rect = ImVec4(pos.x - 120.f, pos.y - 50.f, pos.x + 120.f, pos.y + 50.f);
		Utils::ImGuiRender::setDrawList(ImGui::GetBackgroundDrawList());
		Utils::ImGuiRender::setColor(ImColor(0.f, 0.f, 0.f, 0.5f));
		Utils::ImGuiRender::setRound(20.f);

		if (blurs != 0.f) {
			Utils::ImGuiRender::setBlurStreangth(blurs);
			Utils::ImGuiRender::fillBlurRectangle(rect);
		}

		Utils::ImGuiRender::fillRectangle(rect);

		{
			Utils::ImGuiRender::setSize(50.f);
			Utils::ImGuiRender::setThickness(100.f);
			Utils::ImGuiRender::fillShadowRectangle(rect);
		}

		Utils::ImGuiRender::setFont(ImGui::GetIO().Fonts->Fonts[0 + (4 * interfaceMod->font)]);
		Utils::ImGuiRender::setSize(1.2f);
		ImVec2 size = Utils::ImGuiRender::getTextSize(_("Statistics"));
		ImVec2 pos = ImVec2(rect.x + (((rect.z - rect.x) - size.x) / 2), rect.y + 5.f);

		Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));
		Utils::ImGuiRender::drawText(_("Statistics"), pos);

		ImVec4 barrect = ImVec4(rect.x + 10.f, rect.y + (10.f + size.y), rect.z - 10.f, rect.y + (13.f + size.y));
		Utils::ImGuiRender::setColor(Utils::Color::getColor(4.f, 0, 1.f));
		Utils::ImGuiRender::setRound(0.f);
		Utils::ImGuiRender::setSize(50.f);
		Utils::ImGuiRender::setThickness(100.f);
		Utils::ImGuiRender::fillShadowRectangle(barrect);
		Utils::ImGuiRender::setSecondColor(Utils::Color::getColor(4.f, 40, 1.f));
		Utils::ImGuiRender::fillGradientOpaqueRectangle(barrect);

		Utils::ImGuiRender::setSize(1.1f);

		ImVec2 playtimepos = ImVec2(barrect.x, barrect.w + 2.f);

		Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));

		ImVec4 playtimerect = ImVec4(rect.x, playtimepos.y, rect.z, playtimepos.y + size.y);
		Utils::ImGuiRender::pushClip(playtimerect);
		Utils::ImGuiRender::drawText(_("Play Time"), playtimepos);

		static int seconds = 0, minutes = 0, hours = 0;

		seconds = Client::playtime % 60;
		minutes = (Client::playtime / 60) % 60;
		hours = Client::playtime / 3600;

		float allnumssize = 0.f;
		for (int i = 0; i <= 9; i++) {
			allnumssize += Utils::ImGuiRender::getTextSize(std::to_string(i)).y;
		}
		float allnumssize_ = 0.f;
		for (int i = 0; i <= 6; i++) {
			allnumssize_ += Utils::ImGuiRender::getTextSize(std::to_string(i)).y;
		}

		int seconds1 = seconds % 10;
		int seconds2 = (seconds / 10) % 10;

		int minutes1 = minutes % 10;
		int minutes2 = (minutes / 10) % 10;

		int hours1 = hours % 10;
		int hours2 = (hours / 10) % 10;

		float secondssize = Utils::ImGuiRender::getTextSize(std::to_string(seconds1)).y;
		float minutessize = Utils::ImGuiRender::getTextSize(std::to_string(minutes1)).y;
		float hourssize = Utils::ImGuiRender::getTextSize(std::to_string(hours1)).y;

		float secondssize_ = Utils::ImGuiRender::getTextSize(std::to_string(seconds2)).x;
		float minutessize_ = Utils::ImGuiRender::getTextSize(std::to_string(minutes2)).x;
		float hourssize_ = Utils::ImGuiRender::getTextSize(std::to_string(hours2)).x;

		float allnumsize2 = 0.f;
		for (int i = 0; i <= seconds1; i++) {
			allnumsize2 += Utils::ImGuiRender::getTextSize(std::to_string(i)).y;
		}
		float allnumsize3 = 0.f;
		for (int i = 0; i <= seconds2; i++) {
			allnumsize3 += Utils::ImGuiRender::getTextSize(std::to_string(i)).y;
		}
		float allnumsize4 = 0.f;
		for (int i = 0; i <= minutes1; i++) {
			allnumsize4 += Utils::ImGuiRender::getTextSize(std::to_string(i)).y;
		}
		float allnumsize5 = 0.f;
		for (int i = 0; i <= minutes2; i++) {
			allnumsize5 += Utils::ImGuiRender::getTextSize(std::to_string(i)).y;
		}
		float allnumsize6 = 0.f;
		for (int i = 0; i <= hours1; i++) {
			allnumsize6 += Utils::ImGuiRender::getTextSize(std::to_string(i)).y;
		}
		float allnumsize7 = 0.f;
		for (int i = 0; i <= hours2; i++) {
			allnumsize7 += Utils::ImGuiRender::getTextSize(std::to_string(i)).y;
		}

		static float anim1 = 0.f;
		anim1 = Utils::Math::animate(allnumsize2, anim1, Client::deltaTime * 5.f);
		static float anim2 = 0.f;
		anim2 = Utils::Math::animate(allnumsize3, anim2, Client::deltaTime * 5.f);
		static float anim3 = 0.f;
		anim3 = Utils::Math::animate(allnumsize4, anim3, Client::deltaTime * 5.f);
		static float anim4 = 0.f;
		anim4 = Utils::Math::animate(allnumsize5, anim4, Client::deltaTime * 5.f);
		static float anim5 = 0.f;
		anim5 = Utils::Math::animate(allnumsize6, anim5, Client::deltaTime * 5.f);
		static float anim6 = 0.f;
		anim6 = Utils::Math::animate(allnumsize7, anim6, Client::deltaTime * 5.f);

		ImVec2 secondspos = ImVec2(barrect.z - (10.f + Utils::ImGuiRender::getTextSize(_("s")).x), (playtimepos.y - allnumssize) + anim1);
		ImVec2 secondspos_ = ImVec2(barrect.z - (10.f + Utils::ImGuiRender::getTextSize(_("s")).x), (playtimepos.y - allnumssize_) + anim2);
		ImVec2 minutespos = ImVec2(barrect.z - (10.f + Utils::ImGuiRender::getTextSize(std::to_string(seconds)).x + Utils::ImGuiRender::getTextSize(_("m")).x), (playtimepos.y - allnumssize) + anim3);
		ImVec2 minutespos_ = ImVec2(barrect.z - (10.f + Utils::ImGuiRender::getTextSize(std::to_string(seconds)).x + Utils::ImGuiRender::getTextSize(_("m")).x), (playtimepos.y - allnumssize_) + anim4);
		ImVec2 hourspos = ImVec2(barrect.z - (10.f + Utils::ImGuiRender::getTextSize(std::to_string(seconds)).x + Utils::ImGuiRender::getTextSize(std::to_string(seconds)).x + Utils::ImGuiRender::getTextSize(_("m")).x + Utils::ImGuiRender::getTextSize(_("s")).x), (playtimepos.y - allnumssize) + anim5);
		ImVec2 hourspos_ = ImVec2(barrect.z - (10.f + Utils::ImGuiRender::getTextSize(std::to_string(seconds)).x + Utils::ImGuiRender::getTextSize(std::to_string(seconds)).x + Utils::ImGuiRender::getTextSize(_("m")).x + Utils::ImGuiRender::getTextSize(_("s")).x), (playtimepos.y - allnumssize_) + anim6);

		ImVec2 secondspos2 = ImVec2(barrect.z - 10.f, playtimepos.y);
		ImVec2 minutespos2 = ImVec2(barrect.z - (20.f + (Utils::ImGuiRender::getTextSize(std::to_string(seconds) + _("s")).x)), playtimepos.y);
		ImVec2 hourspos2 = ImVec2(barrect.z - (20.f + (Utils::ImGuiRender::getTextSize(std::to_string(minutes) + _("m") + std::to_string(seconds) + _("s")).x)), playtimepos.y);

		for (int i = 9; i >= 0; i--) {
			Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));
			Utils::ImGuiRender::drawText(std::to_string(i), secondspos);

			secondspos.y += secondssize;
		}

		secondspos_.x -= secondssize_;

		if (seconds2 != 0) {
			for (int i = 6; i >= 0; i--) {
				Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));
				Utils::ImGuiRender::drawText(std::to_string(i), secondspos_);

				secondspos_.y += secondssize;
			}
		}

		minutespos.x -= (secondssize_ + 5.f);

		if (minutes != 0) {
			for (int i = 9; i >= 0; i--) {
				Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));
				Utils::ImGuiRender::drawText(std::to_string(i), minutespos);

				minutespos.y += minutessize;
			}

			if (minutes2 != 0) {
				minutespos_.x -= (minutessize + 5.f);

				for (int i = 6; i >= 0; i--) {
					Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));
					Utils::ImGuiRender::drawText(std::to_string(i), minutespos_);

					minutespos_.y += minutessize;
				}
			}

			minutespos2.x += secondssize_ - 5.f;
			Utils::ImGuiRender::drawText(_("m"), minutespos2);
		}

		hourspos.x -= hourssize_;

		if (hours != 0) {
			for (int i = 9; i >= 0; i--) {
				Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));
				Utils::ImGuiRender::drawText(std::to_string(i), hourspos);

				hourspos.y += hourssize;
			}

			hourspos_.x -= hourssize_ * 2;
			if (hours2 != 0) {

				for (int i = 6; i >= 0; i--) {
					Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));
					Utils::ImGuiRender::drawText(std::to_string(i), hourspos_);

					hourspos_.y += hourssize;
				}
			}

			Utils::ImGuiRender::drawText(_("h"), hourspos2);
		}

		Utils::ImGuiRender::drawText(_("s"), secondspos2);
		Utils::ImGuiRender::popClip();



		ImVec2 _killspos = ImVec2(barrect.x, barrect.w + 1.f + size.y);
		ImVec4 killsrect = ImVec4(rect.x, _killspos.y, rect.z, _killspos.y + size.y);
		Utils::ImGuiRender::pushClip(killsrect);
		Utils::ImGuiRender::drawText(_("Kills"), _killspos);

		int kills = Client::kills;

		float allnumssize2 = 0.f;
		for (int i = 0; i <= 9; i++) {
			allnumssize2 += Utils::ImGuiRender::getTextSize(std::to_string(i)).y;
		}

		int kills1 = kills % 10;
		int kills2 = (kills / 10) % 10;

		float killssize = Utils::ImGuiRender::getTextSize(std::to_string(kills1)).y;

		float killssize_ = Utils::ImGuiRender::getTextSize(std::to_string(kills2)).x;

		float allnumsize2_ = 0.f;
		for (int i = 0; i <= kills1; i++) {
			allnumsize2_ += Utils::ImGuiRender::getTextSize(std::to_string(i)).y;
		}
		float allnumsize3_ = 0.f;
		for (int i = 0; i <= kills2; i++) {
			allnumsize3_ += Utils::ImGuiRender::getTextSize(std::to_string(i)).y;
		}

		static float anim1_ = 0.f;
		anim1_ = Utils::Math::animate(allnumsize2_, anim1_, Client::deltaTime * 5.f);
		static float anim2_ = 0.f;
		anim2_ = Utils::Math::animate(allnumsize3_, anim2_, Client::deltaTime * 5.f);

		ImVec2 killspos = ImVec2(barrect.z - 10.f, (_killspos.y - allnumssize2) + anim1_);
		ImVec2 killspos_ = ImVec2(barrect.z - 10.f, (_killspos.y - allnumssize2) + anim2_);

		ImVec2 killspos2 = ImVec2(barrect.z - 10.f, playtimepos.y);

		for (int i = 9; i >= 0; i--) {
			Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));
			Utils::ImGuiRender::drawText(std::to_string(i), killspos);

			killspos.y += killssize;
		}

		killspos_.x -= killssize_;

		if (kills2 != 0) {
			for (int i = 9; i >= 0; i--) {
				Utils::ImGuiRender::setColor(ImColor(1.f, 1.f, 1.f, 1.f));
				Utils::ImGuiRender::drawText(std::to_string(i), killspos_);

				killspos_.y += killssize;
			}
		}

		Utils::ImGuiRender::popClip();
		break;
	}
	}
}