-- Modulo per creare una taskbar con icone
local p = {} -- per l'esportazione delle funzioni del modulo


function p.Taskbar(frame)
    local args
    -- Se chiamata mediante  #invoke, usa gli argomenti passati al template invocante.
    -- Altrimenti a scopo di test assume che gli argomenti siano passati direttamente
    if frame == mw.getCurrentFrame() then
        args = frame:getParent().args
    else
        args = frame
    end
    local nbox = 0
    local finito = false
    local celle = {}
    while not finito do
        local pos = nbox*6+1
        local cella = {}
        if args[pos] then
            table.insert(cella, args[pos])
            table.insert(cella, args[pos+1] or '')
            table.insert(cella, args[pos+2] or '')
            table.insert(cella, args[pos+3] or '18px')
            table.insert(cella, args[pos+4] or '')
            table.insert(cella, args[pos+5] or '')
            table.insert(celle, cella)
            nbox = nbox + 1
        else
            finito = true
        end
    end
    local width = tostring(math.floor(100 / nbox)-1) .. "%" --larghezza delle celle
    --creo la tabella
    local root = mw.html.create('table')
    root
        .addClass("wikitable")
        .cssText('float:center;clear;none;margin:auto;text-align:center;widt:100%') 
        .attr('cellspacing', 0)
        .tag('caption')
            .wikitext(args.argomento or 'Di cosa vuoi parlare oggi?')
            .css('font-size', '120%')
    local row=root.tag('tr')

    for i = 1, nbox do
        link, _, immagine, dimensione, _, stile =unpack(celle[i])
        row.tag('td')
            .css('border-bottom', 'none')
            .cssText(stile)
            .attr('width', width)
            .wikitext(mw.ustring.format('[[File:%s|%s|link=Discussioni progetto:%s|%s]]', immagine, dimensione, link, link)) 
    end
    row = root.tag('tr')
    for i = 1, nbox do
        link, etichetta, _, _, sottotitolo, stile =unpack(celle[i])
        local td= row.tag('td')
            .css('border-top', 'none')
            .css('text-align', 'center')
            .cssText(stile)
        if etichetta ~='' then
            td.wikitext(mw.ustring.format('<span style="font-size:120%;font-weight:bold;">[[Discussioni progetto:%s|%s]]</span>',
                                        link, etichetta))
            if sottotitolo ~= '' then
                td.wikitext('<br />')
            end
        end
        if sottotitolo ~= '' then 
            td.wikitext(mw.ustring.format('<span style="font-size:80%;">%s</span>', sottotitolo))
        end 
    end
    return tostring(root)
end

return p