/*
    Generic NUI Windows
    Created By: Daz
*/

#include "inc_nui"

const string GNW_WINDOW_PREFIX      = "GNW";
const string GNW_OK_CANCEL_WINDOW   = "OKCANCEL";
const string GNW_OPTIONS_WINDOW     = "OPTIONS";
const string GNW_INFO_WINDOW        = "INFO";
const string GNW_MENU_WINDOW        = "MENU";

// Call this in your EVENT_SCRIPT_MODULE_ON_NUI_EVENT script
void GNW_HandleNUIEvents();
void GNW_ShowOkCancelWindow(object oPlayer, string sText, string sOkAction, string sCancelAction);
json GNW_AddOption(json jOptions, string sLabel, string sVarName, object oTarget);
void GNW_ShowOptionsWindow(object oPlayer, string sTitle, json jOptions);
void GNW_ShowInfoWindow(object oPlayer, string sTitle, string sText);

void GNW_HandleNUIEvents()
{
    object oPlayer = NuiGetEventPlayer();
    int nToken = NuiGetEventWindow();
    string sWindowId  = NuiGetWindowId(oPlayer, nToken);

    if (GetStringLeft(sWindowId, GetStringLength(GNW_WINDOW_PREFIX)) != GNW_WINDOW_PREFIX)
        return;

    string sType = NuiGetEventType();
    string sElement = NuiGetEventElement();

    if (sWindowId == GNW_WINDOW_PREFIX + GNW_OK_CANCEL_WINDOW)
    {
        if (sType == "click")
        {
            if (sElement == "btn_ok")
            {
                ExecuteScriptChunk(GetLocalString(oPlayer, GNW_WINDOW_PREFIX + GNW_OK_CANCEL_WINDOW + "_OKACTION"), oPlayer);
                NuiDestroy(oPlayer, nToken);
            }
            else if (sElement == "btn_cancel")
            {
                ExecuteScriptChunk(GetLocalString(oPlayer, GNW_WINDOW_PREFIX + GNW_OK_CANCEL_WINDOW + "_CANCELACTION"), oPlayer);
                NuiDestroy(oPlayer, nToken);
            }
        }
        else if (sType == "close")
        {
            ExecuteScriptChunk(GetLocalString(oPlayer, GNW_WINDOW_PREFIX + GNW_OK_CANCEL_WINDOW + "_CANCELACTION"), oPlayer);
        }
    }
    else if (sWindowId == GNW_WINDOW_PREFIX + GNW_OPTIONS_WINDOW)
    {
        if (sType == "click")
        {
            if (sElement == "btn_ok")
            {
                NuiDestroy(oPlayer, nToken);
            }
        }
        else if (sType == "watch")
        {
            json jOptions = GetLocalJson(oPlayer, GNW_WINDOW_PREFIX + GNW_OPTIONS_WINDOW);
            json jChecked = NuiGetBind(oPlayer, nToken, "checked");
            int nOption, nNumOptions = JsonGetLength(jOptions);

            for (nOption = 0; nOption < nNumOptions; nOption++)
            {
                json jOption = JsonArrayGet(jOptions, nOption);
                string sVarName = JsonGetString(JsonObjectGet(jOption, "varname"));
                object oTarget = StringToObject(JsonGetString(JsonObjectGet(jOption, "target")));
                int bCurrentValue = GetLocalInt(oTarget, sVarName);
                int bNewValue = JsonGetInt(JsonArrayGet(jChecked, nOption));

                if (bCurrentValue != bNewValue)
                {
                    SetLocalInt(oTarget, sVarName, bNewValue);
                    SendMessageToPC(oPlayer, "Setting '" + sVarName + "' on " + GetName(oTarget) + " to " + IntToString(bNewValue));
                }
            }
        }
    }
    else if (sWindowId == GNW_WINDOW_PREFIX + GNW_INFO_WINDOW)
    {
        if (sType == "click")
        {
            if (sElement == "btn_ok")
            {
                NuiDestroy(oPlayer, nToken);
            }
        }
    }
    else if (sWindowId == GNW_WINDOW_PREFIX + GNW_MENU_WINDOW)
    {
        if (sType == "click")
        {
            if (sElement == "btn_menu")
            {
                int bSelected = JsonGetInt(NuiGetBind(oPlayer, nToken, "btn_menu"));

                SendMessageToPC(oPlayer, "Menu: " + (bSelected ? "Open" : "Close"));
            }
        }
    }

}

