#pragma once

#include "IUI/ImGui/imgui.h"
#include "tl/narrow_cast.h"
#include "Kit/util/Color.h"
#include "IUI/Icons.h"
#include "Kit/util/IdentifierUtils.h"
#include "extras/imguiwrap.dear.h"
#include "tl/enum.h"
#include "tl/variant.h"

namespace ImGui
{

inline void PushID(size_t id) { PushID(static_cast<int>(id)); }

ImU32 ColorU32(kit::color::sRGBAi c);
ImU32 ColorU32(kit::color::sRGBi c);

void AddDottedLine(ImDrawList& list, ImVec2 p1, ImVec2 p2, ImU32 col, float dotLength, float thickness = 1.0f);

ImVec2 GetWindowContentPos();
ImVec2 GetWindowContentSize();
ImVec2 GetWindowContentBottomRightSide();

int8_t GetMouseButtonClicked();
int8_t GetMouseButtonDown();
int8_t GetMouseButtonReleased();

bool ColorEditor(kit::color::lRGBAhdr& io_color, bool& saveUndo, bool isHDR, bool hasAlpha, const tl::string& label, float alignNameX) noexcept;
bool CheckBoxTristate(const char* label, int* v_tristate);

bool BeginIconMenu(kit::icons::Icon icon, const char* label);
bool BeginIconMenu(kit::icons::FontIcon icon, const char* label);
bool MenuIconItem(kit::icons::Icon icon, const char* label, bool* selected = nullptr);
bool MenuIconItem(kit::icons::FontIcon icon, const char* label, bool* selected = nullptr);

void Icon(kit::icons::Icon icon, ImVec2 frameSize, ImVec2 size = ImVec2(0, 0), ImVec4 tint_col = ImVec4(1,1,1,1), ImVec4 border_col = ImVec4(0,0,0,0));
void Icon(kit::icons::FontIcon icon, ImVec2 frameSize, ImVec2 size = ImVec2(0, 0), ImVec4 tint_col = ImVec4(1,1,1,1), ImVec4 border_col = ImVec4(0,0,0,0));
bool IconButton(const char* str_id, kit::icons::Icon icon, ImVec2 size, bool active = false, ImVec4 bg_col = ImVec4(0,0,0,0), ImVec4 tint_col = ImVec4(1,1,1,1));    // <0 frame_padding uses default frame padding settings. 0 for no padding
bool IconButton(const char* str_id, kit::icons::FontIcon icon, const char* label, bool active = false);

bool ToggleButton(const char* str_id, bool* v);
bool Splitter(bool split_vertically, float thickness, float* size1, float* size2, float min_size1, float min_size2, float splitter_long_axis_size = -1.0f);

void BeginGroupPanel(const char* name, const ImVec2& size);
void EndGroupPanel();

template<typename Enum>
bool EnumCombo(const char* str_id, Enum& crtValue, bool humanReadable)
{
	bool changed = false;
	const tl::string preview = humanReadable ? kit::util::identifierToHumanReadableString(tl::string(tl::enum_name(crtValue))) : tl::string(tl::enum_name(crtValue));
	if (const dear::Combo combo(str_id, preview.c_str(), ImGuiComboFlags_None); combo)
	{
		for (auto e: tl::enum_entries<Enum>())
		{
			tl::string label = humanReadable ? kit::util::identifierToHumanReadableString(tl::string(e.second)) : tl::string(e.second);
			//int32_t value = static_cast<int32_t>(e.first);
			PushID(tl::narrow<int>(e.first));
			const bool itemSelected = crtValue == e.first;
			if (Selectable(label.c_str(), itemSelected))
			{
				crtValue = e.first;
				changed = true;
			}
			if (itemSelected)
				SetItemDefaultFocus();
			PopID();
		}
	}
	return changed;
}

void AddUnderLine(ImColor col);
void TextURL(const char* name, const char* url);

}
