#include "jkGUIDialog.h"

#include "General/Darray.h"
#include "General/stdBitmap.h"
#include "General/stdString.h"
#include "General/stdFont.h"
#include "Engine/rdMaterial.h" // TODO move tVBuffer
#include "stdPlatform.h"
#include "jk.h"
#include "Gui/jkGUIRend.h"
#include "Gui/jkGUI.h"
#include "Main/jkStrings.h"
#include "Win95/stdDisplay.h"

jkGuiElement jkGuiDialog_Ok_buttons[4] = {
    {ELEMENT_TEXT, 0, 2, 0, 3, {0x82, 0x8C, 0x186, 0x28}, 1, 0, 0, 0, 0, 0, {0}, 0},
    {ELEMENT_TEXT, 0, 0, 0, 3, {0x82, 0xBE, 0x186, 0x5A}, 1, 0, 0, 0, 0, 0, {0}, 0},
    {ELEMENT_TEXTBUTTON, 1, 2, "GUI_OK", 3, {0x118, 0x122, 0xBE, 0x28}, 1, 0, 0, 0, 0, 0, {0}, 0},
    {ELEMENT_END, 0, 0, 0, 0, {0}, 0, 0, 0, 0, 0, 0, {0}, 0}
};

static jkGuiMenu jkGuiDialog_Ok_menu  = {jkGuiDialog_Ok_buttons, -1, 0xE1, 0xFF, 0xF, 0, 0, jkGui_stdBitmaps, jkGui_stdFonts, 0, 0, "thermloop01.wav", "thrmlpu2.wav", 0, 0, 0, 0, 0, 0};

jkGuiElement jkGuiDialog_OkCancel_buttons[5] = {
    {ELEMENT_TEXT, 0, 2, 0, 3, {0x82, 0x8C, 0x186, 0x28}, 1, 0, 0, 0, 0, 0, {0}, 0},
    {ELEMENT_TEXT, 0, 0, 0, 3, {0x82, 0xBE, 0x186, 0x5A}, 1, 0, 0, 0, 0, 0, {0}, 0},
    {ELEMENT_TEXTBUTTON, 1, 2, "GUI_OK", 3, {0x14A, 0x122, 0xBE, 0x28}, 1, 0, 0, 0, 0, 0, {0}, 0},
    {ELEMENT_TEXTBUTTON, -1, 2, "GUI_CANCEL", 3, {0x82, 0x122, 0xBE, 0x28}, 1, 0, 0, 0, 0, 0, {0}, 0},
    {ELEMENT_END, 0, 0, 0, 0, {0}, 0, 0, 0, 0, 0, 0, {0}, 0}
};

jkGuiMenu jkGuiDialog_OkCancel_menu  = {jkGuiDialog_OkCancel_buttons, -1, 0xE1, 0xFF, 0xF, 0, 0, jkGui_stdBitmaps, jkGui_stdFonts, 0, 0, "thermloop01.wav", "thrmlpu2.wav", 0, 0, 0, 0, 0, 0};

void jkGuiDialog_Startup()
{
    jkGui_InitMenu(&jkGuiDialog_OkCancel_menu, 0);
    jkGui_InitMenu(&jkGuiDialog_Ok_menu, 0);
}

void jkGuiDialog_Shutdown()
{
    stdPlatform_Printf("OpenJKDF2: %s\n", __func__); // Added
}