void GNW_ShowOkCancelWindow(object oPlayer, string sText, string sOkAction, string sCancelAction)
{
    object oModule = GetModule();
    json jWindow = GetLocalJson(oModule, GNW_WINDOW_PREFIX + GNW_OK_CANCEL_WINDOW);
    if (jWindow == JsonNull())
    {
        json jCol;
        json jRow;

        jCol = JsonArray();

        jRow = JsonArray();
        {
            json jText = NuiText(NuiBind("text"));
            jRow = JsonArrayInsert(jRow, jText);
        }
        jCol = JsonArrayInsert(jCol, NuiRow(jRow));

        jRow = JsonArray();
        {
            json jButtonCancel = NuiButton(JsonString("Cancel"));
                 jButtonCancel = NuiId(jButtonCancel, "btn_cancel");
                 jButtonCancel = NuiWidth(jButtonCancel, 100.0f);
                 jButtonCancel = NuiHeight(jButtonCancel, 35.0f);
            jRow = JsonArrayInsert(jRow, jButtonCancel);

            jRow = JsonArrayInsert(jRow, NuiSpacer());

            json jButtonOK = NuiButton(JsonString("OK"));
                 jButtonOK = NuiId(jButtonOK, "btn_ok");
                 jButtonOK = NuiWidth(jButtonOK, 100.0f);
                 jButtonOK = NuiHeight(jButtonOK, 35.0f);
            jRow = JsonArrayInsert(jRow, jButtonOK);

        }
        jCol = JsonArrayInsert(jCol, NuiRow(jRow));

        json jRoot = NuiCol(jCol);
        jWindow = NuiWindow(jRoot, JsonBool(FALSE), NuiRect(-1.0f, -1.0f, 400.0f, 200.0f), JsonBool(FALSE), JsonBool(FALSE), JsonBool(FALSE), JsonBool(FALSE), JsonBool(TRUE));
        SetLocalJson(oModule, GNW_WINDOW_PREFIX + GNW_OK_CANCEL_WINDOW, jWindow);
    }

    int nToken = NuiCreate(oPlayer, jWindow, GNW_WINDOW_PREFIX + GNW_OK_CANCEL_WINDOW);

    NuiSetBind(oPlayer, nToken, "text", JsonString(sText));
    SetLocalString(oPlayer, GNW_WINDOW_PREFIX + GNW_OK_CANCEL_WINDOW + "_OKACTION", sOkAction);
    SetLocalString(oPlayer, GNW_WINDOW_PREFIX + GNW_OK_CANCEL_WINDOW + "_CANCELACTION", sCancelAction);
}

json GNW_AddOption(json jOptions, string sLabel, string sVarName, object oTarget)
{
    json jOption = JsonObject();
         jOption = JsonObjectSet(jOption, "label", JsonString(sLabel));
         jOption = JsonObjectSet(jOption, "varname", JsonString(sVarName));
         jOption = JsonObjectSet(jOption, "target", JsonString(ObjectToString(oTarget)));
    return JsonArrayInsert(jOptions, jOption);
}

void GNW_ShowOptionsWindow(object oPlayer, string sTitle, json jOptions)
{
    object oModule = GetModule();
    json jWindow = GetLocalJson(oModule, GNW_WINDOW_PREFIX + GNW_OPTIONS_WINDOW);
    if (jWindow == JsonNull())
    {
        json jCol;
        json jRow;

        jCol = JsonArray();

        jRow = JsonArray();
        {
            json jListTemplate = JsonArray();
            {
                json jCheck = NuiCheck(NuiBind("labels"), NuiBind("checked"));
                jListTemplate = JsonArrayInsert(jListTemplate, NuiListTemplateCell(jCheck, 0.0f, TRUE));
            }
            jRow = JsonArrayInsert(jRow, NuiList(jListTemplate, NuiBind("checked"), 15.0f));
        }
        jCol = JsonArrayInsert(jCol, NuiRow(jRow));

        jRow = JsonArray();
        {
            jRow = JsonArrayInsert(jRow, NuiSpacer());
            json jButtonOK = NuiButton(JsonString("OK"));
                 jButtonOK = NuiId(jButtonOK, "btn_ok");
                 jButtonOK = NuiWidth(jButtonOK, 100.0f);
                 jButtonOK = NuiHeight(jButtonOK, 35.0f);
            jRow = JsonArrayInsert(jRow, jButtonOK);

        }
        jCol = JsonArrayInsert(jCol, NuiRow(jRow));

        json jRoot = NuiCol(jCol);
        jWindow = NuiWindow(jRoot, NuiBind("title"), NuiRect(-1.0f, -1.0f, 350.0f, 300.0f), JsonBool(FALSE), JsonNull(), JsonBool(TRUE), JsonBool(FALSE), JsonBool(TRUE));
        SetLocalJson(oModule, GNW_WINDOW_PREFIX + GNW_OPTIONS_WINDOW, jWindow);
    }

    int nToken = NuiCreate(oPlayer, jWindow, GNW_WINDOW_PREFIX + GNW_OPTIONS_WINDOW);

    SetLocalJson(oPlayer, GNW_WINDOW_PREFIX + GNW_OPTIONS_WINDOW, jOptions);
    NuiSetBind(oPlayer, nToken, "title", JsonString(sTitle));

    json jLabels = JsonArray();
    json jChecked = JsonArray();
    int nOption, nNumOptions = JsonGetLength(jOptions);
    for (nOption = 0; nOption < nNumOptions; nOption++)
    {
        json jOption = JsonArrayGet(jOptions, nOption);
        json jLabel = JsonObjectGet(jOption, "label");
        string sVarName = JsonGetString(JsonObjectGet(jOption, "varname"));
        object oTarget = StringToObject(JsonGetString(JsonObjectGet(jOption, "target")));

        jLabels = JsonArrayInsert(jLabels, jLabel);
        jChecked = JsonArrayInsert(jChecked, JsonBool(GetLocalInt(oTarget, sVarName)));
    }

    NuiSetBind(oPlayer, nToken, "labels", jLabels);
    NuiSetBind(oPlayer, nToken, "checked", jChecked);
    NuiSetBindWatch(oPlayer, nToken, "checked", TRUE);
}

