#include "Velocity.h"
#include "Client.h"

#include "Utils/MoveUtils.h"

auto Velocity::onPacketReceive(Packet* _this, bool* cancel) -> void {
	auto clientinstance = Client::clientinstance;	
	if (!clientinstance) return;

	auto localplayer = clientinstance->getLocalPlayer();
	if (!localplayer) return;

	auto runtimeidcomponent = localplayer->getRuntimeIDComponent();
	if (!runtimeidcomponent) return;

	if (_this->getID() == PacketID::SetActorMotion) {
		auto setactormotion = reinterpret_cast<SetActorMotionPacket*>(_this);

		if (setactormotion->getRuntimeID().rawID == runtimeidcomponent->mRuntimeID.rawID) {
			if (jumpreset) {
				Utils::Move::setJumping(localplayer, true);
				Utils::Move::setJumping(localplayer, false);
			}

			if (horizontal == 0.f && vertical == 0.f) {
				*cancel = true;
				return;
			}

			glm::vec3 motion = setactormotion->getMotion();
			motion.x *= horizontal;
			motion.y *= vertical;
			motion.z *= horizontal;
			setactormotion->setMotion(motion);
		}
	}
}

auto Velocity::getMode() -> std::string {
	if (horizontal == 1 && vertical == 1) return _("Standard");

	return std::to_string(static_cast<int>(horizontal * 100)) + _("% ") + std::to_string(static_cast<int>(vertical * 100)) + _("%");
}