tVBuffer *jkGuiDialog_sub_416900()
{
    tVBuffer *v0; // eax
    tVBuffer *v1; // esi

    // Added
    stdBitmap_EnsureData(jkGui_stdBitmaps[JKGUI_BM_BK_MAIN]);
    stdBitmap_EnsureData(jkGui_stdBitmaps[JKGUI_BM_BK_DIALOG]);

    v0 = stdDisplay_VBufferNew(&(*jkGui_stdBitmaps[JKGUI_BM_BK_MAIN]->mipSurfaces)->format, 0, 0, Video_menuBuffer.palette);
    v1 = v0;
    if ( v0 )
    {
        int copyX = jkGui_stdBitmaps[JKGUI_BM_BK_DIALOG]->xPos;
        int copyY = jkGui_stdBitmaps[JKGUI_BM_BK_DIALOG]->yPos;
#ifdef JKGUI_SMOL_SCREEN
        copyX = (int)((flex_t)copyX * 0.4);
        copyY = (int)((flex_t)copyY * 0.4);
#endif
        stdDisplay_VBufferCopy(v0, &Video_menuBuffer, 0, 0, 0, 0);
        stdDisplay_VBufferCopy(v1, *jkGui_stdBitmaps[JKGUI_BM_BK_DIALOG]->mipSurfaces, copyX, copyY, 0, 0);
    }

    stdBitmap_UnloadData(jkGui_stdBitmaps[JKGUI_BM_BK_DIALOG]);

    return v1;
}

int jkGuiDialog_OkCancelDialog(char16_t *stringA, char16_t *stringB)
{
    int v2; // edi
    int v5; // esi

    // Added
    stdBitmap_EnsureData(jkGui_stdBitmaps[JKGUI_BM_BK_MAIN]);

    v2 = 0;
    if ( !jkGui_GdiMode )
    {
        jkGui_SetModeMenu(jkGui_stdBitmaps[JKGUI_BM_BK_MAIN]->palette);
        v2 = 1;
    }
    jkGuiDialog_OkCancel_buttons[0].wstr = stringA;
    jkGuiDialog_OkCancel_buttons[1].wstr = stringB;
#ifdef STDBITMAP_PARTIAL_LOAD
    jkGuiDialog_OkCancel_menu.pTextureOverride = jkGuiDialog_sub_416900();
    jkGuiDialog_OkCancel_menu.pBgBitmap = NULL;
#else
    jkGuiDialog_OkCancel_menu.texture = jkGuiDialog_sub_416900();
    jkGuiDialog_OkCancel_menu.palette = NULL;
#endif
    jkGuiDialog_OkCancel_buttons[2].wstr = jkStrings_GetUniStringWithFallback("GUI_OK");
    jkGuiDialog_OkCancel_buttons[3].bIsVisible = 1;
    jkGuiRend_MenuSetReturnKeyShortcutElement(&jkGuiDialog_OkCancel_menu, &jkGuiDialog_OkCancel_buttons[2]);
    jkGuiRend_MenuSetEscapeKeyShortcutElement(&jkGuiDialog_OkCancel_menu, &jkGuiDialog_OkCancel_buttons[3]);
    v5 = jkGuiRend_DisplayAndReturnClicked(&jkGuiDialog_OkCancel_menu);
    
#ifdef STDBITMAP_PARTIAL_LOAD
    stdDisplay_VBufferFree(jkGuiDialog_OkCancel_menu.pTextureOverride);
    jkGuiDialog_OkCancel_menu.pTextureOverride = NULL;
#else
    stdDisplay_VBufferFree(jkGuiDialog_OkCancel_menu.texture);
    jkGuiDialog_OkCancel_menu.texture = NULL;
#endif
    if ( v2 )
        jkGui_SetModeGame();

    // Added
    stdBitmap_UnloadData(jkGui_stdBitmaps[JKGUI_BM_BK_MAIN]);

    return v5 == 1;
}

