local p = {}
function p.pesquisa(frame)
local args = frame.args
local output = ""

for key, value in pairs(args) do
local items = mw.text.split(value, ",")
if #items == 4 then
local barrinha = frame:expandTemplate{ title = 'barrinha', args = { items[3], '200px', '#{{CorBRPartido|' .. items[2] .. '}}', items[4] } }
    output = output .. '<tr>\n'
    output = output .. '<td style="font-size:120%">' .. items[1] .. '</td>\n'
    output = output .. '<td>' .. items[2] .. '</td>\n'
    output = output .. '<td>' .. barrinha .. '</td>\n'
    output = output .. '</tr>\n'
    end
end

    return output
end

function p.camara(frame)
    local args = frame.args
    local total = tonumber(args.total)
    local output = ""
    
    output = output .. '<table style="width:50%; text-align:center;">\n'
    output = output .. '<caption style="font-size:150%; font-weight:bold;">Câmara</caption>\n'
    
    for key, value in pairs(args) do
        if key ~= "total" then 
        local items = mw.text.split(value, ",")
            if #items == 2 then 
        local A = items[1]
        local B = tonumber(items[2])
        local porcentagem = (B / total) * 100
        local barrinha = frame:expandTemplate{
        title = 'barrinha', 
        args = { string.format("%.2f", porcentagem), '200px', '#{{CorBRPartido|' .. A .. '}}', 'transparent;font-size:1%' }
    }
                
    output = output .. '<tr>\n'
    output = output .. '<td style="font-size:120%; font-weight:bold;">' .. A .. '</td>\n'
    output = output .. '<td>' .. barrinha .. '</td>\n'
    output = output .. '<td>' .. B .. '</td>\n'
    output = output .. '</tr>\n'
        end
    end
end
    
    output = output .. '<tr style="font-size:80%; text-align:right;"><td colspan="3">Total: ' .. total .. '</td></tr>\n'
    output = output .. '</table>\n'
    return output
end

function p.prefeito(frame)
    local args = frame.args
    local output = ""

    output = output .. '<table style="width:50%; text-align:center;">\n'
    output = output .. '<caption style="font-size:150%; font-weight:bold;">Prefeito(a)</caption>\n'
    output = output .. '<tr style="font-size:80%;">\n'
    output = output .. '<td colspan="3">Candidato e partido</td>\n'
    output = output .. '<td colspan="2">Total votos (%)</td>\n'
    output = output .. '</tr>\n'

for i = 1, 25 do
local candidate = args[tostring(i)]
    if candidate then
    local items = mw.text.split(candidate, ",")
    if #items == 4 then
    output = output .. '<tr>\n'
    output = output .. '<td style="font-size:120%; font-weight:bold;">' .. items[1] .. '</td>\n'
    output = output .. '<td>' .. items[2] .. '</td>\n'
    output = output .. '<td>' .. items[3] .. '</td>\n'
    output = output .. '<td>' .. items[4] .. '</td>\n'
    output = output .. '</tr>\n'
         end
    end
end
    
    output = output .. '<tr class="b-dark" style="background:#f1f1f1;">\n'
    output = output .. '<td style="font-weight:500">VÁLIDOS</td>\n'
    output = output .. '<td style="font-weight:500">BRANCOS</td>\n'
    output = output .. '<td style="font-weight:500">NULOS</td>\n'
    output = output .. '<td></td>'
    output = output .. '</tr>\n'
    
local finalData = args.final
    if finalData then
        local itemsFinal = mw.text.split(finalData, ",")
        if #itemsFinal == 4 then
        output = output .. '<tr>\n'
        output = output .. '<td>' .. itemsFinal[1] .. '</td>\n'
        output = output .. '<td>' .. itemsFinal[2] .. '</td>\n'
        output = output .. '<td>' .. itemsFinal[3] .. '</td>\n'
        output = output .. '<td>' .. itemsFinal[4] .. '</td>\n'
        output = output .. '</tr>\n'
        end
    end
    
    output = output .. '</table>\n'

    return output
end

function p.vereador(frame)
    local args = frame.args
    local output = ""

    output = output .. '<table style="width:50%; text-align:center;">\n'
    output = output .. '<caption style="font-size:150%; font-weight:bold;">Vereadores</caption>\n'
    output = output .. '<tr style="font-size:80%; color:#555;">\n'
    output = output .. '<td colspan="2">Candidato e partido</td>\n'
    output = output .. '<td colspan="2">Total votos (%)</td>\n'
    output = output .. '</tr>\n'

for i = 1, 50 do
    local candidate = args[tostring(i)]
    if candidate then
    local items = mw.text.split(candidate, ",")
    if #items == 4 then
        output = output .. '<tr>\n'
        output = output .. '<td style="font-size:120%; font-weight:bold;">' .. items[1] .. '</td>\n'
        output = output .. '<td style="color:#555">' .. items[2] .. '</td>\n'
        output = output .. '<td>' .. items[3] .. '</td>\n'
        output = output .. '<td>(' .. items[4] .. '%)</td>\n'
        output = output .. '</tr>\n'
        end
    end
end
    
    output = output .. '<tr>\n'
    output = output .. '<td colspan="4" style="font-size:70%; line-height:normal; color:#222;">'
    output = output .. 'A lista é ordenada pela quantidade de votos, mas apenas aqueles indicados como eleitos devem assumir o cargo. Segundo o TSE, a escolha de vereadores segue o sistema proporcional, com os votos sendo destinados aos partidos e federações.'
    output = output .. '</td>\n'
    output = output .. '</tr>\n'

    output = output .. '</table>\n'

    return output
end
return p