-- Panel

Panel.map_button_callback = nil

----------------------------------------------------------------------------------------------------
function Panel:show(aState)
    Panel:get_element("panel"):setVisible(aState)
    Panel:get_element("task"):setTextColor(0, 0, 0, 255)

    if aState then
        addUpdateListener(ho)
        addUpdateListener(Hint)
    else
        removeUpdateListener(ho)
        removeUpdateListener(Hint)
    end
end

----------------------------------------------------------------------------------------------------
function Panel:set_ho_text(index, str)
	Panel:get_element("ho"..index):setText(str, true)
	Panel:set_ho_state(index, 1)
end

----------------------------------------------------------------------------------------------------
function Panel:set_task_text(str)

    if type(str) ~= "string" then return end
    
    if len_ids(str) > 35 then
        Panel:slide_task_panel(true)
    else
        Panel:slide_task_panel(false)
    end
    
	Panel:get_element("task"):setText(str, true)
end

----------------------------------------------------------------------------------------------------
function Panel:reset_pos()
    if Panel.mTaskPanelPosIdle == nil then
        return
    end
    Panel:get_element("task_panel"):setPos(Panel.mTaskPanelPosIdle)
end

----------------------------------------------------------------------------------------------------
function Panel:slide_task_panel(slide)

    if Panel.mTaskPanelPosIdle == nil then
        local pos = Panel:get_element("task_panel"):getPos()
        Panel.mTaskPanelPosIdle = Vec2(0,0)
        Panel.mTaskPanelPosIdle.x = pos.x
        Panel.mTaskPanelPosIdle.y = pos.y
        Panel.mIsSlide = false
    end
    
    if slide == Panel.mIsSlide then return end
    
    Panel.task_panel = Panel:get_element("task_panel")
    
    if slide then
        Panel.task_end_pos = Vec2(0,0)
        Panel.task_end_pos.x = Panel.mTaskPanelPosIdle.x
        Panel.task_end_pos.y = Panel.mTaskPanelPosIdle.y - 40
        Panel.mIsSlide = true
    elseif Panel.mIsSlide then
        Panel.task_end_pos.x = Panel.mTaskPanelPosIdle.x
        Panel.task_end_pos.y = Panel.mTaskPanelPosIdle.y
        Panel.mIsSlide = false
    end
    addUpdateListener(Panel)
end

----------------------------------------------------------------------------------------------------

function Panel:update(aElapsed)
    -- slide task panel
    local current_pos = Panel.task_panel:getPos()
    if Panel.mIsSlide then
        if current_pos.y <= Panel.task_end_pos.y then removeUpdateListener(Panel) end
        current_pos.y = current_pos.y - (30 * aElapsed)
    elseif not Panel.mIsSlide then
        if current_pos.y >= Panel.task_end_pos.y then removeUpdateListener(Panel) end
        current_pos.y = current_pos.y + (30 * aElapsed)
    end
    Panel.task_panel:setPos(current_pos)
end

----------------------------------------------------------------------------------------------------
function Panel:set_ho_state(index, state)
    --[[if (state ~= 0) then
        Panel:get_element("ho"..index):setAlpha(1)
    else
        Panel:get_element("ho"..index):setAlpha(0.5)
    end]]
end

----------------------------------------------------------------------------------------------------
function Panel:clear()
	for i = 1, 10 do
		Panel:set_ho_text(i, "")
	end
end

----------------------------------------------------------------------------------------------------
function Panel_UPDATE()
	return 0
end

----------------------------------------------------------------------------------------------------
function Panel_INIT()
	Panel:clear()
	Panel:get_element("mix_panel"):setVisible(false)

    Panel:set_element_eh("btn_map",
        function()
            if not HODialogs:isVisible() and not HOLevel.locked then
                if HOLevel.ExitTarget ~= "" then
					if Panel.map_button_callback then Panel.map_button_callback() Panel.map_button_callback = nil end
                    level_mgr:switch_to_level(HOLevel.ExitTarget)
                    HOLevel.ExitTarget = ""
                else
                    level_mgr:switch_to_level("Map")
                end
                fade_panel:fade_out()
            end
        end)
    Panel:set_element_eh("btn_hint", function() Hint:onClick() end)
    Panel:set_element_eh("btn_menu",
        function()
            Panel:onPauseClick()
        end)
end

function Panel:onPauseClick()
    if not HODialogs:isVisible() and not HOLevel.locked then
        HODialogs:show(true)
        HODialogs:switchScreen("menu")
        return true
    end

    return false
end

----------------------------------------------------------------------------------------------------
function Panel:getInventorySlot(aSlot)
    return Panel:get_element("inventory_anchor_"..aSlot)
end

----------------------------------------------------------------------------------------------------
function Panel:getHOListRecord(aIndex)
    return Panel:get_element("ho"..aIndex)
end

