#include "ImHelper.h"
#include "Utils/Utils.h"
#include "d3dx11.h"
#pragma comment(lib,"D3Dx11.lib")
void ImDrawLine(float x1, float y1, float x2, float y2, ImU32 clr, float thickness)
{
    ImGui::GetBackgroundDrawList()->AddLine(ImVec2(x1, y1), ImVec2(x2, y2), clr, thickness);
}
void ImDrawCircle(float x, float y, float rad, ImU32 clr, float thickness)
{
    int line = rad / 2;
    ImGui::GetBackgroundDrawList()->AddCircle(ImVec2(x, y), rad, clr, line, thickness);
}
void ImDrawRect(float x, float y, float width, float height, ImU32 clr, float thickness)
{
    ImVec2 min = ImVec2(x, y);
    ImVec2 max = ImVec2(x + width, y + height);
    ImGui::GetBackgroundDrawList()->AddRect(min, max, clr, 0.0F, -1, thickness);
}
void ImFillRect(float x, float y, float width, float height, ImU32 clr)
{
    ImVec2 min = ImVec2(x, y);
    ImVec2 max = ImVec2(x + width, y + height);
    ImGui::GetBackgroundDrawList()->AddRectFilled(min, max, clr, 0.0F, -1);
}
void ImDrawString(ImFont* font, float sizez, float x, float y, ImU32 color, const char* message, ...)
{
    char output[1024] = {};
    va_list args;
    va_start(args, message);
    vsprintf_s(output, message, args);
    va_end(args);
    auto coord = ImVec2(x, y);
    auto size = ImGui::CalcTextSize(output);
    auto coord_out = ImVec2{ coord.x + 1.f, coord.y + 1.f };
    ImColor black = ImColor(0, 0, 0);
    auto _msg = Convert::AnsiToUtf8(message);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x - 1, y), black, _msg.c_str());
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y - 1), black, _msg.c_str());
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x + 1, y), black, _msg.c_str());
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y + 1), black, _msg.c_str());
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y), color, _msg.c_str());
}
void ImDrawStringCenter(ImFont* font, float sizez, float x, float y, ImU32 color, const char* message, ...)
{
    char output[1024] = {};
    va_list args;
    va_start(args, message);
    vsprintf_s(output, message, args);
    va_end(args);
    auto coord = ImVec2(x, y);
    auto size = ImGui::CalcTextSize(output);
    auto coord_out = ImVec2{ coord.x + 1.f, coord.y + 1.f };
    ImColor black = ImColor(0, 0, 0);
    x -= (size.x * 0.5f);
    y -= (size.y * 0.5f);
    auto _msg = Convert::AnsiToUtf8(message);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x - 1, y), black, _msg.c_str());
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y - 1), black, _msg.c_str());
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x + 1, y), black, _msg.c_str());
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y + 1), black, _msg.c_str());
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y), color, _msg.c_str());
}

void ImDrawStringU8(ImFont* font, float sizez, float x, float y, ImU32 color, const char* message, ...)
{

    char output[1024] = {};
    va_list args;
    va_start(args, message);
    vsprintf_s(output, message, args);
    va_end(args);
    auto coord = ImVec2(x, y);
    auto size = ImGui::CalcTextSize(output);
    auto coord_out = ImVec2{ coord.x + 1.f, coord.y + 1.f };
    ImColor black = ImColor(0, 0, 0);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x - 1, y), black, message);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y - 1), black, message);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x + 1, y), black, message);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y + 1), black, message);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y), color, message);
}
void ImDrawStringCenterU8(ImFont* font, float sizez, float x, float y, ImU32 color, const char* message, ...)
{
    char output[1024] = {};
    va_list args;
    va_start(args, message);
    vsprintf_s(output, message, args);
    va_end(args);
    auto coord = ImVec2(x, y);
    auto size = ImGui::CalcTextSize(output);
    auto coord_out = ImVec2{ coord.x + 1.f, coord.y + 1.f };
    ImColor black = ImColor(0, 0, 0);
    x -= (size.x * 0.5f);
    y -= (size.y * 0.5f);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x - 1, y), black, message);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y - 1), black, message);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x + 1, y), black, message);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y + 1), black, message);
    ImGui::GetBackgroundDrawList()->AddText(font, sizez, ImVec2(x, y), color, message);
}
ID3D11ShaderResourceView* LoadImageFromMem(const unsigned char* mem, int size)
{
    ID3D11Device* d3dDevice = *(ID3D11Device**)ImGui::GetIO().BackendRendererUserData;
    ID3D11ShaderResourceView* pFontTextureView = NULL;
    D3D11_TEXTURE2D_DESC dec = {};

    HRESULT hr;
    D3DX11_IMAGE_LOAD_INFO loadInfo;
    ZeroMemory(&loadInfo, sizeof(D3DX11_IMAGE_LOAD_INFO));
    loadInfo.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    loadInfo.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    loadInfo.MipLevels = D3DX11_DEFAULT;
    loadInfo.MipFilter = D3DX11_FILTER_LINEAR;
    hr = D3DX11CreateShaderResourceViewFromMemory(d3dDevice, mem, size, &loadInfo, NULL, &pFontTextureView, NULL);
    if (hr != S_OK)
        return NULL;
    return pFontTextureView;
}
ID3D11ShaderResourceView* LoadImageFromTexture(ID3D11Texture2D* pTexture2D)
{
    ID3D11Device* d3dDevice = *(ID3D11Device**)ImGui::GetIO().BackendRendererUserData;
    D3D11_TEXTURE2D_DESC dec{};
    pTexture2D->GetDesc(&dec);
    ID3D11ShaderResourceView* pFontTextureView = NULL;
    D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc;
    ZeroMemory(&srvDesc, sizeof(srvDesc));
    srvDesc.Format = dec.Format;
    srvDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
    srvDesc.Texture2D.MostDetailedMip = 0;
    srvDesc.Texture2D.MipLevels = dec.MipLevels;
    HRESULT hr = d3dDevice->CreateShaderResourceView(pTexture2D, &srvDesc, &pFontTextureView);
    if (FAILED(hr))
        return NULL;
    return pFontTextureView;
}
void ImImage(ID3D11ShaderResourceView* img, ImVec2 location, ImVec2 size)
{
    ImGui::GetBackgroundDrawList()->AddImage(img, location, size);
}