#include "MCP.h"
#include "Client.h"

#include "Utils/ChatUtils.h"
#include "Utils/TimeUtils.h"

auto MCP::onMouse(char button, char action, bool* cancel) -> void {
	if (static_cast<int>(action) == 1 && button == 3) {
		use = true;
	}
}

auto MCP::onWorldTick(Player* _this) -> void {
	auto gamemode = _this->getGameMode();
	if (!gamemode) return;

	auto playerinventory = _this->getPlayerInventory();
	if (!playerinventory) return;

	auto inventory = playerinventory->getInventory();
	if (!inventory) return;

	auto level = _this->getLevel();
	if (!level) return;

	auto hitresult = level->getHitResult();
	

	long long now = Utils::Time::getMs();
	static long long start = now;

	if (use) {
		auto entity = hitresult.mEntity;
		if (hitresult.mType == HitResultType::Entity) {
			use = false;
			return;
		}

		for (int i = 0; i < 9; i++) {
			auto itemstack = inventory->getItemStack(i);
			if (itemstack->getItem()) {
				if ((*itemstack->getItem())->getName().find(_("ender_pearl")) != std::string::npos) {
					int oldslot = playerinventory->getSelectedSlot();
					playerinventory->setSelectedSlot(i);
					gamemode->baseuseitem(*itemstack);
					playerinventory->setSelectedSlot(oldslot);
					use = false;
					return;
				}
			}
		}

		Utils::Chat::display(_("No enderpearl!"), Utils::Chat::Type::RED);
		use = false;

	}
}
