#include "new_key_dialog_logic.lua"
#include "ui/ui_extensions.lua"

function drawNewKeyDialog(title, note)
    -- draw frame F535_1179_DialogForNewKey
    UiPush()
        local bgw = 440
        local bgh = newKeyDialogState.noCancelButton and 180 or 230
        local padding = 10

        UiTranslateToScreenCenter(-bgw / 2, 0)
        UiTranslateToScreenMiddle(0, -bgh / 2)

        UiWindow(bgw, bgh)

        UiAlign("top left")

        UiColorFilter(1.0, 1.0, 1.0, 1)
        UiPush()
            UiColor(1, 1, 1, 1)
            UiImageBox("common/rect_c#000_o0.55_cr8.png", bgw, bgh, 9, 9)
        UiPop()

        UiPush()
            UiAlign("top center")
            local _, th = GetTextSizeWithFont(GetTranslatedStringByKey(note), "regular.ttf", 27)
            UiTranslate(bgw / 2, bgh / 2 - th / 2 + (newKeyDialogState.noCancelButton and 20 or 0))
            UiColor(1.0, 1.0, 1.0, 0.8)
            UiFont("regular.ttf", 27)
            UiWordWrap(bgw - 2 * padding)
            UiTextAlignment("center")
            UiText(note)
        UiPop()

        UiPush()
            UiAlign("top center")
            UiTranslate(bgw / 2, 35)
            UiColor(1.0, 1.0, 1.0, 1)
            UiFont("bold.ttf", 36)
            UiWordWrap(bgw -  2 * padding)
            UiTextAlignment("center")
            UiText(title)
        UiPop()
        
        if not newKeyDialogState.noCancelButton then
            UiPush()
                UiAlign("top center")
                UiButtonImageBox("common/rect_c#d9d9d9_o1.00_sw1.50_sc#ffffff_so1.00_cr4.png", 6, 6, 1, 1, 1)
                UiTranslate(bgw / 2, 161.25)
                UiColor(1.0, 1.0, 1.0, 1)
                UiButtonHoverColor(1, 1, 0.5, 1)
                UiFont("regular.ttf", 25)
                if UiTextButton(cancelNewKeyButtonParams.text, 181.5, 39.5) then
                    cancelNewKeyButtonParams:onPressed()
                end
             UiPop()
        end
    UiPop()
end

newKeyDialogState.update = function(self)
        if self.requestShow then
            SetValue('newKeyDialogFade', 1, "linear", 0.1)
            self.requestShow = false
            self.isClosed = false
            InputClear()
        end

        if self.requestHide then
            SetValue('newKeyDialogFade', 0, "linear", 0.1)
            self.requestHide = false
        end

        if newKeyDialogFade == 0 then
            if self.isClosed == false then
                self.isClosed = true
                self:onClose()
            end
            return
        end

        local drawObj = 
        {
            state = self,

            draw = function(self)
                UiPush()
                    UiBlur(newKeyDialogFade)
                    UiColorFilter(1, 1, 1, newKeyDialogFade)

                    UiModalBegin(true)
                    drawNewKeyDialog(self.state.title, self.state.note)

                    self.state:updateLogic()

                    UiModalEnd()
                UiPop()
            end
        }

        UiDrawLater(drawObj)

        existingKeyDialogState:update()
    end