Módulo:Linha do tempo de jogos eletrônicos

--Adaptação de Module:VG timeline anglófono
require('strict')
local getArgs = require('Módulo:Arguments').getArgs
local p = {}

local function isYearReleased(args, year)
	if args[year] then
		return true
	end
	for asciiletter = 97, 106 do -- 97 == a, 106 == j
		if args[year .. string.char(asciiletter)] then
			return true
		end
	end
end

local function color(args, year)
	
	if args[year .. '_cor'] then
		return args[year .. '_cor'] 
	end
	
	for yearrange = 1, 5 do
		if args['anos' .. yearrange] and args['anos' .. yearrange .. '_cor'] then
			local _, _, beginyear, endyear = string.find( args['anos' .. yearrange], '^(%d%d%d%d).+(%d%d%d%d)$' )
			
			local year = tonumber(year) or 9999 -- For year == 'ASA'
			beginyear = tonumber(beginyear) or 0
			endyear =  tonumber(endyear) or 9999
			
			if year >= beginyear and year <= endyear then
				local _, _, color1, color2 = string.find( args['anos' .. yearrange .. '_cor'], '^(%S*)%s*(%S*)$' )
				color2 = (color2 == '') and color1 or color2
				return isYearReleased(args, year) and color1 or color2
			end
		end
	end
	
	return isYearReleased(args, year) and '#0BDA51' or '#228B22'
end

local function titleItem(builder, content)
	if content ~= nil then
		builder:tag( 'span' ):wikitext( content .. '<br />' )
	end
end

local function left(builder, year)
	builder:tag( 'td' )
		:css('padding', '5px')
		:wikitext( year )
end

local function center(builder, args, year)
	builder:tag( 'td' )
		:css( 'width', '10px' )
		:css( 'border', '1px solid black' )
		:css( 'background-color', color(args, year) )
		:css( 'padding', '5px' )
end

local function right(builder, args, year)
	if isYearReleased(args, year) == nil then return end
	
	builder = builder:tag('td')
		:css('padding','5px')
	titleItem(builder, args[year] or args[year .. 'a' ])

	for asciiletter = 98, 106 do -- 98 == b, 106 == j
		titleItem(builder, args[year .. string.char(asciiletter)])
	end
end

local function row(builder, args, year)
	builder = builder:tag('tr')
	left(builder, year)
	center(builder, args, year)
	right(builder, args, year)
end

--------------------------------------------------------------------------------

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	-- Main module code goes here.
	local ret
	local firstyear, lastyear, ASA
	ASA = isYearReleased(args, 'ASA')
	ret = mw.html.create( 'table' )
		:css('float', args.align or 'right')
		:css('clear', args.align or 'right')
		:css('margin', '0 0 0.5ex 1em')
		:css('font-size', '80%')
		:css('line-height', '90%')
		:css('border-collapse', 'collapse')
		:css('border-spacing', '0')
	
	ret:tag('caption')
		:css('padding', '5px')
		:wikitext(args.titulo and ('<b>' .. args.titulo .. '</b>') or '<b>Linha do tempo de anos de lançamento</b>')
		:wikitext(args.legenda and ('<br />' .. args.legenda) )

	if tonumber(args.primeiro) then
		firstyear = tonumber(args.primeiro)
	else
		for i = 1940, os.date('%Y') do
			if isYearReleased(args, i) then
				firstyear = i
				break
			end
		end
	end
	
	if tonumber(args.ultimo) then
		lastyear = tonumber(args.ultimo)
	else
		for i = os.date('%Y') + 3, ASA and os.date('%Y') or firstyear, -1 do
			if isYearReleased(args, i) then
				lastyear = i
				break
			end
		end
		lastyear = lastyear or (os.date('%Y') - 1)
	end

	for year = firstyear, lastyear do
		row(ret, args, year)
	end
	
	if ASA then
		row(ret, args, 'ASA')
	end
	
	return ret
end

return p