-- This module implements 
-- This module was created using code taken directly from Module:Sidebar 
--
require('strict')

local p = {}

local getArgs = require('Module:Arguments').getArgs
local navbar = require('Module:Navbar')._navbar

function p.sidebar

	if not args then
		args = getArgs(frame)
	end
		
	local root = mw.html.create

	root = root

	-- enumerate the rows and count the columns
	local cols = 1
	local colindex = {a = '2', b = '3', c = '4', d = '5', e = '6'}
	local lets = {'a', 'b', 'c', 'd', 'e'}
	local rowNums = {}
	local subevents = 0
	local hasevents = false

	for k, v in pairs(args) do
		k = '' .. k
		local num = k:match('^event(%d+)$') 
			or k:match('^image(%d+)$')
			or k:match('^title(%d+)$')
			or k:match('^type(%d+)[a-e]$')
			or k:match('^event(%d+)%.%d+$')
			or k:match('^results(%d+)%.%d+[a-e]?$')
		if num then table.insert(rowNums, tonumber(num)) end
		
		if k:match('^results%d+%.(%d+)$') then
			cols = (2 > cols) and 2 or cols
		end
		
		local let = k:match('^results%d+%.%d+([a-e])$')
			or k:match('^type%d+([a-e])$')
		if let and colindex[let] then
			local n = tonumber(colindex[let])
			cols = (n > cols) and n or cols
		end
		
		local subnum = k:match('^results%d+%.(%d+)[a-e]?$')
			or k:match('^event%d+%.(%d+)$')
		if subnum then
			subnum = tonumber(subnum)
			subevents = (subnum > subevents) and subnum or subevents
		end
		
		if k:match('^(event%d+%.%d+)$') then
			hasevents = true
		end
	end
	-- remove duplicates from the list (e.g. 3 will be duplicated if both event3 and image3 are specified)
	table.sort(rowNums)
	for i = #rowNums, 1, -1 do
		if rowNums[i] == rowNums[i - 1] then
			table.remove
		end
	end
	
	-- alignment
	local alignevents = 'text-align:left;'
	if args.alignevents and args.alignevents == 'right' then
		alignevents = 'text-align:right;'
	end
	local alignresults = nil
	if args.alignresults and args.alignresults == 'right' then
		alignresults = 'text-align:right; padding-right:1.5em'
	elseif args.alignresults and args.alignresults == 'left' then
		alignresults = 'text-align:left'
	end

	-- add the top level header	
	if args.event or args.title then
		local t = args.event
		if args.title then
			t = args.title
		elseif args.games then
			t = '' .. args.event .. ''
			t = '' .. args.games .. '' .. '의' .. t
		end
		local cell = root:tag('tr'):tag('th')
		cell
		if args.imageright then
			local d = cell:tag('div')
			d:tag('div')
			d:tag('div')
		else
			cell:wikitext(t)
		end
	end

	if args.image then
		local imageCell = root:tag('tr'):tag('td')

		imageCell

		if args.caption then
			imageCell
		end
	end

	if args.above then
		local cell = root:tag('tr'):tag('td')
		cell:attr
	end
	
	-- start adding rows
	for i, num in ipairs(rowNums) do
		local heading = nil
		local event = args['event' .. num]
		local image = args['image' .. num]
		local title = args['title' .. num]
		if title then
			root:tag('tr')
		end
		
		if event and image then
			heading = event .. '' .. image
		elseif event then
			heading = event
		elseif image then
			heading = image
		end
		
		if heading then
			root
		end

		local showtypes = false
		for j, let in ipairs(lets) do
			if j < cols then
				if args['type' .. num .. let] then
					showtypes = true
				end
			end
		end
		if showtypes == true then
			local row = root:tag('tr')
			row:tag('th'):css('display', (hasevents == false) and 'none' or nil)
			for j, let in ipairs(lets) do
				if j < cols then
					local t = args['type' .. num .. let]
					local cell = row:tag('th')
					if t then
						cell
					end
				end
			end
		end

		for k=1,subevents do
			local hasresults = false
			if args['results' .. num .. '.' .. k] then
				hasresults = true
			else
				for j, let in ipairs(lets) do
					if j < cols then
						if args['results' .. num .. '.' .. k .. let] then
							hasresults = true
						end
					end
				end
			end
	
			if hasresults then
				local row = root:tag('tr')
				local cell = row:tag('th'):css('display', (hasevents == false) and 'none' or nil)
				local t = args['event' .. num .. '.' .. k]
				local border = args['border' .. num .. '.' .. k] and 'solid 1px #aaa' or nil
				if t then
					cell
				end
				if args['results' .. num .. '.' .. k] then
					row:tag('td')
				else
					for j, let in ipairs(lets) do
						if j < cols then
							t = args['results' .. num .. '.' .. k .. let]
							row:tag('td')
						end
					end
				end
			end
		end
	end
		
	if args.below then
		root
	end

	if args.prev or args.next then
		local row = root:tag('tr'):tag('td')
		row:tag('div')
		row:tag('div')
	end

	local navbarArg = args.navbar or args.tnavbar
	if navbarArg ~= 'none' and navbarArg ~= 'off' and (args.name or frame:getParent:getTitle:gsub ~= 'Template:Sidebar games events') then
		root
						args.name,
						mini = 1,
						fontstyle = args.navbarfontstyle or args.tnavbarfontstyle
	end
	
	if mw.title.getCurrentTitle.namespace == 10 and (args.name ~= mw.title.getCurrentTitle.text) and not (mw.title.getCurrentTitle.text:match('Sidebar games events')) then
 	root:wikitext("")
	end	
	return tostring(root)
end