require "scripts/style.lua"

function ShowQuitConfirmDlg()
    if (kQuitConfirmShowed == false) and (kFaceBookOverrides == false) then
        kQuitConfirmShowed = true;
        
        EnableWindow("pnl_water_barrel_rollover", false);
        EnableWindow("pnl_fountain_rollover", false);
        EnableWindow("pnl_gates_rollover", false);
        EnableWindow("pnl_magic_object_rollover", false);
        EnableWindow("pnl_storage_rollover", false);
        
        SetCursorAllowed(true);
        
        if GetCount("lvl_gates") ~= 6 then
            if (IsDay ~= nil) then
                GM_StartPersistentAction("UIClick");
                if (IsDay()) then
                    GM_StartPersistentAction("GameClick");
                else
                    if not kDialogActive then
                        GM_StartPersistentAction("ShopClick");
                    end;
                    if GetCount("pnl_sell_screen_state") > 0 then
                        GM_StartPersistentAction("PotionClick");
                    end;
                end;
            end;
        end;
        
        if not kPauseDlgShowAllowed and not kOptionsShowed then
            EnableWindow("pausemenu", false);
        end;
        if kOptionsShowed then
            EnableWindow("optionsmenu", false);
        end;

        SuspendFXPool("");
        ResumeFXPool("yesno");
        ResumeFXPool("menucursor");

        PauseTimer("ui", "UI");
        PauseTimer("panels", "UI");
        PauseTimer("pnl_pause", "UI");
        HM_HideHint();
        
        if (GetCount("pnl_refill_state") == 1) and kPauseDlgShowAllowed then
            EnableWindow("refill_water", false);
            HM_SetMouseProcessing(false);
        end;
        
        retval = DisplayDialog{
            "scripts/dlg_yesno.lua",
            title = "quittitle",
            body = "quitconfirm",
            dlg_name = "quit_confirm",
            image = "interface/dlg_yes_no/quit_game",
            --scale = 2000,
            timer = {
                name = "pnl_quit",
                category = "quit",
            },
        };
        if (GetCount("pnl_refill_state") == 1) and kPauseDlgShowAllowed then
            EnableWindow("refill_water", true);
            if GetCount("water_cant_upgrade") == 0 then
                GM_StartPersistentAction("RefillWaterClick");
            else
                GM_StartPersistentAction("NullClick");
            end;
            HM_SetMouseProcessing(true);
        end;
        
        if not kPauseDlgShowAllowed and not kOptionsShowed then
            EnableWindow("pausemenu", true);
        end;
        if kOptionsShowed then
            EnableWindow("optionsmenu", true);
        end;

        
        if (retval == "yes") then
            SwapToModal("scripts/quit.lua");
            --PostMessage(CreateNamedMessage(kQuitNow, "quit"));
        else
            ResumeFXPool("");
            SuspendFXPool("yesno");

            if EffectRunning ~= nil then
                if not EffectRunning() then
                    if (GetCount("pnl_refill_state") == 0) and (GetCount("pnl_pause_state") == 0) then
                        StartTimer("ui", "UI");
                        StartTimer("panels", "UI");
                        StartTimer("pnl_pause", "UI");
                        if not kSkipTutorialShowed then
                            StartTimer("game", "game");
                        end;
                    end;
                end;
            else
                StartTimer("ui", "UI");
                StartTimer("panels", "UI");
                StartTimer("pnl_pause", "UI");
                if not kSkipTutorialShowed then
                    StartTimer("game", "game");
                end;
            end
            PlaySound("audio/sfx/main_menu/screenswitch.ogg");
            if ((kPauseDlgShowAllowed == true) or (GetCount("pnl_sell_screen_state") == 2)) and not kSkipTutorialShowed then
                HM_RestoreHint();
            end;
            if (RestoreEatenCursorState ~= nil) and not kSkipTutorialShowed and 
                (GetCount("pnl_pause_state") == 0) and (GetCount("hint_attack_scorpion") ~= 1) 
            then
                RestoreEatenCursorState()
            end;
        end;

        kQuitConfirmShowed=false;
    end;
end;
