#include <thread>

#include "../../utils/utils.hpp"

#include "../menu/menu.hpp"
#include "../esp/esp.hpp"

#include "render.hpp"

#include <imgui/imgui.h>
#include <imgui/imgui_freetype.h>

#include "font.h"
#include "../esp/esp.hpp"

void render::Initialize( )
{
	ImGuiIO& IO = ImGui::GetIO( );

	ImVector<ImWchar> ranges;
	ImFontGlyphRangesBuilder builder;

	//IO.Fonts->Clear();
	ImFontConfig font_config;
	//font_config.FontBuilderFlags |= ;
	//IO.Fonts->AddFontFromFileTTF("", 13,
								 //nullptr,//&font_config,
								 //IO.Fonts->GetGlyphRangesCyrillic());
	visuals::pixel_font = IO.Fonts->AddFontFromMemoryTTF((void*) esp, sizeof(esp), 8.f);
	// 
	// ImFontConfig config;
	font_config.OversampleH = 3;
	font_config.OversampleV = 5;
	font_config.PixelSnapH = true;
	//font_config.FontBuilderFlags = ImGuiFreeTypeBuilderFlags_Monochrome | ImGuiFreeTypeBuilderFlags_MonoHinting;

	visuals::menu_font = IO.Fonts->AddFontFromFileTTF("C:\\Windows\\Fonts\\Verdana.ttf", 12, &font_config, IO.Fonts->GetGlyphRangesCyrillic( ));
	//IO.Fonts->Build();
}
void render::NewFrame( )
{
	visuals::Render( );
	menu::Render( );
}
void render::text_outline(ImDrawList* draw_list, const ImVec2& pos, ImU32 col,
						  const char* text)
{
	ImGui::PushFont(visuals::menu_font);
	//draw_list->AddText(ImVec2(pos.x + 1, pos.y - 1), IM_COL32(56, 57, 72, 255),
	//    text);
	//draw_list->AddText(ImVec2(pos.x - 1, pos.y + 1), IM_COL32(56, 57, 72, 255),
	//    text);
	//draw_list->AddText(ImVec2(pos.x - 1, pos.y - 1), IM_COL32(56, 57, 72, 255),
	//    text);
	//draw_list->AddText(ImVec2(pos.x + 1, pos.y + 1), IM_COL32(56, 57, 72, 255),
	//    text);

	//draw_list->AddText(ImVec2(pos.x, pos.y + 1), IM_COL32(85, 85, 143, 255),
	//    text);
	//draw_list->AddText(ImVec2(pos.x, pos.y - 1), IM_COL32(56, 57, 72, 255),
	//    text);
	//draw_list->AddText(ImVec2(pos.x + 1, pos.y), IM_COL32(56, 57, 72, 255),
	//    text);
	//draw_list->AddText(ImVec2(pos.x - 1, pos.y), IM_COL32(56, 57, 72, 255),
	//    text);
	draw_list->AddText(ImVec2(pos.x, pos.y), col, text);
	ImGui::PopFont( );
}

template <class T>
inline void line(ImDrawList* draw, T x1, T y1, T x2, T y2, ImColor color,
				 float thickness = 1.f)
{
	draw->AddLine(ImVec2(x1, y1), ImVec2(x2, y2), color, thickness);
}
template <class T>
inline void outline(ImDrawList* draw, T x1, T y1, T x2, T y2, ImColor color,
					float rounding = 0.f)
{
	draw->AddRectFilled(ImVec2(x1 - 1, y1 - 1), ImVec2(x2 + 1, y2 + 1), color,
						rounding);
}
template <class T>
inline void glow(ImDrawList* draw, T x1, T y1, T x2, T y2, ImColor color,
				 float rounding = 0.f)
{}

void render::corner_outline(ImDrawList* draw, float x1, float y1, float x2,
							float y2,
							ImColor color, ImColor color_outline, float th,
							float rounding)
{
	int w = x2 - x1;
	int h = y2 - y1;

	int iw = w / 4;
	int ih = h / 4;


	/* RENDER GLOW TOP CORNDER */
	/* RENDER GLOW BOTTOM CORNDER */

	draw->AddRectFilled(ImVec2(x1, y1), ImVec2(x2, y2), color, rounding);


	/* RENDER TOP CORNDER */
	outline(draw, x1, y1, x1 + iw, y1 + 1, color_outline, rounding);
	outline(draw, x1 + w - iw, y1, x1 + w, y1 + 1, color_outline, rounding);
	outline(draw, x1, y1, x1 + 1, y1 + ih, color_outline, rounding);
	outline(draw, x1 + w - 1, y1, x1 + w, y1 + ih, color_outline, rounding);
	/* RENDER BOTTOM CORNDER */
	outline(draw, x1, y1 + h, x1 + iw, y1 + h + 1, color_outline, rounding);
	outline(draw, x1 + w - iw, y1 + h, x1 + w, y1 + h + 1, color_outline, rounding);
	outline(draw, x1, y1 + h - ih, x1 + 1, y1 + h, color_outline, rounding);
	outline(draw, x1 + w - 1, y1 + h - ih, x1 + w, y1 + h, color_outline, rounding);

}