void jkGuiDialog_ErrorDialog(char16_t *stringA, char16_t *stringB)
{
    int v2; // edi

    // Added
    stdBitmap_EnsureData(jkGui_stdBitmaps[JKGUI_BM_BK_MAIN]);

    v2 = 0;
    if ( !jkGui_GdiMode )
    {
        jkGui_SetModeMenu(jkGui_stdBitmaps[JKGUI_BM_BK_MAIN]->palette);
        v2 = 1;
    }
    jkGuiDialog_Ok_buttons[0].wstr = stringA;
    jkGuiDialog_Ok_buttons[1].wstr = stringB;
#ifdef STDBITMAP_PARTIAL_LOAD
    jkGuiDialog_Ok_menu.pTextureOverride = jkGuiDialog_sub_416900();
    jkGuiDialog_Ok_menu.pBgBitmap = NULL;
#else
    jkGuiDialog_Ok_menu.texture = jkGuiDialog_sub_416900();
    jkGuiDialog_Ok_menu.palette = NULL;
#endif
    jkGuiDialog_Ok_buttons[2].wstr = jkStrings_GetUniStringWithFallback("GUI_OK");
    jkGuiRend_MenuSetReturnKeyShortcutElement(&jkGuiDialog_Ok_menu, &jkGuiDialog_Ok_buttons[2]);
    jkGuiRend_MenuSetEscapeKeyShortcutElement(&jkGuiDialog_Ok_menu, &jkGuiDialog_Ok_buttons[2]);
    jkGuiRend_DisplayAndReturnClicked(&jkGuiDialog_Ok_menu);
    
#ifdef STDBITMAP_PARTIAL_LOAD
    stdDisplay_VBufferFree(jkGuiDialog_Ok_menu.pTextureOverride);
    jkGuiDialog_Ok_menu.pTextureOverride = NULL;
#else
    stdDisplay_VBufferFree(jkGuiDialog_Ok_menu.texture);
    jkGuiDialog_Ok_menu.texture = NULL;
#endif
    if ( v2 )
        jkGui_SetModeGame();

    // Added
    stdBitmap_UnloadData(jkGui_stdBitmaps[JKGUI_BM_BK_MAIN]);
}

int jkGuiDialog_YesNoDialog(char16_t *stringA, char16_t *stringB)
{
    int v2; // edi
    int v5; // esi

    // Added
    stdBitmap_EnsureData(jkGui_stdBitmaps[JKGUI_BM_BK_MAIN]);

    v2 = 0;
    if ( !jkGui_GdiMode )
    {
        jkGui_SetModeMenu(jkGui_stdBitmaps[JKGUI_BM_BK_MAIN]->palette);
        v2 = 1;
    }
    jkGuiDialog_OkCancel_buttons[0].wstr = stringA;
    jkGuiDialog_OkCancel_buttons[1].wstr = stringB;
#ifdef STDBITMAP_PARTIAL_LOAD
    jkGuiDialog_OkCancel_menu.pTextureOverride = jkGuiDialog_sub_416900();
    jkGuiDialog_OkCancel_menu.pBgBitmap = NULL;
#else
    jkGuiDialog_OkCancel_menu.texture = jkGuiDialog_sub_416900();
    jkGuiDialog_OkCancel_menu.palette = NULL;
#endif
    jkGuiDialog_OkCancel_buttons[2].wstr = jkStrings_GetUniStringWithFallback("GUI_YES");
    jkGuiDialog_OkCancel_buttons[3].wstr = jkStrings_GetUniStringWithFallback("GUI_NO");
    jkGuiDialog_OkCancel_buttons[3].bIsVisible = 1;
    jkGuiRend_MenuSetReturnKeyShortcutElement(&jkGuiDialog_OkCancel_menu, &jkGuiDialog_OkCancel_buttons[2]);
    jkGuiRend_MenuSetEscapeKeyShortcutElement(&jkGuiDialog_OkCancel_menu, &jkGuiDialog_OkCancel_buttons[3]);
    v5 = jkGuiRend_DisplayAndReturnClicked(&jkGuiDialog_OkCancel_menu);
    
#ifdef STDBITMAP_PARTIAL_LOAD
    stdDisplay_VBufferFree(jkGuiDialog_OkCancel_menu.pTextureOverride);
    jkGuiDialog_OkCancel_menu.pTextureOverride = NULL;
#else
    stdDisplay_VBufferFree(jkGuiDialog_OkCancel_menu.texture);
    jkGuiDialog_OkCancel_menu.texture = NULL;
#endif
    if ( v2 )
        jkGui_SetModeGame();

    // Added
    stdBitmap_UnloadData(jkGui_stdBitmaps[JKGUI_BM_BK_MAIN]);

    return v5 == 1;
}
