Questo modulo contiene funzioni di appoggio al template {{Infobox nave}}


local p = {}
local getArgs = require('Module:Arguments').getArgs
local error_flag = false

---============================================================
-- Save argument in table
---============================================================
local function dump(t, ...)
	local args = {...}
	for _, s in ipairs(args) do
		table.insert(t, s)
	end
end

---============================================================
-- Round to precision
---============================================================
function round(num, idp)
	local pow = idp or 0
	if idp then
		local mult = 10^pow
		local adder = (num >= 0 and 0.5) or -0.5
		return math.floor(num * mult + adder) / mult
	end
	return math.floor(num + 0.5)
end

---============================================================
-- Autorounding based on the magnitude of value
---============================================================
local function round_auto(num)
	local  base = - math.floor(math.log10(math.abs(num))) + 1 
	if base < -2 then base = -2 end
	return round(num, base)
end

---============================================================
-- format value using non breaking spaces to separate three digit group
--============================================================
local function sep_thousand(value)
	local formatted = value
	while true do
		formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1&nbsp;%2')
		if (k==0) then
			break
		end
	end
	return formatted
end

---============================================================
-- Convert string to number ("," are considered as ".")
---============================================================
local function get_value(s)
	if s then
		local try_convert = s:gsub(",", ".")
		return tonumber(try_convert)
	end
end

---============================================================
-- Format a number as string replacin "." with "," and adding
-- no breaking space as separator between thousands 
---============================================================
local function format_value(value)
	if value then
		local s = tostring(value)
		s = s:gsub("%.", ",")
		return sep_thousand(s)
	end
end

---============================================================
-- Return formatted error message and set error_flag to true
---============================================================
local function add_error(ignore_error, msg) 
	if ignore_error then return '' end
	error_flag = true
	return '<sup class="error" style="font-size:small;">' .. msg .. '</sup>'
end

local function format_m(m1_raw, m2_raw, factor, ignore_error)
	if not (m1_raw or m2_raw) then
		return
	end	
	local m1_value = get_value(m1_raw)
	local m2_value = get_value(m2_raw)
	if m1_value and not m2_raw then
		m2_value = round_auto(m1_value * factor)
	end
	if m2_value and not m1_raw then
		m1_value = round_auto(m2_value / factor)
	end
	local m1_formatted, m2_formatted
	if m1_value then
		m1_formatted = format_value(tostring(m1_value))
	elseif m1_raw then
		m1_formatted = m1_raw ..  add_error(ignore_error, "Richiesto un numero")
	end
	if m2_value then
		m2_formatted = format_value(tostring(m2_value))
	elseif m2_raw then
		m2_formatted = m2_raw .. add_error(ignore_error, "Richiesto un numero")
	end
	return m1_formatted, m2_formatted 
end

---============================================================
-- Format speed and convert from knots to km/h (or viceversa)
---============================================================
function p.speed(frame)
	
	local args = getArgs(frame)
	local spd_kn = args[1] 
	local spd_kmh = args[2] 
	local spd_type = args[3]
	if spd_kmh == nil and spd_kn == nil then return '' end
	kn_formatted, kmh_formatted = format_m(spd_kn, spd_kmh, 1.852, true)
	local out = {}
	if spd_type then dump(out, spd_type, ":&nbsp;") end
	if kn_formatted then dump(out, kn_formatted, "&nbsp;[[Nodo (unità di misura)|nodi]]") end
	if kmh_formatted then
		if kn_formatted then dump(out, " (") end
		dump(out, kmh_formatted, "&nbsp;[[Chilometro orario|km/h]]")
		if kn_formatted then dump(out, ")") end
	end
	return table.concat(out)
end

---============================================================
-- Format range data (knots to km/h and nmi to km, or viceversa)
---============================================================
function p.range(frame)
	local args = getArgs(frame)
	local range_nmi = args[1] 
	local range_kn = args[2] 
	local range_type = args[3]
	local range_km = args[4] 
	local range_kmh = args[5]
	local out = {}
	local nmi_formatted, km_formatted = format_m(range_nmi, range_km, 1.852)
	local kn_formatted, kmh_formatted = format_m(range_kn, range_kmh, 1.852)
	if range_type then dump(out, range_type, ":") end
	if nmi_formatted then
		dump(out, nmi_formatted, "&nbsp; [[Miglio nautico|miglia]]" )
		if kn_formatted then
			dump(out, " a ", kn_formatted, "&nbsp;[[nodo (unità di misura)|nodi]]")
		end
	end 
	if km_formatted then
		if nmi_formatted then dump(out, " (") end
		dump(out, km_formatted, "&nbsp;[[chilometro|km]]" )
		if kmh_formatted then
			dump(out, " a ", kmh_formatted, "&nbsp;[[chilometro orario|km/h]]")
		end
		if nmi_formatted then dump (out, ")") end
	end
	-- add category error if necessary
	if error_flag then
		local current_page = mw.title.getCurrentTitle()
		if current_page.namespace == 0 then
			dump(out, "[[Categoria:Errori di compilazione del template Infobox nave]]")
		end
	end
	return table.concat(out)
end

return p