--[[
* Modulo a supporto del template FictionTV.
]]--

require('Modulo:No globals')

local getArgs = require('Modulo:Arguments').getArgs
local errorCategory = 'Errori di compilazione del template FictionTV'
local p = {}

-- Parsifica il parametro annoprimatv
local function parse_annoprimatv(annoprimatv)
	local inizio, produzione, fine, err

	-- annoprimatv senza intervallo, esempio "[[2010]]"
	inizio = annoprimatv:match('^%[?%[?(%d%d%d%d)%]?%]?$')
	if inizio then
		fine = inizio
	-- intervallo tipo "[[2010]]-[[2015]]"
	elseif mw.ustring.match(annoprimatv, '^%[?%[?%d%d%d%d%]?%]?%s*[–-]%s*%[?%[?%d%d%d%d%]?%]?$') then
		inizio, fine = mw.ustring.match(annoprimatv, '^%[?%[?(%d%d%d%d)%]?%]?%s*[–-]%s*%[?%[?(%d%d%d%d)%]?%]?$')
	-- intervallo tipo "[[2010]] - in produzione"
	elseif mw.ustring.match(annoprimatv, '^%[?%[?%d%d%d%d%]?%]?%s*[–-]%s*in produzione$') or 
		   mw.ustring.match(annoprimatv, '^%[?%[?%d%d%d%d%]?%]?%s*[–-]%s*in corso$') then
		inizio = annoprimatv:match('^%[?%[?(%d%d%d%d)%]?%]?')
		produzione = ' – in produzione'
	end

	-- verifica che inizio e fine siano compresi tra 1880 e 2030
	inizio = inizio and tonumber(inizio)
	fine = fine and tonumber(fine)
	if not inizio or (inizio and (inizio <= 1880 or inizio >= 2030)) or 
	   (fine and not produzione and (fine <= 1880 or fine >= 2030)) then
		err = true
	end

	return inizio, produzione, fine, err
end

-- Funzione per {{#invoke:FictionTV|categorie_anno}}
-- Restituisce un formato standard per annoprimatv e la categorizzazione.
function p.categorie_anno(frame)
	local args, inizio, produzione, fine, err, cat, finecat
	local ret = {}

	args = getArgs(frame)

	-- annoprimatv e tipofiction sono obbligatori
	if args.annoprimatv and args.tipofiction then
		inizio, produzione, fine, err = parse_annoprimatv(args.annoprimatv)
	else
		err = true
	end

	if err then
		cat = errorCategory
	elseif args.tipofiction:match('[Ww]ebserie') then
		-- webserie non è attualmente categorizzata per anno
	elseif args.tipofiction:match('[Mm]iniserie [Tt][Vv]') and inizio then
		cat = 'Miniserie televisive del ' .. inizio
	elseif args.tipofiction:match('[Ff]ilm [Tt][Vv]') and inizio then
		cat = 'Film per la televisione del ' .. inizio
	elseif args.tipofiction:match('[Ss]erie [Tt][Vv]') then
		if inizio then
			cat = 'Serie televisive iniziate nel ' .. inizio
		end
		if produzione then
			finecat = 'Serie televisive in produzione'
		elseif fine then
			finecat = 'Serie televisive terminate nel ' .. fine
		end
	elseif args.tipofiction:match('[Ss]erial [Tt][Vv]') then
		if inizio then
			cat = 'Serial televisivi iniziati nel ' .. inizio
		end
		if produzione then
			finecat = 'Serial televisivi in produzione'
		elseif fine then
			finecat = 'Serial televisivi terminati nel ' .. fine
		end
	end

	if cat then
		table.insert(ret, string.format('[[Categoria:%s]]', cat))
	end
	if finecat then
		table.insert(ret, string.format('[[Categoria:%s]]', finecat))
	end
	if inizio then
		table.insert(ret, string.format('[[%s]]', inizio))
	end
	if fine and fine ~= inizio then
		table.insert(ret, string.format('-[[%s]]', fine))
	end
	if produzione then
		table.insert(ret, produzione)
	end

	return args.debug and table.concat(ret):gsub('%[%[Cat', '[[:Cat'):gsub(']]', ']]<br />', 2) or
		   table.concat(ret)
end

return p