#include "AImGui.hpp"
#include "../Header/ANwCreator.hpp"
#include "STOM/Includes/font.h"
extern bool g_PreventRecord;
namespace android
{
    AImGui::AImGui(const Options &options)
        : m_options(options)
    {
        InitEnvironment();
    }

    void AImGui::Destroy()
    {
        UnInitEnvironment();
    }

    void AImGui::BeginFrame()
    {
        if (!m_state)
            return;

        auto displayInfo = ANwCreator::GetDisplayInfo(m_options.activity);

        // Check if display orientation is changed
        if (m_rotateTheta != displayInfo.theta)
        {
            UnInitEnvironment();
            InitEnvironment();
        }

        ImGui_ImplOpenGL3_NewFrame();
        ImGui_ImplAndroid_NewFrame();
        ImGui::NewFrame();
    }

    void AImGui::EndFrame()
    {
        if (!m_state)
            return;

        ImGui::Render();
        glClear(GL_COLOR_BUFFER_BIT);
        ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
        eglSwapBuffers(m_defaultDisplay, m_eglSurface);
    }

    bool AImGui::InitEnvironment()
    {
        if (!m_options.activity)
        {
            
            return false;
        }

if(g_PreventRecord){
        ANwCreator::CreateOptions createOptions;
        createOptions.name = "AImGui";
        createOptions.skipScreenshot = m_options.skipScreenshot;
m_nativeWindow = ANwCreator::Create(m_options.activity, createOptions);
}else{
ANwCreator::CreateOptions createOptions;
createOptions.name = "AImGui";
createOptions.skipScreenshot = false;
createOptions.secure = false;
        m_nativeWindow = ANwCreator::Create(m_options.activity, createOptions);
}

        if (!m_nativeWindow)
        {
            
            return false;
        }

        ANativeWindow_acquire(m_nativeWindow);

        auto displayInfo = ANwCreator::GetDisplayInfo(m_options.activity);
        m_screenWidth = displayInfo.width;
        m_screenHeight = displayInfo.height;

        m_defaultDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
        if (EGL_NO_DISPLAY == m_defaultDisplay)
        {
            
            return false;
        }

        if (EGL_TRUE != eglInitialize(m_defaultDisplay, nullptr, nullptr))
        {
            
            return false;
        }

        EGLint numConfig = 0;
        EGLConfig config{};
        const EGLint attribs[] = {
            EGL_SURFACE_TYPE, EGL_WINDOW_BIT,

            EGL_RED_SIZE, 8,
            EGL_GREEN_SIZE, 8,
            EGL_BLUE_SIZE, 8,
            EGL_ALPHA_SIZE, 8,
            EGL_DEPTH_SIZE, 24,
            EGL_STENCIL_SIZE, 8,
            EGL_SAMPLE_BUFFERS, 0,
            EGL_NONE};

        if (EGL_TRUE != eglChooseConfig(m_defaultDisplay, attribs, &config, 1, &numConfig))
        {
            
            return false;
        }

        if (0 == numConfig)
        {
            
            return false;
        }

        EGLint format;
        if (EGL_TRUE != eglGetConfigAttrib(m_defaultDisplay, config, EGL_NATIVE_VISUAL_ID, &format))
        {
            
            return false;
        }

        ANativeWindow_setBuffersGeometry(m_nativeWindow, 0, 0, format);

        m_eglSurface = eglCreateWindowSurface(m_defaultDisplay, config, m_nativeWindow, nullptr);
        if (EGL_NO_SURFACE == m_eglSurface)
        {
            
            return false;
        }

        const EGLint contextAttribs[] = {
            EGL_CONTEXT_CLIENT_VERSION, 3,
            EGL_NONE};

        m_eglContext = eglCreateContext(m_defaultDisplay, config, EGL_NO_CONTEXT, contextAttribs);
        if (EGL_NO_CONTEXT == m_eglContext)
        {
            
            return false;
        }

        if (EGL_TRUE != eglMakeCurrent(m_defaultDisplay, m_eglSurface, m_eglSurface, m_eglContext))
        {
            
            return false;
        }

        IMGUI_CHECKVERSION();

        m_imguiContext = ImGui::CreateContext();
        if (nullptr == m_imguiContext)
            return false;

        auto &io = ImGui::GetIO();
        io.IniFilename = nullptr;

ImGui::GetStyle().ScaleAllSizes(6.0f);
ImGuiStyle *style = &ImGui::GetStyle();
ImGui::StyleColorsLight(style);

style->WindowRounding = 12.0f;
style->FrameRounding = 6.0f;
style->GrabRounding = 4.0f;
style->WindowPadding = ImVec2(12, 10);
style->ItemSpacing = ImVec2(8, 6);
style->ScrollbarSize = 12.0f;
style->WindowBorderSize = 1.0f;
style->FrameBorderSize = 0.0f;
style->PopupBorderSize = 1.0f;
io.ConfigWindowsMoveFromTitleBarOnly = true;
io.IniFilename = NULL;

ImFontConfig fontConfig;
fontConfig.SizePixels = FontSize;
fontConfig.MergeMode = false;
fontConfig.PixelSnapH = true;
io.Fonts->AddFontFromMemoryTTF((void *)FontData, FontSize, 18.0f, NULL, io.Fonts->GetGlyphRangesChineseFull());

io.Fonts->Build();
        ImGui_ImplAndroid_Init(m_nativeWindow);
        ImGui_ImplOpenGL3_Init("#version 300 es");

        glViewport(0, 0, m_screenWidth, m_screenHeight);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        m_rotateTheta = displayInfo.theta;
        m_screenWidth = displayInfo.width;
        m_screenHeight = displayInfo.height;

        return (m_state = true);
    }

    void AImGui::UnInitEnvironment()
    {
        m_state = false;

        if (nullptr != m_imguiContext)
        {
            ImGui_ImplOpenGL3_Shutdown();
            ImGui_ImplAndroid_Shutdown();
            // ImGui::DestroyContext(m_imguiContext);
            ImGui::SetCurrentContext(nullptr);
        }

        if (EGL_NO_DISPLAY != m_defaultDisplay)
        {
            eglMakeCurrent(m_defaultDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);

            if (EGL_NO_CONTEXT != m_eglContext)
            {
                eglDestroyContext(m_defaultDisplay, m_eglContext);
            }

            if (EGL_NO_SURFACE != m_eglSurface)
            {
                eglDestroySurface(m_defaultDisplay, m_eglSurface);
            }

            eglTerminate(m_defaultDisplay);
        }

        if (nullptr != m_nativeWindow)
        {
            ANativeWindow_release(m_nativeWindow);
            ANwCreator::Destroy(m_options.activity, m_nativeWindow);
        }

        m_imguiContext = nullptr;
        m_eglContext = EGL_NO_CONTEXT;
        m_eglSurface = EGL_NO_SURFACE;
        m_defaultDisplay = EGL_NO_DISPLAY;
        m_nativeWindow = nullptr;
    }

} // namespace android

