#include "FastBreak.h"
#include "Client.h"

#include "Mod/Regen.h"

auto FastBreak::onClient() -> void {

	for (Setting* setting : Client::findSetting(this, _("Delay"))) {
		setting->visible = mode == 0;
	}
	for (Setting* setting : Client::findSetting(this, _("Percentage"))) {
		setting->visible = mode == 1;
	}
}

auto FastBreak::onImGuiRender() -> void {
	if (ImGui::IsMouseDown(0)) {
		stopdestroy = false;
	}
	else {
		stopdestroy = true;
	}
}

auto FastBreak::onWorldTick(Player* _this) -> void {
	auto clientinstance = Client::clientinstance;
	if (!clientinstance) return;

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

	auto gamemode = localplayer->getGameMode();
	if (!gamemode) return;

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

	auto hitresult = level->getHitResult();
	
	auto regenMod = Client::findMod<Regen>(_("Regen"));

	//if (regenMod->enabled && regenMod->dorot) return;
	if (regenMod->enabled && regenMod->destroying) return;

	if (stopdestroy) return;

	switch (mode) {
	case 0:
		if (gamemode->getDestroyProgress() > 0.01f) {
			delay_++;
			if (delay_ > delay) {
				gamemode->destroyBlock(hitresult.mBlock, hitresult.mFacing);
				delay_ = 0;
			}
		}
		break;
	case 1:
		if (gamemode->getDestroyProgress() > percentage) {
			gamemode->destroyBlock(hitresult.mBlock, hitresult.mFacing);
		}
		break;
	}
}

auto FastBreak::getMode() -> std::string {
	switch (mode) {
	case 0:
		return _("Delay");
	case 1:
		return _("Percentage");
	}

	return _("Unknown");
}