----------------------------------------------------------------------------------------------------
function Panel:setAvatarClickHandler(func)
    Panel:set_element_eh('robin_avatar', func)
end

----------------------------------------------------------------------------------------------------
function Panel:showMix(aState, aTime)
    local tMixPanel = Panel:get_element("mix_panel")

    if aState then
        local step_count = 40
        local step_time = aTime / step_count
        tMixPanel:setVisible(true)
        tMixPanel:setAlpha(0.0)
        local step = 0
        timer_mgr:add_timer(
            function()
                step = step + 1
                tMixPanel:setAlpha(step / step_count)
            end, step_time, step_count)

    else
        tMixPanel:setActive(false)
        local step_count = 40
        local step_time = aTime / step_count
        local step = 0
        timer_mgr:add_timer(
            function()
                step = step + 1
                if step == step_count then
                    tMixPanel:setVisible(false)
                else
                    tMixPanel:setAlpha(1.0 - step / step_count)
                end
            end, step_time, step_count)
    end
end

----------------------------------------------------------------------------------------------------
function Panel:setMixCallback(aCallback)
    Panel:get_element("mix_panel"):setActive(true)
    Panel:set_element_eh("mix_panel", aCallback)
end

----------------------------------------------------------------------------------------------------
function Panel:getMixAnchorPos()
    local anch = Panel:get_element("mix_anchor")
    local pos = anch:getPos(true)
    local loc = Vec2(pos.x, pos.y)
    return loc
end

----------------------------------------------------------------------------------------------------
function Panel:getMixAnchor()
    return Panel:get_element("mix_anchor")
end

----------------------------------------------------------------------------------------------------
function Panel:setMixItemName(aName)
    return Panel:get_element("mix_item_name"):setText(aName)
end

----------------------------------------------------------------------------------------------------
function Panel:showTimer(aState)
    return Panel:get_element("timer"):setVisible(aState)
end

----------------------------------------------------------------------------------------------------
function Panel:updateTimer(aTime)
    if aTime < 0 then
        aTime = 0.0
    end

    local sec = math.floor(aTime)

    local min_dec = math.floor(sec / 600)
    sec = sec - min_dec * 600

    local min = math.floor(sec / 60)
    sec = sec - min * 60

    local sec_dec = math.floor(sec / 10)
    sec = sec - sec_dec * 10

    Panel:play_clip('min_dec', {frame_begin = min_dec, frame_end = min_dec})
    Panel:play_clip('min', {frame_begin = min, frame_end = min})
    Panel:play_clip('sec_dec', {frame_begin = sec_dec, frame_end = sec_dec})
    Panel:play_clip('sec', {frame_begin = sec, frame_end = sec})
end

----------------------------------------------------------------------------------------------------
function Panel:showRobinMessage(aText, aTime)
    if Panel.mRobinBubble ~= nil then
        return
    end

    local RobinBubble = {}
    RobinBubble.mDisplay = Panel:get_element('robin_bubble')
    RobinBubble.mCloseButton = Panel:get_element('robin_msg_close')
    RobinBubble.mTextElem = Panel:get_element('robin_bubble_text')

    RobinBubble.mTextElem:setText(aText)
    --RobinBubble.mTextElem:setTextColor(0, 0, 0, 255)

    RobinBubble.mTime = aTime

    function RobinBubble:update(aElapsed)
        RobinBubble.mTextElem:updateTextColor()
        if RobinBubble.mTime ~= nil and RobinBubble.mTime > 0.0 then
            RobinBubble.mTime = RobinBubble.mTime - aElapsed
            if RobinBubble.mTime <= 0.0 then
                RobinBubble:close()
            end
        end
    end

    function RobinBubble:close()
        Media:soundEvent('bubble.close')
        RobinBubble.mCloseButton:setActive(false)
        --RobinBubble.mTextElem:setTextColor(255, 255, 255, 0)
        RobinBubble.mTextElem:updateTextColor()
        set_frame_listener(RobinBubble.mDisplay, 'bubble_close', function()
            removeUpdateListener(RobinBubble)
            Tutorial:sendMessage('remark.closed')
        end)
        play_clip(RobinBubble.mDisplay:getCharacter(), {label_begin = 'bubble_open', label_end = 'bubble_close'})
        Panel.mRobinBubble = nil
    end

    Panel:set_element_eh('robin_msg_close', function() RobinBubble:close() end)
    addUpdateListener(RobinBubble)
    play_clip(RobinBubble.mDisplay:getCharacter(), {frame_begin = 0, label_end = 'bubble_open'})
    RobinBubble.mCloseButton:setActive(true)

    Panel.mRobinBubble = RobinBubble
end

----------------------------------------------------------------------------------------------------
function Panel:closeRobinMessage()
    if Panel.mRobinBubble ~= nil then
        Panel.mRobinBubble:close()
    end
end