void GNW_ShowInfoWindow(object oPlayer, string sTitle, string sText)
{
    object oModule = GetModule();
    json jWindow = GetLocalJson(oModule, GNW_WINDOW_PREFIX + GNW_INFO_WINDOW);
    if (jWindow == JsonNull())
    {
        json jCol;
        json jRow;

        jCol = JsonArray();

        jRow = JsonArray();
        {
            json jText = NuiText(NuiBind("text"));
            jRow = JsonArrayInsert(jRow, jText);
        }
        jCol = JsonArrayInsert(jCol, NuiRow(jRow));

        jRow = JsonArray();
        {
            jRow = JsonArrayInsert(jRow, NuiSpacer());

            json jButtonOK = NuiButton(JsonString("OK"));
                 jButtonOK = NuiId(jButtonOK, "btn_ok");
                 jButtonOK = NuiWidth(jButtonOK, 100.0f);
                 jButtonOK = NuiHeight(jButtonOK, 35.0f);
            jRow = JsonArrayInsert(jRow, jButtonOK);

            jRow = JsonArrayInsert(jRow, NuiSpacer());
        }
        jCol = JsonArrayInsert(jCol, NuiRow(jRow));

        json jRoot = NuiCol(jCol);
        jWindow = NuiWindow(jRoot, NuiBind("title"), NuiRect(-1.0f, -1.0f, 400.0f, 200.0f), JsonBool(FALSE), JsonNull(), JsonBool(TRUE), JsonBool(FALSE), JsonBool(TRUE));
        SetLocalJson(oModule, GNW_WINDOW_PREFIX + GNW_INFO_WINDOW, jWindow);
    }

    int nToken = NuiCreate(oPlayer, jWindow, GNW_WINDOW_PREFIX + GNW_INFO_WINDOW);

    NuiSetBind(oPlayer, nToken, "title", JsonString(sTitle));
    NuiSetBind(oPlayer, nToken, "text", JsonString(sText));
}


void GNW_ShowMenuButton(object oPlayer)
{
    object oModule = GetModule();
    json jWindow = GetLocalJson(oModule, GNW_WINDOW_PREFIX + GNW_MENU_WINDOW);
    if (jWindow == JsonNull())
    {
        json jButton = NuiButtonSelect(JsonString("Menu"), NuiBind("btn_menu"));
             jButton = NuiId(jButton, "btn_menu");
             jButton = NuiMargin(jButton, 0.0f);
             jButton = NuiWidth(jButton, 72.0f);
             jButton = NuiHeight(jButton, 28.0f);
             jButton = NuiGroup(jButton, FALSE, NUI_SCROLLBARS_NONE);
             jButton = NuiMargin(jButton, 0.0f);

        jWindow = NuiWindow(jButton, JsonBool(FALSE), NuiBind(NUI_WINDOW_GEOMETRY_BIND), JsonBool(FALSE), JsonBool(FALSE), JsonBool(FALSE), JsonBool(TRUE), JsonBool(FALSE));
        SetLocalJson(oModule, GNW_WINDOW_PREFIX + GNW_INFO_WINDOW, jWindow);
    }

    int nToken = NuiCreate(oPlayer, jWindow, GNW_WINDOW_PREFIX + GNW_MENU_WINDOW);

    float fGuiScale = IntToFloat(GetPlayerDeviceProperty(oPlayer, PLAYER_DEVICE_PROPERTY_GUI_SCALE)) / 100.0f;
    float fX = IntToFloat(GetPlayerDeviceProperty(oPlayer, PLAYER_DEVICE_PROPERTY_GUI_WIDTH)) - (80.0f * fGuiScale);
    float fY = IntToFloat(GetPlayerDeviceProperty(oPlayer, PLAYER_DEVICE_PROPERTY_GUI_HEIGHT)) - (166.0f * fGuiScale);

    NuiSetBind(oPlayer, nToken, NUI_WINDOW_GEOMETRY_BIND, NuiRect(fX, fY, 80.0f, 36.0f));
}