require('Module:No globals')
local p = {}
local political_party = require('Module:Political party')  
function p.main
local args = require('Module:Arguments').getArgs
local index, headings, showtotal = {}, {}, {}
local cols, rounds = 0, 1
local winner, winner_votes = {0, 0}, {0, 0}
local valid = {0, 0}
local invalid = {tonumber(args.invalid) or 0, tonumber(args.invalid2) or 0}
local blank = {tonumber(args.blank) or 0, tonumber(args.blank2) or 0}
local majority = {tonumber(args.majority)}
local majority2 = {tonumber(args.majority2)}
local majoritypct = {tonumber(args.majoritypct)}
local majoritypct2 = {tonumber(args.majoritypct2)}
local totalvotes = {tonumber(args.totalvotes), tonumber(args.totalvotes2)}
local electorate = {tonumber(args.electorate), tonumber(args.electorate2)}
local turnout = {tonumber(args.turnout), tonumber(args.turnout2)}
local seats = 0
local seats_2 = 0
local st1t = 0
local st2t = 0
local st3t = 0
local st4t = 0
local st5t = 0
local totseats = 0
local row, secondrow
local tracking = ''
local max_rows = 0  
-- helper functions
local lang = mw.getContentLanguage
local function fmt(n)
return n and tonumber(n) and lang:formatNum(tonumber(n)) or nil
end
local function pct(n, d)
n, d = tonumber(n), tonumber(d)
if n and d and d > 0 then
return string.format('%.2f', n / d * 100)
end
return '&ndash;'
end
local function tonumdash(s)
if s then
s = mw.ustring.gsub(s, '&[MmNn][Dd][Aa][Ss][Hh];', '-')
s = mw.ustring.gsub(s, '&[Mm][Ii][Nn][Uu][Ss];', '-')
s = mw.ustring.gsub(s, '[—–−]', '-')
return tonumber(s) or 0
end
end
local function unlink(s)
if s then
s = s:match("^[^%[]-%[%[([^%]]-)|[^%]]-%]%].*$") or s
s = s:match("^[^%[]-%[%[([^%]]-)%]%].*$") or s
end
return s
end
local function get_color(color, party)
if color == nil then
local party = unlink(party) or ''
if party ~= '' then
color = political_party._fetch({party, 'color'})
end
end  
if color ~= nil then
color = mw.ustring.gsub(color, '&(#)35;', '%1')
end
return color
end  
-- preprocess the input
local stop_flag = false
local i = 0
local has_votes = args['invalid'] or args['totalvotes'] or args['electorate'] or args['turnout']
local has_seats = args['seats' .. i .. '_2']
local has_sw = args['sw' .. i .. '_2']
local seats_only = has_seats and not has_votes
local has_rspan = args['vspan' .. i] or args['v2span' .. i]
local has_sspan = args['sspan' .. i]
while stop_flag == false do
stop_flag = true
for kk = 1, 20 do
i = i + 1
for k, key in ipairs({'alliance', 'atotal', 'cand', 'vp', 'party', 'ivotes', 'ipct', 'iseats', 'sc', 'st1t', 'st2t', 'st3t', 'st4t', 'st5t', 'sw', 'seats', 'totalvotes', 'totseats', 'acolor'}) do
if args[key .. i] then
headings[key] = true
stop_flag = false
max_rows = i > max_rows and i or max_rows
end
end
if args['row' .. i] then
stop_flag = false
max_rows = i > max_rows and i or max_rows
end  
if args['row' .. i] or args['cand' .. i] or args['party' .. i] or args['alliance' .. i] or args['atotal' .. i] or args['atotalnv' .. i] or args['header' .. i] then
table.insert(index, i)
if args['votes' .. i] then
has_votes = true
if tonumber(args['votes' .. i]) then showtotal.votes = 1 end
local votesi = tonumber(args['votes' .. i]) or 0
args['votes' .. i] = votesi
if votesi > winner_votes[1] then
winner[1] = i
winner_votes[1] = votesi
end
valid[1] = valid[1] + votesi
end
if args['ivotes' .. i] then
has_votes = true
end
if args['ipct' .. i] then
has_votes = true
end
if args['sw' .. i .. '_2'] then
has_sw = true
rounds = 2
end
if args['valid2'] or args['invalid2'] or args['totalvotes2'] or args['electorate2'] or args['turnout2'] then
rounds = 2
end
if args['vspan' .. i] or args['v2span' .. i] then
has_rspan = true
end
if args['sspan' .. i] then
has_sspan = true
end
if args['ipct' .. i .. '_2'] then
rounds = 2
end
if args['votes' .. i .. '_2'] then
has_votes = true
rounds = 2
if tonumber(args['votes' .. i .. '_2']) then showtotal.votes_2 = 1 end
local votesi = tonumber(args['votes' .. i .. '_2']) or 0
args['votes' .. i .. '_2'] = votesi
if votesi > winner_votes[2] then
winner[2] = i
winner_votes[2] = votesi
end
valid[2] = valid[2] + votesi
end
if args['seats' .. i] then
if tonumber(args['seats' .. i]) then showtotal.seats = 1 end
seats = seats + (tonumber(args['seats' .. i]) or 0)
end
if args['seats' .. i .. '_2'] then
has_seats = true
rounds = 2
if tonumber(args['seats' .. i ..'_2']) then showtotal.seats_2 = 1 end
seats_2 = seats_2 + (tonumber(args['seats' .. i .. '_2']) or 0)
end
if args['st1t' .. i] then
if tonumber(args['st1t' .. i]) then showtotal.st1t = 1 end
st1t = st1t + (tonumber(args['st1t' .. i]) or 0)
end
if args['st2t' .. i] then
if tonumber(args['st2t' .. i]) then showtotal.st2t = 1 end
st2t = st2t + (tonumber(args['st2t' .. i]) or 0)
end
if args['st3t' .. i] then
if tonumber(args['st3t' .. i]) then showtotal.st3t = 1 end
st3t = st3t + (tonumber(args['st3t' .. i]) or 0)
end
if args['st4t' .. i] then
if tonumber(args['st4t' .. i]) then showtotal.st4t = 1 end
st4t = st4t + (tonumber(args['st4t' .. i]) or 0)
end
if args['st5t' .. i] then
if tonumber(args['st5t' .. i]) then showtotal.st5t = 1 end
st5t = st5t + (tonumber(args['st5t' .. i]) or 0)
end
if args['totseats' .. i] then
if tonumber(args['totseats' .. i]) then showtotal.totseats = 1 end
totseats = totseats + (tonumber(args['totseats' .. i]) or 0)
end
if has_seats and not has_votes then
seats_only = true
end
end
end
end  
local ovalid = {valid[1], valid[2]}
seats = ((args['total_seats'] or '') == 'TOTAL' and seats) or args['total_seats'] or seats
totseats = ((args['total_totseats'] or '') == 'TOTAL' and totseats) or args['total_totseats'] or totseats
st1t = ((args['total_st1t'] or '') == 'TOTAL' and st1t) or args['total_st1t'] or st1t
st2t = ((args['total_st2t'] or '') == 'TOTAL' and st2t) or args['total_st2t'] or st2t
st3t = ((args['total_st3t'] or '') == 'TOTAL' and st3t) or args['total_st3t'] or st3t
st4t = ((args['total_st4t'] or '') == 'TOTAL' and st4t) or args['total_st4t'] or st4t
st5t = ((args['total_st5t'] or '') == 'TOTAL' and st5t) or args['total_st5t'] or st5t
if seats or totseats or st1t or st2t or st3t or st4t or st5t or args['total_sc'] or args['total_st1t'] or args['total_st2t'] or args['total_st3t'] or args['total_st4t'] or args['total_st5t'] or args['valid'] or ((rounds > 1) and args['valid2']) then
max_rows = max_rows + 1
local i = max_rows
table.insert(index, i)
args['votes' .. i] = showtotal.votes and valid[1] or nil
args['votes' .. i .. '_2'] = showtotal.votes_2 and valid[2] or nil
args['colour' .. i] = 'inherit'
args['color' .. i] = 'inherit'
args['row' .. i] = 'மொத்தம்'
args['sw' .. i] = '–'
args['sw' .. i .. '_2'] = '–'
args['seats' .. i] = showtotal.seats and seats or nil
args['seats' .. i .. '_2'] = showtotal.seats_2 and seats_2 or nil
args['st1t' .. i] = showtotal.st1t and st1t or args.total_st1t
args['st2t' .. i] = showtotal.st2t and st2t or args.total_st2t
args['st3t' .. i] = showtotal.st3t and st3t or args.total_st3t
args['st4t' .. i] = showtotal.st4t and st4t or args.total_st4t
args['st5t' .. i] = showtotal.st5t and st5t or args.total_st5t
args['sc' .. i] = args['total_sc']
args['totseats' .. i] = showtotal.totseats and totseats or nil
args['font-weight' .. i] = 'bold'
args['class' .. i] = 'sortbottom'
ovalid[1] = tonumber(args['valid']) or valid[1]
ovalid[2] = tonumber(args['valid2']) or valid[2]
end  
-- build the table
local root = mw.html.create(args['embed'] and '' or 'table')
if args['embed'] == nil then
root
:addClass('wikitable sortable')
:tag('caption')
:wikitext(args.caption)
:done
end  
local topcell = nil
if args['image'] then
topcell = root
:tag('th')
:wikitext(args['image'])
:css('text-align', '')
:css('background', '#F8F9FA')
end  
local rowspan = (rounds > 1) and 2 or headings['st1t'] and 2 or nil
row = args['embed'] and mw.html.create('') or root:tag('tr')
if args['dsv'] then
row
:tag('th')
:wikitext('கட்சி')
:attr('scope', 'col')
:attr('colspan', 2)
:attr('rowspan', rowspan)
:done
cols = cols + 2
row
:tag('th')
:wikitext(args.candtitle or 'வேட்பாளர்')