----------------------------------------------------------------------------------------------------
---Bubble = {}

--Bubble.mDisplay = level_mgr:get_layer("Bubble")
local ARROW_OFFSET = 24
local FRAME_OFFSET = 24

Bubble.TYPE_DOWN_RIGHT = 0
Bubble.TYPE_RIGHT_DOWN = 1
Bubble.TYPE_DOWN_LEFT = 2
Bubble.TYPE_LEFT_DOWN = 3
Bubble.TYPE_RIGHT_UP = 4
Bubble.TYPE_UP_RIGHT = 5
Bubble.TYPE_LEFT_UP = 6
Bubble.TYPE_UP_LEFT = 7
Bubble.TYPE_NORMAL = 8

Bubble.visible = false


----------------------------------------------------------------------------------------------------
function Bubble_INIT()

    Bubble:get_element("bubble_text"):setTextColor(0, 0, 0, 255)
    Bubble:set_element_eh('close', function()
            Bubble:hide(true)
            Tutorial:onBubbleClose()
        end)

    Bubble:hide(false)
    --Bubble:show('ids_bubble_sample', 500, 400, 253, 152, Bubble.TYPE_LEFT_DOWN, true)
end

----------------------------------------------------------------------------------------------------
function Bubble_UPDATE()
	return 0
end

function setFrame(elem, frame)
    play_clip(elem:getCharacter(), {frame_begin = frame, frame_end = frame})
end

