Documentation icon دستاویز [تخلیق]
--
-- This module will implement {{Super Over}}
--

local p = {}
local args

local function getArgs(frame)
    local parent_args = frame:getParent().args;
    local args = {}

    for k, v in pairs(parent_args) do
        if v ~= '' then args[k] = v end
    end
    return args;
end

function addTableRow(tbl, small)
    local row = tbl:tag('tr')
    if small then
        row:css('font-size', '85%')
        end
    return row
end

function renderScores(tbl)
    local scoreRow = addTableRow(tbl, true)
    scoreRow:tag('th')
    scoreRow:tag('th'):css('border-right-style', 'hidden')
    scoreRow:tag('th'):wikitext('مجموعہ')
    scoreRow:tag('th'):wikitext(args.Score1)
    scoreRow:tag('th'):css('border-right-style', 'hidden')
    scoreRow:tag('th'):wikitext('مجموعہ')
    scoreRow:tag('th'):wikitext(args.Score2)
end

function renderDelivery(tbl, b)
    local ball = "B" .. tostring(b)
    local bat1 = args[ball .. "T1Bat"]
    local bat2 = args[ball .. "T2Bat"]
    local bowl1, bowl2, run1, run2

    if bat1 then
        bowl1 = args.Team2Bowler
        run1 = args[ball .. "T1Run"]
    end
    if bat2 then
        bowl2 = args.Team1Bowler
        run2 = args[ball .. "T2Run"]
    end
    if not run1 and not run2 then return false end

    local deliveryRow = addTableRow(tbl, true)
    if b ~= 1 then
        deliveryRow:css('border-top-style', 'hidden')
    end
    deliveryRow:tag('td'):wikitext(tostring(b))

    deliveryRow:tag('td'):wikitext(bowl1)
    deliveryRow:tag('td'):wikitext(bat1)
    deliveryRow:tag('td'):wikitext(run1)

    deliveryRow:tag('td'):wikitext(bowl2)
    deliveryRow:tag('td'):wikitext(bat2)
    deliveryRow:tag('td'):wikitext(run2)

    return true
end
 
function renderHeading(tbl)
    local titleRow = addTableRow(tbl, false)
    titleRow:tag('th')
        :attr('colspan', '7')
        :css('background', '#ebf5ff')
        :css('border', 'none')
        :wikitext('[[سوپر اوور]]')
        
    local teamsRow = addTableRow(tbl, true)
    local headerRow = addTableRow(tbl, true)

    teamsRow:tag('th')
        :attr('rowspan', '2')
        :css('width', '10%')
        :wikitext('گیند')
    for i = 1, 2 do
        teamsRow:tag('th')
            :attr('colspan', '3')
            :wikitext(args["Team" .. tostring(i)])
        headerRow:tag('th'):css('width', '19%'):wikitext('گیند باز')
        headerRow:tag('th'):css('width', '19%'):wikitext('بلے باز')
        headerRow:tag('th'):css('width', '7%'):wikitext('سکور')
    end
end

function p.superOver(frame)
    args = getArgs(frame)

    local tbl = mw.html.create('table')
        :addClass('wikitable')
        :addClass('collapsible')
        :addClass(args.state or 'collapsed')
        :css('background', '#ebf5ff')
        :css('text-align', 'center')
        :css('width', '100%')
        :css('border', 'none')
        :css('margin', '0')
 
    renderHeading(tbl)
    local i = 1
    local ball
    while true do
        if not renderDelivery(tbl, i) then break end
        i = i + 1
    end
    renderScores(tbl)

    return tostring(tbl)
end

return p