/*
 * drawmgr.cpp
 *
 *  Created on: May 22, 2017
 *      Author: nullifiedcat
 */

#include <MiscTemporary.hpp>
#include <hacks/Aimbot.hpp>
#include <hacks/hacklist.hpp>
#if ENABLE_IMGUI_DRAWING
#include "imgui/imrenderer.hpp"
#elif !ENABLE_ENGINE_DRAWING
#include <glez/glez.hpp>
#include <glez/record.hpp>
#include <glez/draw.hpp>
#endif
#include <settings/Bool.hpp>
#include <settings/Float.hpp>
#include <menu/GuiInterface.hpp>
#include "common.hpp"
#include "visual/drawing.hpp"
#include "hack.hpp"
#include "menu/menu/Menu.hpp"

static settings::Bool info_text{ "hack-info.enable", "true" };
static settings::Bool info_text_min{ "hack-info.minimal", "false" };

void render_cheat_visuals()
{
    {
        PROF_SECTION(BeginCheatVisuals);
        BeginCheatVisuals();
    }
    {
        PROF_SECTION(DrawCheatVisuals);
        DrawCheatVisuals();
    }
    {
        PROF_SECTION(EndCheatVisuals);
        EndCheatVisuals();
    }
}
#if !ENABLE_ENGINE_DRAWING && !ENABLE_IMGUI_DRAWING
glez::record::Record bufferA{};
glez::record::Record bufferB{};

glez::record::Record *buffers[] = { &bufferA, &bufferB };
#endif
int currentBuffer = 0;

void BeginCheatVisuals()
{
#if ENABLE_IMGUI_DRAWING
    im_renderer::bufferBegin();
#elif !ENABLE_ENGINE_DRAWING
    buffers[currentBuffer]->begin();
#endif
    ResetStrings();
}

std::mutex drawing_mutex;

double getRandom(double lower_bound, double upper_bound)
{
    std::uniform_real_distribution<double> unif(lower_bound, upper_bound);
    static std::mt19937 rand_engine(std::time(nullptr));

    double x = unif(rand_engine);
    return x;
}

void DrawCheatVisuals()
{
    {
        PROF_SECTION(DRAW_info);
        std::string name_s, reason_s;
        PROF_SECTION(PT_info_text);
        if (info_text)
        {
            auto color = colors::RainbowCurrent();
            color.a    = 1.0f;
            AddSideString("cathook by nullworks", color);
            if (!info_text_min)
            {
                AddSideString(hack::GetVersion(),
                              GUIColor());                  // github commit and date
                AddSideString(hack::GetType(), GUIColor()); //  Compile type
#if ENABLE_GUI
                AddSideString("Press 'INSERT' key to open/close cheat menu.", GUIColor());
                AddSideString("Use mouse to navigate in menu.", GUIColor());
#endif
            }
        }
    }
    if (spectator_target)
    {
        AddCenterString("Press SPACE to stop spectating");
    }
    {
        PROF_SECTION(DRAW_WRAPPER);
        EC::run(EC::Draw);
    }
    if (CE_GOOD(g_pLocalPlayer->entity) && !g_Settings.bInvalid)
    {
        IF_GAME(IsTF2())
        {
            PROF_SECTION(DRAW_skinchanger);
            hacks::tf2::skinchanger::DrawText();
        }
#ifndef FEATURE_FIDGET_SPINNER_ENABLED
        DrawSpinner();
#endif
        Prediction_PaintTraverse();
    }
    {
        PROF_SECTION(DRAW_strings);
        DrawStrings();
    }
#if ENABLE_GUI
    {
        PROF_SECTION(DRAW_GUI);
        gui::draw();
    }
#endif
}

void EndCheatVisuals()
{
#if !ENABLE_ENGINE_DRAWING && !ENABLE_IMGUI_DRAWING
    buffers[currentBuffer]->end();
#endif
#if !ENABLE_ENGINE_DRAWING || ENABLE_IMGUI_DRAWING
    currentBuffer = !currentBuffer;
#endif
}

void DrawCache()
{
#if !ENABLE_ENGINE_DRAWING && !ENABLE_IMGUI_DRAWING
    buffers[!currentBuffer]->replay();
#endif
}
