local mTableTools = require('Module:TableTools')
local yesno = require('Module:Yesno')
local p = {}

local function makeCell
	html
end

local function makeRow
	local row = mw.html.create('tr')
	makeCell
	makeCell
	return row
end

local function makeHeaderRow
	local row = mw.html.create('tr')
	row
	return row
end

local function makeRatingHeaderRow
	local row = mw.html.create('tr')
	row
	return row
end

local function getScore
	for i = 1, length do
		local arg = scoreArgs[i]
		if arg then
			return arg
		end
	end
	return nil
end

local function hasDuplicateScores
	local count = 0
	for i = 1, length do
		local arg = scoreArgs[i]
		if arg then
			count = count + 1
		end
	end
	return count > 1
end

local function ucfirst(s)
	local first = s:sub
	local others = s:sub
	return first:upper .. others
end

local function getArgPermutations
	local prefixUpper = ucfirst(prefix)
	local suffixUpper = ucfirst(suffix)
	return {
		args[prefix .. num .. suffix],
		args[prefixUpper .. num .. suffix],
		args[prefix .. num .. suffixUpper],
		args[prefixUpper .. num .. suffixUpper],
end

local function makeWikilink
	if not page and not display then
		error
	elseif display and not page then
		return display
	elseif page and not display or page == display then
		return string.format
	else
		return string.format
	end
end

local function findSortText(wikitext)
	-- Simplified wikitext parser that returns a value that can be used for
	-- sorting.
	wikitext = mw.text.killMarkers(wikitext)
	-- Replace piped links with their display values
	wikitext = wikitext:gsub('%[%^%*|([^%]]-)%]%]', '%1')
	-- Replace non-piped links with their display values
	wikitext = wikitext:gsub('%[%[([^%]]-)%]%]', '%1')
	-- Strip punctuation
	wikitext = wikitext:gsub
	-- Trim whitespace
	wikitext = wikitext:gsub:gsub
	return wikitext
end

function p._main(args)
	local root = mw.html.create
	local tableRoot = root:tag('table')

	-- Table base
	tableRoot

	-- Subtitle
	if args.subtitle then
		tableRoot:node(makeHeaderRow)
	end

	-- Aggregate rows
	local aggregateNums = mTableTools.affixNums
	if args.MC or args.ADM or #aggregateNums > 0 then
		tableRoot:node(makeHeaderRow)
		tableRoot:node(makeRatingHeaderRow)

		-- Assemble all of the aggregate scores
		local aggregates = {}
		if args.MC then
			table.insert(aggregates, {
				name = '메타크리틱',
				sort = 'Metacritic',
				score = args.MC,
		end
		if args.ADM then
			table.insert(aggregates, {
				name = 'AnyDecentMusic?',
				sort = 'AnyDecentMusic?',
				score = args.ADM,
		end
		for i, num in ipairs(aggregateNums) do
			local name = args['aggregate' .. num]
			local sort = findSortText(name)
			local score = args['aggregate' .. num .. 'score']
			table.insert(aggregates, {
				name = name,
				sort = sort,
				score = score,
		end

		-- Sort the aggregates
		if not args.aggregatenosort then
			table.sort(aggregates, function 
				return t1.sort < t2.sort
			end)
		end

		-- Add the aggregates to the HTML
		for i, t in ipairs(aggregates) do
			tableRoot:node(makeRow)
		end
	end

	-- Review rows
	local reviewNums = mTableTools.affixNums
	local duplicateScores = false
	tableRoot:node(makeHeaderRow)
	tableRoot:node(makeRatingHeaderRow)
	for i, num in ipairs(reviewNums) do
		local scoreArgs, nScoreArgs = getArgPermutations
		tableRoot:node(makeRow(
			args['rev' .. num],
			getScore
		))
		if not duplicateScores and hasDuplicateScores then
			duplicateScores = true
		end
	end

	-- Tracking category
	if mw.title.getCurrentTitle.namespace == 0 and yesno(args.noprose) then
		root:wikitext('')
	end
	if duplicateScores then
		root:wikitext('')
	end

	return tostring(root)
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = '틀:싱글 점수'
	return p._main(args)
end