local p = {}

-- Internationalisation
local trackingcat = 'Category:Pages using sports overview with unknown parameters'
local labels = {
	competition = '대회',
	firstmatch = '첫 경기',
	lastmatch = '마지막 경기',
	startinground = '라운드 시작',
	finalposition = '최종 위치',
	record = '기록',
	total = '합계',
	source = '출처: ',
	lastupdated = 'Last updated: ',
	sourcedefault = '대회',
	unknownparameter = 'Unknown parameter: ',
	pld = '',
	w = '',
	d = '',
	l = '',
	pf = '',
	pa = '',
	pd = '',
	gf = '',
	ga = '',
	gd = '',
	wp = '',
	winner = '우승',
	runnerup = '2위',
	runnersup = '준우승'

-- Main function
function p.main(frame)
	local getArgs = require('Module:Arguments').getArgs
	local args = getArgs(frame)

	-- Get the row numbers and check for invalid input
	local rownumbers = {}
	local unknown = {}
	local showdates, showrounds, showpos = false, false, false
	local maxrow = -1
	local rowlimit = 99

	local function addrownumber
		if num <= rowlimit then
			table.insert
			maxrow = (num > maxrow) and num or maxrow
			return true
		end
		return flag
	end

	for k, v in pairs(args) do
		k = tostring(k)
		local n = tonumber(k:match('^[a-z]+(%d+)$') or '-1')
		if k == 'u' or k == 'c' or k == 's' or k == 'pts' then
			-- These are valid
		elseif k:match('^[cwdlfa]%d+$') then
			local added = addrownumber
		elseif k:match('[dfl]m%d+$') then
			showdates = addrownumber
		elseif k:match('sr%d+$') then
			showrounds = addrownumber
		elseif k:match('fp%d+$') then
			showpos = addrownumber
		else
			table.insert
		end
	end
	-- Sort the row numbers
	table.sort(rownumbers)

	-- Remove duplicates
	for i=#rownumbers,2,-1 do
		if rownumbers[i-1] == rownumbers[i] then
			table.remove
		end
	end

	local root = {}
	if maxrow > -1 then
		local WDL = require('Module:WDL').main
		-- Make the table
		table.insert
		-- Add the headers
		table.insert
		table.insert
		local totspan = 1
		if showdates then
			table.insert
			table.insert
			totspan = totspan + 2
		end
		if showrounds then
			table.insert
			totspan = totspan + 1
		end
		if showpos then
			table.insert
			totspan = totspan + 1
		end
		table.insert
		table.insert
		table.insert
		table.insert
		table.insert
		table.insert
		local pg = args.pts and args.pts == 'y' and 'p' or 'g'
		table.insert
		table.insert
		table.insert
		table.insert
		local evenodd = 'odd'

		-- Now add the rows
		local wtot, dtot, ltot, ftot, atot = 0, 0, 0, 0, 0
		for i=1,#rownumbers do
			local r = rownumbers[i]
			if evenodd == 'even' then
				table.insert
				evenodd = 'odd'
			else
				table.insert
				evenodd = 'even'
			end
			table.insert(root,'| ' .. (args['c' .. r] or ''))
			if showdates then
				if args['dm' .. r] then
					table.insert
				else
					table.insert(root,'| ' .. (args['fm' .. r] or ''))
					table.insert(root,'| ' .. (args['lm' .. r] or ''))
				end
			end
			if showrounds then
			table.insert(root,'| ' .. (args['sr' .. r] or ''))
			end
			if showpos then
				local fp = args['fp' .. r] or ''
				local bg =
					(fp:match('^' .. labels['winner']) and 'gold') or
					(fp:match('^' .. labels['runnersup']) and 'silver') or
					(fp:match('^' .. labels['runnerup']) and 'silver') or nil
				if bg then
					table.insert
				else
					table.insert
				end
			end
			wtot = wtot + (tonumber(args['w' .. r]) or 0)
			dtot = dtot + (tonumber(args['d' .. r]) or 0)
			ltot = ltot + (tonumber(args['l' .. r]) or 0)
			ftot = ftot + (tonumber(args['f' .. r]) or 0)
			atot = atot + (tonumber(args['a' .. r]) or 0)
			table.insert(root, WDL
			)
		end
		table.insert
		if totspan > 1 then
			table.insert
		else
			table.insert
		end
		table.insert(root, WDL
			)
		table.insert
		if args.u then
			table.insert
	end

	if #unknown > 0 then
		
		local unknown_params = require('Module:If preview')._warning({
			labels['unknownparameter'] ..
			'') ..
			'.'
		
		table.insert
	end

	return table.concat
end