Documentation for this module may be created at Module:CareerList/doc

local p = {}

function p.getSiteConfig(siteName)
    local siteConfig = mw.loadData('Module:CareerList/config')[siteName:lower()]
    if not siteConfig then
        error('Unknown site: ' .. siteName)
    end
    return siteConfig
end

function p.makeLink(siteName, id)
    local siteConfig = p.getSiteConfig(siteName)
    local url = siteConfig.url:gsub('%$1', id)
    return string.format('[[%s|%s]]', url, siteName)
end

function p.getCellBgColor(siteName)
    local siteConfig = p.getSiteConfig(siteName)
    return siteConfig.type == 'studio' and '#DADADA' or '#E7E7E7'
end

function p.getTableHtml(args)
    local list = args.list or error('List not specified')
    local rows = {}
    for _, item in ipairs(list) do
        local siteName, id, alias, notes
        if type(item) == 'table' then
            siteName = item.site or error('Site not specified')
            id = item.id or error('ID not specified')
            alias = item.alias or mw.title.getCurrentTitle().text
            notes = item.notes or 'N/A'
        else
            error('Invalid list item')
        end
        local link = p.makeLink(siteName, id)
        local cellBgColor = p.getCellBgColor(siteName)
        local rowBgColor = #rows % 2 == 0 and '#FFFFFF' or '#F3F3F3'
        table.insert(rows, string.format([[
            <tr style="background-color:%s;">
                <td style="background-color:%s;">%s</td>
                <td style="background-color:%s;">%s</td>
                <td>%s</td>
                <td>%s</td>
            </tr>
        ]], rowBgColor, cellBgColor, siteName, cellBgColor, link, alias, notes))
    end
    local tableHtml = string.format([[
        <table style="width:100%%;border-collapse:collapse;">
            <tr style="background-color:#F2F2F2;">
                <th style="background-color:#F2F2F2;">Website/Studio</th>
                <th style="background-color:#F2F2F2;">Link</th>
                <th style="background-color:#F2F2F2;">Alias</th>
                <th style="background-color:#F2F2F2;">Notes</th>
            </tr>
            %s
        </table>
    ]], table.concat(rows))
    return tableHtml
end

function p.main(frame)
    local args = frame:getParent().args
    return p.getTableHtml(args)
end

return p