dialeg={
    list = {},
}

-- dialegs = {}

-- function dialeg.update()
--     for i,d in ipairs(dialegs) do
--         if distancia(d.actor1, d.actor2, d.direccio)<=d.distancia and d.enabled then
--             dialeg.new(d.text, d.actor1)
--             d.enabled=false
--         end
--     end    
-- end

-- function dialeg.new( _text, _actor )
-- print(_text)
--     table.insert( 
--         dialeg.list, 
--         {
--             actor= _actor, 
--             text=_text, 
--             time = 120,
--             w = math.floor(4.6*#_text), 
--             h = 20,
--         } )
-- end

function dialeg.draw_globo(x, y, w, h, flip, text)
    draw.rrectf(x, y, w, h, 5, 2)
                
    if flip then
        draw.line(x+w, y+h-1+5, x+w-20, y+h-1, 2)
        draw.line(x+w, y+h-1+5, x+w-18, y+h-1, 2)
        draw.line(x+w, y+h-1+5, x+w-14, y+h-1, 2)
        draw.line(x+w, y+h-1+5, x+w-12, y+h-1, 2)
    else
        draw.line(x, y+h-1+5, x+20, y+h-1, 2)
        draw.line(x, y+h-1+5, x+18, y+h-1, 2)
        draw.line(x, y+h-1+5, x+14, y+h-1, 2)
        draw.line(x, y+h-1+5, x+12, y+h-1, 2)
    end
    
    local font_curr = font.current()
    font.current(font_default)
    draw.text(text, x+5,y+7, 16)
    font.current(font_curr)    
end

function dialeg.add(_actor, _text, _time)
    local _w = math.floor(4.6*#_text)
    table.insert(
        dialeg.list, 
        { actor= _actor, 
          text = _text,
          time = _time,
          w    = _w,
          h    = 20,
        }
    )
end

function dialeg.show(_x, _y, _w, flip, text, always_visible)
    local x = 0
    local y = 0
    
    y = _y-20
    if flip then
        x = _x-w
    else
        x = _x+_w
    end
    x, y = viewp:screen_coords( x, y )
    if always_visible then
        if x<0 then x=0 end
        if y<0 then y=0 end
    end
    
    local w = math.floor(4.6*#text)
    local h = 20
    dialeg.draw_globo(x, y, w, h, flip, text)
end

function dialeg.draw( )
    local i = 1
    while i <= #dialeg.list do
        local d = dialeg.list[i]
        
        if d.time>0 and d.actor then
            if viewp:inside(d.actor.x, d.actor.y, d.actor.w, d.actor.h) then
                local x = 0
                local y = 0
            
                y = d.actor.y-20
                if d.actor.flip then
                    x = d.actor.x-d.w
                else
                    x = d.actor.x+d.actor.w
                end
                
                x, y = viewp:screen_coords( x, y )
                
                -- draw.rrectf(x, y, d.w, d.h, 5, 2)
                -- 
                -- if d.actor.flip then
                --     draw.line(x+d.w, y+d.h-1+5, x+d.w-20, y+d.h-1, 2)
                --     draw.line(x+d.w, y+d.h-1+5, x+d.w-18, y+d.h-1, 2)
                --     draw.line(x+d.w, y+d.h-1+5, x+d.w-14, y+d.h-1, 2)
                --     draw.line(x+d.w, y+d.h-1+5, x+d.w-12, y+d.h-1, 2)
                -- else
                --     draw.line(x, y+d.h-1+5, x+20, y+d.h-1, 2)
                --     draw.line(x, y+d.h-1+5, x+18, y+d.h-1, 2)
                --     draw.line(x, y+d.h-1+5, x+14, y+d.h-1, 2)
                --     draw.line(x, y+d.h-1+5, x+12, y+d.h-1, 2)
                -- end
                
                -- local font_curr = font.current()
                -- font.current(font_default)
                -- draw.text(d.text, x+5,y+7, 16)
                -- font.current(font_curr)

                dialeg.draw_globo(x, y, d.w, d.h, d.actor.flip, d.text)
                
                d.time = d.time-1
            end
            i = i + 1
        else
            table.remove(dialeg.list, i)
        end
    end
end

return dialeg