--[[
* 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_annoprimavisione(anno_prima_visione, annoprimatv)
	local inizio, produzione, fine, err

	-- annoprimatv senza intervallo, esempio "[[2010]]" (con o senza wikilink)
	if annoprimatv:match('^%d%d%d%d$') or annoprimatv:match('^%[%[%d%d%d%d%]%]$') then
		inizio = annoprimatv:match('^%[?%[?(%d%d%d%d)%]?%]?$')
		fine = inizio
	-- intervallo tipo "[[2010]]-[[2015]]" (con o senza wikilink)
	elseif mw.ustring.match(annoprimatv, '^%d%d%d%d%s*[–-]%s*%d%d%d%d$') or
		   mw.ustring.match(annoprimatv, '^%[%[%d%d%d%d%]%]%s*[–-]%s*%[%[%d%d%d%d%]%]$') or
		   mw.ustring.match(annoprimatv, '^%[%[%d%d%d%d%]%]%s*[–-]%s*%d%d%d%d$') or
		   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/corso" (con o senza wikilink)
	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 produzione$') or
		   mw.ustring.match(annoprimatv, '^%d%d%d%d%s*[–-]%s*in corso$') 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

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

	-- verifica che inizio e fine siano compresi tra 1880 e 2030
	inizio = tonumber(inizio)
	fine = tonumber(fine)
	if not inizio or inizio <= 1880 or inizio >= 2030 or 
	   (fine 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)

	-- con il parametro nocatperanno restituisce direttamente annoprimatv
	if args.nocatperanno == 'true' then
		return args.annoprimatv
	end

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

	if err then
		cat = errorCategory
	elseif args.tipofiction == 'webserie' then
		-- webserie non è attualmente categorizzata per anno
	elseif args.tipofiction == 'miniserie tv' then
		cat = 'Miniserie televisive del ' .. inizio
	elseif args.tipofiction == 'film tv' then
		cat = 'Film per la televisione del ' .. inizio
	elseif args.tipofiction == 'serie tv' 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 == 'serial tv' 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 mw.title.getCurrentTitle().namespace == 0 or args.debug then
		if cat then
			table.insert(ret, string.format('[[Categoria:%s]]', cat))
		end
		if finecat then
			table.insert(ret, string.format('[[Categoria:%s]]', finecat))
		end
	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%1]]<br />') or
		   table.concat(ret)
end

return p