#include "NoRender.h"

#include "Client.h"

auto NoRender::onBobHurt(glm::mat4* mat, bool* cancel) -> void {
    if (hurtcam && enabled) 
        *cancel = true;
}

auto NoRender::onClient() -> void {
	if (hurtcam && enabled) {
        if (!patched) {
            if (Utils::File::getVersion() == _("1.21.73")) {
                DWORD oldProtect;
                VirtualProtect((LPVOID)cameraassignangle_, 1, PAGE_EXECUTE_READWRITE, &oldProtect);
                memcpy((LPVOID)cameraassignangle_, _("\xEB\x90\x90\x90\x90"), 1);
                VirtualProtect((LPVOID)cameraassignangle_, 1, oldProtect, &oldProtect);
            }
            else if (Utils::File::getVersion() == _("1.21.72")) {
                DWORD oldProtect;
                VirtualProtect((LPVOID)cameraassignangle_, 1, PAGE_EXECUTE_READWRITE, &oldProtect);
                memcpy((LPVOID)cameraassignangle_, _("\xEB\x90\x90\x90\x90"), 1);
                VirtualProtect((LPVOID)cameraassignangle_, 1, oldProtect, &oldProtect);
            }
            else if (Utils::File::getVersion() == _("1.21.71")) {
                DWORD oldProtect;
                VirtualProtect((LPVOID)cameraassignangle_, 1, PAGE_EXECUTE_READWRITE, &oldProtect);
                memcpy((LPVOID)cameraassignangle_, _("\xEB\x90\x90\x90\x90"), 1);
                VirtualProtect((LPVOID)cameraassignangle_, 1, oldProtect, &oldProtect);
            }
            else if (Utils::File::getVersion() == _("1.21.70")) {
                DWORD oldProtect;
                VirtualProtect((LPVOID)cameraassignangle_, 1, PAGE_EXECUTE_READWRITE, &oldProtect);
                memcpy((LPVOID)cameraassignangle_, _("\xEB\x90\x90\x90\x90"), 1);
                VirtualProtect((LPVOID)cameraassignangle_, 1, oldProtect, &oldProtect);
            }
            else {
                DWORD oldProtect;
                VirtualProtect((LPVOID)cameraassignangle_, 5, PAGE_EXECUTE_READWRITE, &oldProtect);
                memcpy((LPVOID)cameraassignangle_, _("\x90\x90\x90\x90\x90"), 5);
                VirtualProtect((LPVOID)cameraassignangle_, 5, oldProtect, &oldProtect);
            }

            patched = true;
        }
	}
	else {
        if (patched) {
            if (Utils::File::getVersion() == _("1.21.73")) {
                DWORD oldProtect;
                VirtualProtect((LPVOID)cameraassignangle_, 1, PAGE_EXECUTE_READWRITE, &oldProtect);
                memcpy((LPVOID)cameraassignangle_, ogBytes, 1);
                VirtualProtect((LPVOID)cameraassignangle_, 1, oldProtect, &oldProtect);
            }
            else if (Utils::File::getVersion() == _("1.21.72")) {
                DWORD oldProtect;
                VirtualProtect((LPVOID)cameraassignangle_, 1, PAGE_EXECUTE_READWRITE, &oldProtect);
                memcpy((LPVOID)cameraassignangle_, ogBytes, 1);
                VirtualProtect((LPVOID)cameraassignangle_, 1, oldProtect, &oldProtect);
            }
            else if (Utils::File::getVersion() == _("1.21.71")) {
                DWORD oldProtect;
                VirtualProtect((LPVOID)cameraassignangle_, 1, PAGE_EXECUTE_READWRITE, &oldProtect);
                memcpy((LPVOID)cameraassignangle_, ogBytes, 1);
                VirtualProtect((LPVOID)cameraassignangle_, 1, oldProtect, &oldProtect);
            }
            else if (Utils::File::getVersion() == _("1.21.70")) {
                DWORD oldProtect;
                VirtualProtect((LPVOID)cameraassignangle_, 1, PAGE_EXECUTE_READWRITE, &oldProtect);
                memcpy((LPVOID)cameraassignangle_, ogBytes, 1);
                VirtualProtect((LPVOID)cameraassignangle_, 1, oldProtect, &oldProtect);
            }
            else {
                DWORD oldProtect;
                VirtualProtect((LPVOID)cameraassignangle_, 5, PAGE_EXECUTE_READWRITE, &oldProtect);
                memcpy((LPVOID)cameraassignangle_, ogBytes, 5);
                VirtualProtect((LPVOID)cameraassignangle_, 5, oldProtect, &oldProtect);
            }
            patched = false;
		}
	}
}

auto NoRender::onGetShadowRadius(Player* _this, float* radius) -> void {
    if (shadows && enabled)
        *radius = 0.f;
}

auto NoRender::onFire(Player* _this, bool* isonfire) -> void {
    auto clientinstance = Client::clientinstance;
    if (!clientinstance) return;

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

    if (onfire && enabled && _this == localplayer)
        *isonfire = false;
}

auto NoRender::onPacketReceive(Packet* packet, bool* cancel) -> void {
    if (oneffect && enabled) {
        if (packet->getID() == PacketID::MobEffect) {
            auto mobeffect = reinterpret_cast<MobEffectPacket*>(packet);

            int effectid = mobeffect->getEffectID();

            *cancel = true;
            /*
            if (effectid == 9 || 
                effectid == 15 ||
                effectid == 20 ||
                effectid == 30) 
                *cancel = true;*/
        }
    }
}

auto NoRender::onDisabled() -> void {
    if (patched) {
        DWORD oldProtect;
        VirtualProtect((LPVOID)cameraassignangle_, 5, PAGE_EXECUTE_READWRITE, &oldProtect);
        memcpy((LPVOID)cameraassignangle_, ogBytes, 5);
        VirtualProtect((LPVOID)cameraassignangle_, 5, oldProtect, &oldProtect);
        patched = false;
    }
}
