#include "common_components.lua"

newKeyDialogFade = 0.0
existingKeyDialogFade = 0.0

existinKeyWarningParams = {}

existingKeyDialogState =
{
    currentActionName = nil,
    conflictingInfo = nil,
    requestShow = false,
    requestHide = false,
    isClosed = true,
    parentDialog = nil,
    onAccept = function(dialog) end,

    onClose = function(self)
    end,

    show = function(self)
        local readableName = GetTranslatedStringByKey(self.conflictingInfo.locKey.."_UPPERCASE")
        existinKeyWarningParams.details = "loc@UI_THE_KEY_IN_USE_DETAILS," .. "\"" .. readableName .. "\""
        self.requestShow = true
    end,

    hide = function(self)
        self.requestHide = true
    end,

    updateLogic = function(self)
        if InputPressed("menu_cancel") then
            self.parentDialog:hide()
        end
    end,

    update = function(self)
        if self.requestShow then
            SetValue('existingKeyDialogFade', 1, "linear", 0.1)
            self.requestShow = false
            self.isClosed = false
        end
        
        if self.requestHide then
            SetValue('existingKeyDialogFade', 0, "linear", 0.1)
            self.requestHide = false
        end

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

        local drawObj = 
        {
            state = self,

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

                    UiModalBegin(true)
                    WarningView:draw(existinKeyWarningParams)

                    self.state:updateLogic()

                    UiModalEnd()
                UiPop()
            end
        }

        UiDrawLater(drawObj)
    end
}

newKeyDialogState =
{
    requestShow = false,
    requestHide = false,
    currentCallerInfo = nil,
    onLogicUpdate = function(src) end,
    isClosed = true,
    noCancelButton = false,

    title = "",
    note = "",

    onClose = function(self)
        UiForceFocus(self.currentCallerInfo.focusId)
    end,

    show = function(self)
        self.requestShow = true
    end,

    hide = function(self)
        self.requestHide = true
        existingKeyDialogState:hide()
    end,

    updateLogic = function(self)
        if not existingKeyDialogState.isClosed then
            return
        end

        self.onLogicUpdate(self)
    end,

    update = function(self)
        -- will be written in new_key_dialog_view.lua
    end
}

cancelNewKeyButtonParams = 
{
    text = "loc@UI_BUTTON_CANCEL",

    onPressed = function(self)
        newKeyDialogState:hide()
    end,
}


existinKeyWarningParams = 
{
    note = "loc@UI_THE_KEY_IN_USE_WARNING",
    details = "loc@UI_THE_KEY_IN_USE_DETAILS",

    cancelButton = 
    {
        text = "loc@UI_BUTTON_CANCEL",

        onPressed = function(self)
            newKeyDialogState:hide()
        end,
    },

    acceptButton = 
    {
        text = "loc@UI_BUTTON_ACCEPT",

        onPressed = function(self)
            existingKeyDialogState.onAccept(existingKeyDialogState)
            newKeyDialogState:hide()
        end,
    }
}




