require('Module:No globals')
local p = {}
local getArgs = require('Module:Arguments').getArgs
local colore = require('Module:Partiti').colore
local lcella = 60
local tcella = '<th width="'..lcella..'px">Voti</th><th width="'..lcella..'px">%</th>'

local function vrg(a,b,c)
	local s1, k
	local s2 = a
	if (c == 1) then
		s1 = '<b>%s</b></td><td><small><b>%4.2f</b></small>'
	else
		s1 = '%s</td><td><small>%4.2f</small>'
	end
	while true do
		s2, k = string.gsub(s2, "^(-?%d+)(%d%d%d)", '%1&nbsp;%2')
		if (k==0) then break end
	end
	s1 = string.format(s1, s2, 100*tonumber(a)/b)
	s1 = s1:gsub('%.',',')
	s1 = '<td>' .. s1
	if (tonumber(a)<b) then
		s1 = s1 .. '<div style="width:'..lcella..'px;background:#eee;border-bottom:1px solid #999;height:3px"><div style="width:'..math.floor(0.5+lcella*tonumber(a)/b)..'px;background:#007cc3;height:3px"></div></div>'
	end
	s1 = s1 ..'</td>'
	return s1
end

local function colora(a,b,c)
	local clr = '#eee'
	if (a) then
		local x1 = string.find(a,'%[%[')
		if (x1) then
			local x2 = string.find(a:sub(x1),'%|')
			local x3 = string.find(a:sub(x1),'%]%]')
			if (x3) then
				if (x2) then
					a = a:sub(x1+2,x2-1)
				else
					a = a:sub(x1+2,x3-1)
				end
			end
		end
		clr = colore({a,(b or clr)})
	end
	return clr
end

local function verifica(e,el,vt,tt,b,el2,vt2,tt2)
	if (vt == 0) then e = e..' - non indicati votanti della votazione' end
	if (el == 0) then e = e..' - non indicati elettori della votazione' end
	if (vt < tt) then e = e..' - il totale è superiore al numero di votanti' end
	if (b>0) then
		if (vt2 == 0) then e = e..' - non indicati votanti del ballottaggio' end
		if (el2 == 0) then e = e..' - non indicati elettori del ballottaggio' end
		if (vt2 < tt2) then e = e..' - il totale è superiore al numero di votanti nel ballottaggio' end
	end
	return e
end

local function fine(el,vt,tt,b,el2,vt2,tt2,cc,cc2,e)
	local ss, ss2, ss3
	ss2 = '<tr><td align=left colspan='..cc..'>'
	if (b>0) then
		ss3 = '<tr><td colspan='..(cc2+2)..'></td></tr>'
		ss = ''..ss3
		ss = ss..ss2..'Iscritti</td>'..vrg(el,el,0)..vrg(el2,el2,0)..'</tr>'
		ss = ss..ss2..'– Astenuti</td>'..vrg(el-vt,el,0)..vrg(el2-vt2,el2,0)..'</tr>'
		ss = ss..ss2..'– Votanti</td>'..vrg(vt,el,0)..vrg(vt2,el2,0)..'</tr>'
		if (e == 1) then
			ss = ss..s3
			ss = ss..ss2..'– – Voti non validi</td>'..vrg(vt-tt,vt,0)..vrg(vt2-tt2,vt2,0)..'</tr>'
			ss = ss..ss2..'– – Voti validi</td>'..vrg(tt,vt,0)..vrg(tt2,vt2,0)..'</tr>'
		end
		ss = ss..'</table>'
	else
		ss3 = '<tr><td colspan='..cc2..'></td></tr>'
		ss = ''..ss3
		ss = ss..ss2..'Iscritti</td>'..vrg(el,el,0)..'</tr>'
		ss = ss..ss2..'– Astenuti</td>'..vrg(el-vt,el,0)..'</tr>'
		ss = ss..ss2..'– Votanti</td>'..vrg(vt,el,0)..'</tr>'
		if (e == 1) then
			ss = ss..ss3
			ss = ss..ss2..'– – Voti non validi</td>'..vrg(vt-tt,vt,0)..'</tr>'
			ss = ss..ss2..'– – Voti validi</td>'..vrg(tt,vt,0)..'</tr>'
		end
		ss = ss..'</table>'
	end
	return ss
end

