-- EditTextElem

EditTextElem = {}
local BLINK_TIME = 0.35

-- params: filter = {<charCode1>, <charCode2>, ..}
--         max_length = <number>
function EditTextElem:new(aTextElem, aParams)
    local obj = {}
	setmetatable(obj, self)
	self.__index = self

    obj.mElem = aTextElem

    obj.filter = {13, 27, 9}
    if aParams ~= nil and aParams.filter ~= nil then
        for i = 1, #aParams.filter do
            table.insert(obj.filter, aParams.filter[i])
        end
    end

    obj.mMaxLength = 16
    if aParams ~= nil and aParams.max_length ~= nil then
        obj.mMaxLength = aParams.max_length
    end

    if aParams ~= nil and aParams.enterCall ~= nil then
        obj.enterCall = aParams.enterCall
    end

    obj.mSymbols = {}
    obj.mElem:setText('')
    obj.mBlinkTimer = BLINK_TIME
    obj.mBlinkVisible = false

    return obj
end

function EditTextElem:input(aKey)
    trace(self.mElem:getName()..': char '..aKey.char..'; scan '..aKey.scan..';')

    local durty = true
    if aKey.scan == SDLK_RETURN then
        self.enterCall()
        return
    end

    if aKey.scan == SDLK_BACKSPACE then
        if Bubble:isVisible() then
            Bubble:hide(false)
        end
        table.remove(self.mSymbols, #self.mSymbols)
    elseif aKey.char ~= 0 then
        local filtered = false
        durty = false
        for i = 1, #self.filter do
            if aKey.char == self.filter[i] then
                filtered = true
            end
        end

        --local pos = self.mElem:getPos(true)
        if filtered then
            self:notify('ids_filtered_chars')
            --Bubble:show('ids_filtered_chars', pos.x + 10, pos.y + 5, 200, 150, Bubble.TYPE_DOWN_RIGHT, true)
        elseif #self.mSymbols >= self.mMaxLength then
            self:notify('ids_max_chars')
            --Bubble:show('ids_max_chars', pos.x + 10, pos.y + 5, 200, 150, Bubble.TYPE_DOWN_RIGHT, true)
        else
            if Bubble:isVisible() then
                Bubble:hide(true)
            end
            table.insert(self.mSymbols, aKey.char)
            durty = true
        end
    else
        durty = false
    end

    if durty then
        local text = ''
        for i = 1, #self.mSymbols do
            text = text..'u'..self.mSymbols[i]
        end
        self.mElem:setUnicodeSumbols(text)
    end
end

function EditTextElem:update(aElapsed)
    self.mBlinkTimer = self.mBlinkTimer - aElapsed
    if self.mBlinkTimer <= 0.0 then
        self.mBlinkTimer = BLINK_TIME

        local text = ''
        for i = 1, #self.mSymbols do
            text = text..'u'..self.mSymbols[i]
        end
        self.mBlinkVisible = not self.mBlinkVisible

        if self.mBlinkVisible and #self.mSymbols < self.mMaxLength then
            text = text..'u95'
        end

        self.mElem:setUnicodeSumbols(text)
    end
end

function EditTextElem:reset()
    self.mSymbols = {}
    self.mElem:setText('')
end

function EditTextElem:getText()
    local text = ''

    for i = 1, #self.mSymbols do
        text = text..'u'..self.mSymbols[i]
    end

    return text
end

function EditTextElem:notify(message)
    local pos = self.mElem:getPos(true)
    Bubble:show(message, pos.x + 10, pos.y + 5, 200, 150, Bubble.TYPE_DOWN_RIGHT, true)
end
