Hint = {}

Hint.TIME = 10.0

Hint.mList = {}
Hint.mTimer = Hint.TIME


--------------------------------------------------------------------------------------------------------
--- aElementAssist подсвечивается в случае невидимости aParent
function Hint:addElement(aElem, aParent, aElementAssist)
    --trace('add hint: '..aElem:getName())
    table.insert(Hint.mList, {name = aElem:getName(), elem = aElem, parent = aParent, assist = aElementAssist})
end

--------------------------------------------------------------------------------------------------------
function Hint:removeElement(aElem)
    for i = 1, #Hint.mList do
        if Hint.mList[i].name == aElem:getName() then
            trace('remove hint: '..aElem:getName())
            table.remove(Hint.mList, i)
            --Hint.mList[aElem:getName()] = nil
            return
        end
    end
end

--------------------------------------------------------------------------------------------------------
function Hint:onClick()

    if HODialogs:isVisible() then
        return
    end

    if Hint.mTimer > 0 then
        return
    end

    -- сначала ищем просто активные хиды
    local tElem = nil
    for key, val in pairs(Hint.mList) do
        if val.elem:isActive() and val.elem:getName():find("hid_") then
            tElem = val.elem
            break
        end
    end

    if tElem == nil then
        for key, val in pairs(Hint.mList) do
            if val.elem:getName():find("hid_") and val.assist ~= nil then
                tElem = val.assist
                break
            end
        end
    end

    if tElem == nil then
        for key, val in pairs(Hint.mList) do
            if val.elem:getName():find("int_") then
                tElem = val.elem
                break
            end
        end
    end

    if tElem == nil then
        for key, val in pairs(Hint.mList) do
			if val.elem:isVisible() then
				tElem = val.elem
				break
			end
        end
    end

    if tElem == nil then
        return
    end

    --trace(tElem:getName())
    Media:soundEvent('hint.activate')
    start_effect_at("hint", tElem:getPos(true))
    Hint.mTimer = Hint.TIME
    
    Tutorial:sendMessage('hint.pressed')
end

--------------------------------------------------------------------------------------------------------
function Hint:reset()
    Hint.mTimer = Hint.TIME
    Hint.mList = {}
end

--------------------------------------------------------------------------------------------------------
function Hint:update(aElapsed)

    if HODialogs:isVisible() then
        return
    end

    if Hint.mTimer > 0.0 then
        Hint.mTimer = Hint.mTimer - aElapsed
        local frame = math.floor((1.0 - Hint.mTimer / Hint.TIME) * 7.0)
        if Hint.mTimer <= 0.0 then
            frame = 7
            Media:soundEvent('hint.charged')
        end
        Panel:play_clip('btn_hint', {frame_begin = frame, frame_end = frame})
    end
end