----------------------------------------------------------------------------------------------------
function Bubble:show(text, x, y, width, height, type, fade_in, close_cb)

    if Bubble.visible then
        Bubble:hide(false)
    end

    Bubble:set_visible(true)
    Bubble:set_active(true)

    Bubble.visible = true

    local sect_ul = Bubble:get_element('bubble_ul')
    local sect_u = Bubble:get_element('bubble_u')
    local sect_ur = Bubble:get_element('bubble_ur')
    
    local sect_l = Bubble:get_element('bubble_l')
    local sect_c = Bubble:get_element('bubble_c')
    local sect_r = Bubble:get_element('bubble_r')

    local sect_dl = Bubble:get_element('bubble_dl')
    local sect_d = Bubble:get_element('bubble_d')
    local sect_dr = Bubble:get_element('bubble_dr')

    setFrame(sect_ul, 0)
    setFrame(sect_ur, 0)
    setFrame(sect_dl, 0)
    setFrame(sect_dr, 0)

    sect_ur:setPos(Vec2(width, 0))
    sect_dl:setPos(Vec2(0, height))
    sect_dr:setPos(Vec2(width, height))

    sect_r:setPos(Vec2(width, sect_r:getPos().y))
    sect_d:setPos(Vec2(sect_d:getPos().x, height))

    local border_width = 57
    local center_width = width - border_width
    local center_height = height - border_width

    sect_u:setScaleSeparated(1.0, 1.0)
    xscale = center_width / sect_u:getSize().x
    sect_u:setScaleSeparated(xscale, 1.0)

    sect_d:setScaleSeparated(1.0, 1.0)
    xscale = center_width / sect_d:getSize().x
    sect_d:setScaleSeparated(xscale, 1.0)

    sect_l:setScaleSeparated(1.0, 1.0)
    yscale = center_height / sect_l:getSize().y
    sect_l:setScaleSeparated(1.0, yscale)

    sect_r:setScaleSeparated(1.0, 1.0)
    yscale = center_height / sect_r:getSize().y
    sect_r:setScaleSeparated(1.0, yscale)

    sect_c:setScaleSeparated(1.0, 1.0)
    xscale = center_width / sect_c:getSize().x
    yscale = center_height / sect_c:getSize().y
    sect_c:setScaleSeparated(xscale, yscale)

    local close = Bubble:get_element('close_display')
    --close:setPos(Vec2(sect_ur:getPos().x + 18, close:getPos().y))
    xscale = (width + border_width / 6) / 250.0
    yscale = (height + border_width / 6) / 160.0
    close:setScaleSeparated(xscale, yscale)

    local text_elem = Bubble:get_element('bubble_text')
    text_elem:setText('')
    text_elem:setText(text)
    text_elem:setScale(1.0, 1.0)
    xscale = sect_dr:getPos().x / 243.0
    yscale = sect_dr:getPos().y / 152.0
    text_elem:setScale(xscale, yscale)

    --close:setPos(Vec2(sect_ur:getPos().x + 17, close:getPos().y))
 
    --sect_d:setScaleSeparated(xscale, 1.0)
    --sect_l:setScaleSeparated(1.0, yscale)
    --sect_r:setScaleSeparated(1.0, yscale)
    --sect_c:setScaleSeparated(xscale, yscale)

    local abs_x = x
    local abs_y = y

    if type == Bubble.TYPE_DOWN_RIGHT then
        setFrame(sect_ul, 2)
        abs_x = x - FRAME_OFFSET

    elseif type == Bubble.TYPE_RIGHT_DOWN then
        setFrame(sect_ul, 1)
        abs_y = y - FRAME_OFFSET

    elseif type == Bubble.TYPE_DOWN_LEFT then
        setFrame(sect_ur, 2)
        abs_x = x - (border_width - FRAME_OFFSET) - width

    elseif type == Bubble.TYPE_LEFT_DOWN then
        setFrame(sect_ur, 1)
        abs_x = x - border_width - width
        abs_y = y - FRAME_OFFSET

    elseif type == Bubble.TYPE_RIGHT_UP then
        setFrame(sect_dl, 1)
        abs_y = y - (border_width - FRAME_OFFSET) - height

    elseif type == Bubble.TYPE_UP_RIGHT then
        setFrame(sect_dl, 2)
        abs_x = x - FRAME_OFFSET
        abs_y = y - border_width - height

    elseif type == Bubble.TYPE_LEFT_UP then
        setFrame(sect_dr, 1)
        abs_x = x - border_width - width
        abs_y = y - (border_width - FRAME_OFFSET) - height

    elseif type == Bubble.TYPE_UP_LEFT then
        setFrame(sect_dr, 2)
        abs_x = x - (border_width - FRAME_OFFSET) - width
        abs_y = y - border_width - height

    end

    Bubble:get_element('bubble'):setPos(Vec2(abs_x, abs_y))
    Bubble:get_element('bubble'):setVisible(true)

    if fade_in then
        Bubble:get_element('bubble'):setAlpha(0.0)
        text_elem:updateTextColor()
        local step = 0
        timer_mgr:add_timer(
            function()
                step = step + 1
                Bubble:get_element('bubble'):setAlpha(step / 30)
                text_elem:updateTextColor()
                if step == 30 then
                    Bubble:get_element('close'):setActive(true)
                end
            end, 0.01, 30)
    else
        Bubble:get_element('bubble'):setAlpha(1.0)
        Bubble:get_element('close'):setActive(true)
    end

    Bubble.onClose = close_cb
end

----------------------------------------------------------------------------------------------------
function Bubble:hide(fade_out)
    Bubble.visible = false

    if fade_out then
        Bubble:get_element('bubble'):setAlpha(1.0)
        step = 10
        timer_mgr:add_timer(
            function()
                step = step - 1
                if step > 0 then
                    Bubble:get_element('bubble'):setAlpha(step / 10)
                else
                    Bubble:get_element('bubble'):setVisible(false)
                    if Bubble.onClose ~= nil then
                        Bubble.onClose()
                    end
                end
                
                Bubble:get_element('bubble_text'):updateTextColor()
            end, 0.01, 10)
        Media:soundEvent('bubble.close')
    else
        Bubble:get_element('bubble'):setVisible(false)
        if Bubble.onClose ~= nil then
            Bubble.onClose()
        end
    end

    Bubble:get_element('close'):setActive(false)

    Bubble:set_visible(false)
    Bubble:set_active(false)
end

----------------------------------------------------------------------------------------------------
function Bubble:isVisible()
    return Bubble.visible
end