function p.unico(frame)
	local args = getArgs(frame, {
		valueFunc = function (key, value)
			if type(key) == "number" then
				if value == nil then
					return nil
				else
					value = mw.text.trim(value)
				end
			else
				if value == '' then return nil end
			end
			return value
		end
	})
	local elettori = tonumber(args['elettori'] or 0)
	local elettori2 = tonumber(args['elettori2'] or elettori)
	local votanti = tonumber(args['votanti'] or 0)
	local votanti2 = tonumber(args['votanti2'] or 0)
	local ball = tonumber(args['ball1'] or 0)
	local titolo = args['titolo'] or ''
	local err = '-'
	local ss = ''
	local num = 0
	local tot,tot2 = 0,0

	ss = '<table class="wikitable" style="text-align:right;margin:10px auto"><caption>'..titolo..'</caption><tr><th rowspan=2>Candidato</th><th rowspan=2 colspan=2>Partito</th><th colspan=2>'
	if (ball>0) then ss= ss..'Primo turno</th><th colspan=2>Ballottaggio' else ss = ss..'Risultati' end
	ss = ss..'</th></tr><tr>'..tcella..((ball>0) and tcella or '')..'</tr>'

	num = 1
	while (args['cand'..num]) do
		if (args['voti'..num]) then
			tot = tot+tonumber(args['voti'..num])
			tot2 = tot2+tonumber(args['ball'..num] or 0)
		else
			err = ' - non indicato il numero di voti per il candidato '..num
		end
		num = num+1
	end
	num = num-1

	err = verifica(err,elettori,votanti,tot,ball,elettori2,votanti2,tot2)
	if (err == '-') then
		for i=1,num do
			if (i==1) then
				ss = ss..string.format('<tr><td align=left><b>%s</b></td><td style="width:0;background:%s"> </td><td align=left>%s</td>', args['cand'..i], colora(args['part'..i],args['col'..i]), (args['part'..i] or '—'))
				if (ball>0) then
					ss = ss..vrg(args['voti'..i],tot,0)
					ss = ss..vrg(args['ball'..i],tot2,1)
				else
					ss = ss..vrg(args['voti'..i],tot,1)
				end
				ss = ss..'</tr>'
			else
				ss = ss..string.format('<tr><td align=left>%s</td><td style="width:0;background:%s"> </td><td align=left>%s</td>', args['cand'..i], colora(args['part'..i],args['col'..i]), (args['part'..i] or '—'))
				ss = ss.. vrg(args['voti'..i],tot,0)
				if (args['ball'..i]) then ss = ss..vrg(args['ball'..i],tot2,0) end
				ss = ss..'</tr>'
			end
		end
		ss = ss..fine(elettori,votanti,tot,ball,elettori2,votanti2,tot2,3,5,1)
		return ss
	else
		return '<p style="color:red"><b>Errore nel template:</b> '..err..'</p>'
	end
end

function p.poli(frame)
	local args = getArgs(frame, {
		valueFunc = function (key, value)
			if type(key) == "number" then
				if value == nil then
					return nil
				else
					value = mw.text.trim(value)
				end
			else
				if value == '' then return nil end
			end
			return value
		end
	})
	local elettori = tonumber(args['elettori'] or 0)
	local elettori2 = tonumber(args['elettori2'] or elettori)
	local votanti = tonumber(args['votanti'] or 0)
	local votanti2 = tonumber(args['votanti2'] or 0)
	local eletti = tonumber(args['eletti'] or 0)
	local ball = tonumber(args['ball1'] or 0)
	local titolo = args['titolo'] or ''
	local err = '-'
	local ss = ''
	local num = 0
	local tot,tot2 = 0,0

	ss = '<table class="wikitable" style="text-align:right;margin:10px auto"><caption>'..titolo..'</caption><tr><th rowspan=2>Candidato</th><th rowspan=2 colspan=2>Partito</th><th colspan=2>'
	if (ball>0) then ss= ss..'Primo turno</th><th colspan=2>Ballottaggio' else ss = ss..'Risultati' end
	ss = ss..'</th></tr><tr>'..tcella..((ball>0) and tcella or '')..'</tr>'

	num = 1
	while (args['cand'..num]) do
		if (not args['voti'..num]) then
			err = ' - non indicato il numero di voti per il candidato '..num
		end
		num = num+1
	end
	num = num-1

	err = verifica(err,elettori,votanti,votanti,ball,elettori2,votanti2,votanti2)
	if (eletti == 0) then err = err..' - non indicati eletti della votazione' end
	if (err == '-') then
		for i=1,num do
			if (i<=eletti) then
				ss = ss..string.format('<tr><td align=left><b>%s</b></td><td style="width:0;background:%s"> </td><td align=left>%s</td>', args['cand'..i], colora(args['part'..i],args['col'..i]), (args['part'..i] or '—'))
				if (ball>0) then
					ss = ss..vrg(args['voti'..i],votanti,0)
					ss = ss..vrg(args['ball'..i],votanti2,1)
				else
					ss = ss..vrg(args['voti'..i],votanti,1)
				end
				ss =ss..'</tr>'
			else
				ss = ss..string.format('<tr><td align=left>%s</td><td style="width:0;background:%s"> </td><td align=left>%s</td>', args['cand'..i], colora(args['part'..i],args['col'..i]), (args['part'..i] or '—'))
				ss = ss..vrg(args['voti'..i],votanti,0)
				if (args['ball'..i]) then ss = ss..vrg(args['ball'..i],votanti2,0) end
				ss = ss..'</tr>'
			end
		end
		ss = ss..fine(elettori,votanti,tot,ball,elettori2,votanti2,tot2,3,5,0)
		return ss
	else
		return '<p style="color:red"><b>Errore nel template:</b> '..err..'</p>'
	end
