local p = {}
local lang = nil
local delink = require('Module:Delink')._delink
local eventschema = "http://schema.org/SportsEvent"
local teamschema = "http://schema.org/SportsTeam"
local placeschema = "http://schema.org/Place"
local penalties = 'Penalties'
local aet = 'a.e.t.'
local asdet = 'a.s.d.e.t.'
local agget = 'a.e.t./g.g.'
local asget = 'a.e.t./s.g.'
local attendance = 'Attendance:'
local referee = 'Referee:'
local templatestyles = 'Module:Football box/styles.css'

local tracking, preview

local function checkarg
	if not k or type(k) ~= 'string' then return end
	
	if k == 'assistantreferees2' then
		table.insert
	elseif k == 'aggregatescore' or k == 'assistantreferees' or 
			k == 'fourthofficial' or k == 'game' or k == 'motm' or 
			k == 'nobars' or k == 'note' or k == 'result' then
		-- valid and tracked
		table.insert
	elseif k == 'size' or k == 'bg' or k == 'id' or k == 'event' or 
		k == 'date' or k == 'time' or k == 'round' or k == 'team1' or 
		k == 'team2' or k == 'score1' or k == 'score2' or k == 'score' or k == 'scorenote' or
		k == 'aet' or k == 'asdet' or k == 'agget' or k == 'asget' or k == 'goals1' or k == 'report' or k == 'goals2' or 
		k == 'penaltyscore' or k == 'penalties1' or k == 'penalties2' or 
		k == 'stadium' or k == 'location' or k == 'attendance' or 
		k == 'referee' or k == 'stack' then
		-- valid and not tracked
	else
		-- invalid
		local vlen = mw.ustring.len(k)
		k = mw.ustring.sub(k, 1, (vlen < 25) and vlen or 25) 
		k = mw.ustring.gsub
		table.insert
		table.insert
	end
end

local function timestamp
	if not d then return nil end
	
	lang = lang or mw.language.getContentLanguage -- lazy initialize
	local success, timestamp = pcall(lang.formatDate, lang, 'c', delink({d .. ' ' .. (t or '')}))
	if success then
		return timestamp
	else
		return nil
	end
end

local function fmtlist(s)
	s = mw.ustring.gsub(s or '', '%[%[ *([%?-]) *%]%]', '%1')
	s = mw.ustring.gsub(s, '%[%[ *[%?-] *| *(.-) *%]%]', '%1')
	if mw.ustring.sub == '*' then
		return tostring(mw.html.create('div'):addClass('plainlist'):newline:wikitext(s))
	end
	return s
end

local function makelink
	if s:match('^http') then
		return '[' .. s .. ' ' .. t .. ']'
	end
	return s
end

local function trim(s)
	return s:match('^[\'"%s]*(.-)[\'"%s]*$')
end

local function getid(s)
	s = trim(s or '')
	if s and s ~= '' then
		return s
	end
	return nil
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local id = getid(args['id'])
	local d = timestamp
	local block
	
	tracking, preview = {}, {}
 for k, v in pairs(args) do
 	if v ~= '' then
 		checkarg
 	end
	end

	local score = 'v'
	if args['score1'] or args['score2'] then
		score = (args['score1'] or '0') .. '&ndash;' .. (args['score2'] or '0')
	elseif args['score'] and args['score'] ~= '' then
		score = args['score']
	end
	if args['aet'] then
		score = score .. ' (' .. aet .. ')'
	elseif args['asdet'] then
		score = score .. ' (' .. asdet .. ')'
	elseif args['agget'] then
		score = score .. ' (' .. agget .. ')'
	elseif args['asget'] then
		score = score .. ' (' .. asget .. ')'
	end
	if args['scorenote'] then
		score = score .. '' .. args['scorenote']
	end
	
	-- Start box
	local root = 
		mw.html.create('div')
	root:newline
	
	if args['event'] then
		root:tag('div')
	end
	
	-- Start left block
	block = root:tag('div')
	
	local timetag = block:tag('time')
		
	timetag:tag('div')
	
	if args['time'] then
		timetag:tag('div')
	end
	
	if args['round'] then
		block:tag('div')
	end
	-- End block
	
	-- Start table
	local rtable = root:tag('table')
	local row = rtable:tag('tr')
	row:newline
	row:tag('th')
	row:tag('th')
	row:tag('th')

	row = rtable:tag('tr')
	row:tag('td')
	row:newline
	row:tag('td')
	row:newline
	row:tag('td')
	row:newline	
	
	if args['penaltyscore'] then
		rtable
		row = rtable:tag('tr')
		row:newline
		row:tag('td')
		row:newline
		row:tag('th')
		row:newline
		row:tag('td')
		row:newline
	end
	-- End table
	
	-- Start right block
	block = root:tag('div')
	
	if args['stadium'] then
		local sdiv = block:tag('div')
		if args['location'] then
			sdiv:tag('span')
			sdiv:wikitext
			sdiv:tag('span')
		else
			sdiv:tag('span')
		end
	end
	
	if args['attendance'] then
		block:tag('div'):wikitext(attendance ..' ' .. args['attendance'])
	end
	if args['referee'] then
		block:tag('div'):wikitext(referee .. ' ' .. args['referee'])
	end

	local trackstr = (#tracking > 0) and table.concat or ''
	if #preview > 0 and frame:preprocess( "" ) == "" then
		trackstr = tostring(mw.html.create('div')
	end
	
	return frame:extensionTag{ name = 'templatestyles', args = { src = templatestyles} } .. tostring(root) .. trackstr
end