--[[  
Questo modulo è in appoggio al template Tassobox 
]]

local p = {}
local cfg = mw.loadData("Modulo:Tassobox/Configurazione")
local getArgs = require('Module:Arguments').getArgs

local function check_value(valore)
	return valore or missing_table[mw.ustring.lower(valore)]
end

--===================================================================
-- Richiamo di un'immagine di file mancante appropriata al 
-- regno/phylum/classe/ordine/ecc della voce in cui è inserito
-- il tassobox
--===================================================================
function p.missing_image(frame)
	local args = getArgs(frame, {wrapper = 'Template:Tassobox'})
	local missing_link
	-- testa in ordine dal più specifico al più generico la classificazione di un essere vivente
	for _,test in ipairs(param_to_check) do
		missing_link = check_value(args[test])
		if missing_link then break end
	end 
	-- se ha non trovato il valore nella tabella ricade nell'immagine di default
	missing_link = missing_link or "File:Unknown missing 1.png"
	return '[[' .. missing_link ..'|220px|Immagine di ' .. mw.title.getCurrentTitle().text .. ' mancante]]'
end

----------------------------------------------------------------------------------
-- Ritorna la configurazione della tabella per le ere geologiche
----------------------------------------------------------------------------------
function p.ere_geologiche_table(frame)
-- Restituisce una tabella delle ere geologiche presenti in configurazione
 
	local root = mw.html.create('table')
	root
		:addClass('wikitable sortable')
		:tag('tr')
			:tag('th'):wikitext('Era'):done()
			:tag('th'):wikitext('Inizio'):done()
			:tag('th'):wikitext('Successiva'):done()
			:tag('th'):wikitext('Colore'):done()
	for name, values in pairs(cfg.ere_geologiche) do
		local start = values['start'] or "?"
		local next_era = values['next_era'] or "?"
		local color 
		if values['color'] then 
			color = mw.html.create('span')
				:cssText("display:inline-block;border:solid grey 1px;width:1em;height:1em")
				:css('background', values['color'])
				:wikitext(' ')
		else
			color = mw.html.create('span'):done()
		end
		root:tag('tr')
			:tag('td'):wikitext(name):done()
			:tag('td'):css('text-align', 'right'):wikitext(start):done()
			:tag('td'):wikitext(next_era):done()
			:tag('td'):css('text-align', 'center'):node(color)
	end
	return tostring(root)
end

--===================================================================
-- Genera lo stato di conservazione di uno specie
--===================================================================
function p.stato_conservazione(frame)

	local args = getArgs(frame, {wrapper = 'template:Tassobox'})
	local statocons = args.statocons
	if statocons == nil then return end
	local iucn =  { ['iucn2.3'] = true, ['iucn3.1'] = true}
	statocons = mw.ustring.lower(statocons)
	statocons = cfg.stato_alias[statocons] or statocons
	local row_stato = cfg.stato[statocons]
	if row_stato == nil then 
		local cat_error = ''
		if mw.title.getCurrentTitle().namespace == 0 then
			cat_error = '[[Categoria:Errori di compilazione del template Tassobox]]'
		end
		return '<strong class=\"error\">Parametro statocons non valido</div>' .. cat_error 
	end
	local immagine
	if row_stato.versione then 
		local versione = args.statocons_versione or ''
		versione = (iucn[versione] and versione) or (statocons == 'ex' and 'none') or 'iucn3.1'
		if versione == 'none' then 
			immagine = 'Status_none_EX.svg'
		else
			immagine = mw.ustring.format(row_stato.image, versione)
		end
	else
		immagine = row_stato.image
	end
	local ref = args.statocons_ref
	ref = (ref and  '<span style=font-size:80%>' .. ref .. '</span>') or '' 
	local dataestinzione = ''
	if row_stato.data then
		dataestinzione = row_stato.text_data or ''
		if args.dataestinzione ~= nil then
			if dataestinzione == '' then
				dataestinzione = '(' .. args.dataestinzione .. ')'
			else
				dataestinzione = dataestinzione .. ' (' .. args.dataestinzione .. ')'
			end
		end
		dataestinzione = mw.ustring.format('<span style="color:%s;">%s</span>', row_stato.color_data or row_stato.color, dataestinzione)
	end
	local base_msg = 
'<div style="margin:0 auto; text-align:center; background:white;">'..
	'<div>\[\[Image:%s|200px\]\]</div>'..
	'<div style="color:%s;font-weight:bold;font-size:80%%;">%s%s%s</span></div>' ..
'</div>'
	return mw.ustring.format(base_msg, immagine, row_stato.color, row_stato.text, dataestinzione, ref)
end

return p