end

function p.lista(frame)
	local args = getArgs(frame, {
		valueFunc = function (key, value)
			if type(key) == "number" then
				if value == nil then
					return nil
				else
					value = mw.text.trim(value)
				end
			else
				if value == '' then return nil end
			end
			return value
		end
	})
	local elettori = tonumber(args['elettori'] or 0)
	local elettori2 = tonumber(args['elettori2'] or elettori)
	local votanti = tonumber(args['votanti'] or 0)
	local votanti2 = tonumber(args['votanti2'] or 0)
	local ball = tonumber(args['ball1'] or 0)
	local titolo = args['titolo'] or ''
	local err = '-'
	local ss = ''
	local num = 0
	local tot,tot2 = 0,0

	ss = '<table cellpadding=0 cellspacing=0 style="border-collapse:collapse;margin:10px auto"><caption><b>'..titolo..'</b><tr style="vertical-align:top"><td>'
	ss = ss..'<table class="wikitable" style="text-align:right"><tr><th rowspan=2 colspan=2>Partito</th><th colspan=2>'
	if (ball>0) then ss= ss..'Primo turno</th><th colspan=2>Ballottaggio' else ss = ss..'Risultati' end
	ss = ss..'</th><th width="'..lcella..'px" rowspan=2>Seggi'
	ss = ss..'</th></tr><tr>'..tcella..((ball>0) and tcella or '')..'</tr>'
	
	num = 1
	while (args['part'..num]) do
		if (args['voti'..num]) then
			tot = tot+tonumber(args['voti'..num])
			tot2 = tot2+tonumber(args['ball'..num] or 0)
		else
			err = ' - non indicato il numero di voti per il partito '..num
			end
		num = num+1
	end
	num = num-1

	err = verifica(err,elettori,votanti,tot,ball,elettori2,votanti2,tot2)
	if (err == '-') then
		for i=1,num do
			ss = ss..string.format('<tr><td style="width:0;background:%s"></td><td align=left>%s</td>', colora(args['part'..i],args['col'..i]), args['part'..i])
			ss = ss..vrg(args['voti'..i],tot,0)
			if (ball>0) then
				if (args['ball'..i]) then ss = ss..vrg(args['ball'..i],tot2,0) else ss=ss..'<td></td><td></td>' end
			end
			ss = ss..'<td>'..(args['seggi'..i] or '')..'</td></tr>'
		end
		ss = ss..fine(elettori,votanti,tot,ball,elettori2,votanti2,tot2,2,5,1)
		ss = ss..'</td><td width="12px"></td><td><table class="wikitable"><tr><th colspan=2>Eletti</th></tr>'
		num = 1
		while (args['el'..num]) do
			ss = ss..string.format('<tr><td style="background:%s"> </td><td>%s</td></tr>',colora(args['part el'..num],args['col'..num]), args['el'..num])
			num = num+1
		end
		ss = ss..'</table></td></tr></table>'
		return ss
	else
		return '<p style="color:red"><b>Errore nel template:</b> '..err..'</p>'
	end
end

return p