Die Dokumentation für dieses Modul kann unter Modul:Europamandate/Doku erstellt werden

local p = {}
local full = require('Module:Partei').full
local farbe = require('Modul:Partei').color
local pipelink = require('Module:Partei').pipelink
local getArgs = require('Module:Arguments').getArgs

function p.variation(frame)
	local args = getArgs(frame)
	local a = string.sub(args[1], 1, 1)
	if a == '+' or (tonumber(a) and tonumber(a) > 0) then pos = true zeichen = '+' elseif a == '0' then unv = true zeichen = '' else zeichen = '-' end
	local symbol = pos and '▲' or unv and '▬' or '▼'
	local value = tonumber(a) and args[1] or mw.ustring.sub(args[1], 2)
	local farbe = pos and '00CC00' or unv and '00AAFF' or 'FF0000'
	if args[2] == 'f' then bild = '<span style="color:#'..farbe..'>'..symbol..'</span>' else bild = symbol end
	return '<span data-sort-value="' ..zeichen..value.. '">'..bild..' '..value..'</span>'
end

function p.fraktionen(frame)
	local args = getArgs(frame)
	local index, data = {}, {}
	local num = 1
	local mandate = 0
	local i = 0
	for a = 1, 80 do i = i + 1
		if args['l'..i] or args['f'..i] or args['p'..i] or args['g'..i] then table.insert(index, i) end
		if args['p'..i] then partei = true end
		if args['g'..i] then gruppe = true end
		if args['k'..i] or args['v'..i] then kand = true end
		if args['e'..i] or args['ee'..i] then eupart = true end
		if args['ef'..i] then eufraktion = true end
	end

	while (args['s'..num]) or (args['m'..num]) do
		data[num] = {
			s = tonumber(args['s'..num]) or tonumber(args['m'..num]),
		}				
		mandate = mandate + data[num].s
		num = num+1
	end

	gesamt = args['gesamt'] or mandate

	if gruppe then cspan = 4 elseif partei then cspan = 3 end
	
	col = 2 + ((args['f1'] or args['l1'] and args['s1'] and not eufraktion) and 1 or 0) + (partei and 2 or 0) + (gruppe and 1 or 0) + (kand and 1 or 0) + (eupart and 1 or 0) + (eufraktion and 1 or 0) + (args['a1'] and 1 or 0) + (args['r1'] and 1 or 0) + (args['fv1'] and 1 or 0) + (args['v1'] and 1 or 0) + (args['prozent'] and 1 or 0) + (args['d1'] and 1 or 0) + (args['z1'] and 1 or 0) + (args['s1'] and 1 or 0) + (args['w1'] and 1 or 0)

	local function pzn(frame)
		local args = getArgs(frame)
		local a = tonumber(args[1])
		local b = tonumber(args[2])
		if a and b then
			x = string.format('%.1f', a / b * 100)
			y = string.gsub(x, "%.", "%,")
			return y
		else
			return ''
		end
	end

	local root = mw.html.create('table'):addClass('wikitable')

	if args['a1'] then
		root
		:tag('th'):wikitext('N.'):done()
	end
	
	if eufraktion or gruppe then
		if gruppe then
			root
			:tag('th'):attr('colspan', '5'):wikitext('Liste/Partei'):done()
		elseif partei then
			root
			:tag('th'):attr('colspan', '4'):wikitext('Liste/Partei'):done()
		else
			root
			:tag('th'):attr('colspan', '2'):wikitext('Partei'):done()
		end
	else
		if args['f1'] then
			root
				:tag('th'):attr('colspan', '2'):wikitext('Fraktion'):done()
		elseif args['l1'] then
			root
				:tag('th'):attr('colspan', '2'):wikitext('Liste'):done()
		end
	end
	if args['r1'] then
		root
			:tag('th'):wikitext('Richtung'):done()
	end
	if args['fv1'] then
		root
			:tag('th'):wikitext('Fraktions-<br/>vorsitzender'):done()
	end
	if args['f1'] or (args['l1'] and not eufraktion and not eupart) then
		root
			:tag('th'):wikitext('Mandate'):done()
	end
	if args['prozent'] then
		root
			:tag('th'):wikitext('% Mandate'):done()
	end
	if args['d1'] then
		root
			:tag('th'):wikitext('+/-'):done()
	end
	if partei and not eufraktion and not gruppe then
		root
			:tag('th'):attr('colspan', '2'):wikitext('Partei'):done()
	end
	if args['s1'] then
		root
			:tag('th'):wikitext('Mandate'):done()
	end
	if args['z1'] then
		root
			:tag('th'):wikitext('Name'):done()
	end	
	if eupart then
		root
			:tag('th'):wikitext('[[Europäische politische Partei|Europapartei]]'):done()
	end
	if eufraktion then
		root
			:tag('th'):wikitext('[[Fraktion im Europäischen Parlament|Fraktion]]'):done()
	end
	if kand then
		root
			:tag('th'):wikitext(args['amt'] or 'Erstplatzierter<br/>in der Liste'):done()
	end
	if args['wahlkreis'] or args['w1'] then
		root
			:tag('th'):wikitext(args['wahlkreis'] or 'Wahlkreise'):done()
	end

	for i, v in ipairs(index) do root:tag('tr')
		row = root:tag('tr')

		if args['f'..v] then
			color = '#'..farbe({args['f'..v], 'EU'})
		elseif args['l'..v] then
			if args['farbliste'] then color = '#'..farbe({(args['p'..v] or args['l'..v])})
				else color = '#'..farbe({args['l'..v]})
			end
		end

		if args['p'..v] then secondcolor = '#'..farbe({args['p'..v], 'EU'})
			else secondcolor = color
		end
		
		if args['f'..v] then spalte = full({args['f'..v], 'EU'})
			elseif args['l'..v] then spalte = args['l'..v]
		end

		if args['l'..v] or args['f'..v] then
			
			if args['a'..v] then
				row:tag('td')
					:wikitext(args['a'..v])				
					:attr('rowspan', args['an'..v] or args['n'..v])
					:css('background', '#F0F0F0')
					:css('font-weight', 'bold')
					:css('text-align', 'center')
			end
				
			row:tag('td')
				:css('background', color)
				:attr('rowspan', args['n'..v])
			
			if gruppe then
				if args['g'..v] then
					row:tag('td')
						:wikitext(spalte)
						:attr('rowspan', args['n'..v])
				elseif args['p'..v] then
					row:tag('td')
						:wikitext(spalte)
						:attr('rowspan', args['n'..v])
						:attr('colspan', 2)
				else
					row:tag('td')
						:wikitext(spalte)
						:attr('rowspan', args['n'..v])
						:attr('colspan', 4)
				end
			elseif partei then
				if args['p'..v] then
					row:tag('td')
						:wikitext(spalte)
						:attr('rowspan', args['n'..v])
				else
					row:tag('td')
						:wikitext(spalte)
						:attr('rowspan', args['n'..v])
						:attr('colspan', 3)
				end
			else
				row:tag('td')
					:wikitext(spalte)
					:attr('rowspan', args['n'..v])
			end

			if args['r'..v] then
				row:tag('td')
					:wikitext(args['r'..v])
					:attr('rowspan', args['n'..v])
			end
			if args['fv'..v] then
				row:tag('td')
					:wikitext(args['fv'..v])
					:attr('rowspan', args['n'..v])
			end
			if args['m'..v] or (args['s'..v] and args['p'..v] and not eufraktion) then
				row:tag('td')
					:wikitext('<div>'..(args['m'..v] or args['s'..v])..' / '.. gesamt ..'</div><div style="width:100px;border:0.1px solid #AAA;background-color:#FFF;height:0.6em;position:relative;"><div style="background-color:' ..color..';width:'.. (args['m'..v] or args['s'..v])/gesamt*100 ..'px;height:0.6em;"></div></div>')
					:attr('rowspan', args['n'..v])
			end
			if args['prozent'] then
				row:tag('td')
					:wikitext(pzn({args['m'..v] or args['s'..v], gesamt}))
					:css('text-align', 'right')
					:css('padding-right', '2em')
					:attr('rowspan', args['n'..v])
			end
			if args['d'..v] then
				row:tag('td')
					:wikitext(args['d'..v])
					:css('text-align', 'center')
					:attr('rowspan', args['n'..v])
			end
		end

		if args['g'..v] then
			if args['p'..v] then
				row:tag('td')
					:wikitext(args['g'..v])
					:attr('rowspan', args['gn'..v])
			else
				row:tag('td')
					:wikitext(args['g'..v])
					:attr('rowspan', args['gn'..v])
					:attr('colspan', 3)
			end
		end

		if args['p'..v] then
			row:tag('td')
				:css('background', "#"..farbe({(args['p'.. v])}))
			row:tag('td')
				:wikitext(args['p'..v])
		end
		if args['z'..v] then
			row:tag('td')
				:wikitext(args['z'..v])
		end
		if args['s'..v] then
			row:tag('td')
				:wikitext('<div>'..args['s'..v]..' / '..gesamt..'</div><div style="width:100px;border:0.1px solid #AAA;background-color:#FFF;height:0.6em;position:relative;"><div style="background-color:'..secondcolor..';width:'.. args['s'..v]/gesamt*100 ..'px;height:0.6em;"></div></div>')
		end
		if eupart then
			if args['e'..v] or args['ee'..v] then
				row:tag('td')
					:wikitext(args['ee'..v] or pipelink({args['e'..v], 'EP'}))
			else
				row:tag('td')
					:wikitext('&ndash;')
			end
		end
		if args['ef'..v] then
			row:tag('td')
				:wikitext(pipelink({args['ef'..v], 'EU'}))
		end
		if args['k'..v] then
			row:tag('td')
				:wikitext(args['k'..v])
				:attr('rowspan', args['an'..v] or args['n'..v])
		end
		if args['v'..v] then
			row:tag('td')
				:wikitext(args['v'..v])
		end
		if args['w'..v] then
			if args['gn'..v] then
				row:tag('td')
					:wikitext(args['w'..v])
					:attr('rowspan', args['gn'..v])
			else
				if args['n'..v] and not args['g'..v] then
					row:tag('td')
						:wikitext(args['w'..v])
						:attr('rowspan', args['n'..v])
				else
					row:tag('td')
						:wikitext(args['w'..v])
				end
			end
		end
	end
	if args['url'] then
		row = root:tag('tr')
			row:tag('td')
				:attr('colspan', col)
		row = root:tag('tr')
			row:tag('td')
				:wikitext('Quelle: ['.. args['url']..' '..(args['quelle'] or 'Europäisches Parlament')..']')
				:attr('colspan', col)
	end
	
	return root
end

return p