Module:Number list

local export = {}

local form_types = {
	{key = "cardinal", display = "[[cairdinal nummer|Cairdinal]]"},
	{key = "ordinal", display = "[[ordinal nummer|Ordinal]]"},
	{key = "adverbial", display = "[[adverbial nummer|Adverbial]]"},
	{key = "multiplier", display = "[[multiplier|Multiplier]]"},
	{key = "distributive", display = "[[distributive nummer|Distributive]]"},
	{key = "collective", display = "[[collective nummer|Collective]]"},
	{key = "fractional", display = "[[fractional|Fractional]]"},
}


function export.show_box(frame)
	local m_links = require("Module:links")
	
	local params = {
		[1] = {required = true},
		[2] = {required = true, type = "number"},
	}
	
	local args = require("Module:parameters").process(frame:getParent().args, params)
	
	local lang = args[1] or "und"
	local cur_num = args[2] or 2
	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	
	-- Get the data from the data module
	local m_data = require("Module:number list/data/" .. lang:getCode())
	local cur_data = m_data.numbers[cur_num]
	
	if not cur_data then
		error('The number "' .. cur_num .. '" is not found in the "numbers" table in Module:number list/data/' .. lang:getCode() .. '.')
	end
	
	-- Go over each number and make links
	local forms = {}
	local cur_type
	
	for _, form_type in ipairs(form_types) do
		if cur_data[form_type.key] then
			table.insert(forms, "     ''" .. form_type.display .. "'' : " ..
				m_links.full_link({lang = lang, term = cur_data[form_type.key], tr = cur_data[form_type.key .. "_tr"]}))
			
			-- If this number is the current page, then store the key for later use
			if lang:makeEntryName(cur_data[form_type.key]) == mw.title.getCurrentTitle().fullText then
				cur_type = form_type.key
			end
		end
	end
	
	if not cur_type and mw.title.getCurrentTitle().nsText ~= "Template" then
		error("The current page name does not match any of the numbers listed in the data module for " .. cur_num .. ". Check the data module or the spelling of the page.")
	end
	
	-- Current number in header
	local cur_display = m_links.full_link({lang = lang, sc = sc, alt = cur_num, tr = "-"})
	
	if cur_data["numeral"] then
		cur_display = m_links.full_link({lang = lang, alt = cur_data["numeral"], tr = "-"}) .. "<br/><span style=\"font-size: smaller;\">" .. cur_display .. "</span>"
	end
	
	-- Link to previous number
	local prev_data = m_data.numbers[cur_num - 1]
	local prev_display = ""
	
	if prev_data and prev_data[cur_type] then
		prev_display = m_links.full_link({lang = lang, term = prev_data[cur_type], alt = "&nbsp;&lt;&nbsp;&nbsp;" .. (cur_num - 1), tr = "-"})
	end
	
	-- Link to next number
	local next_data = m_data.numbers[cur_num + 1]
	local next_display = ""
	
	if next_data and next_data[cur_type] then
		next_display = m_links.full_link({lang = lang, term = next_data[cur_type], alt = (cur_num + 1) .. "&nbsp;&nbsp;&gt;&nbsp;", tr = "-"})
	end
	
	-- Link to number times ten and divided by ten
	-- Show this only if the number is a power of ten times a number 1-9 (that is, of the form x000...)
	local up_display
	local down_display
	
	-- This test *could* be done numerically, but this is nice and simple and it works.
	if mw.ustring.find(tostring(cur_num), "^[1-9]0*$") then
		local up_data = m_data.numbers[cur_num * 10]
		
		if up_data and up_data[cur_type] then
			up_display = m_links.full_link({lang = lang, term = up_data[cur_type], alt = (cur_num * 10), tr = "-"})
		end
		
		-- Only divide by 10 if the number is at least 10
		if cur_num >= 10 then
			local down_data = m_data.numbers[cur_num / 10]
			
			if down_data and down_data[cur_type] then
				down_display = m_links.full_link({lang = lang, term = down_data[cur_type], alt = (cur_num / 10), tr = "-"})
			end
		end
	end
	
	return [=[{| class="floatright" cellpadding="5" cellspacing="0" style="background: #ffffff; border: 1px #aaa solid; border-collapse: collapse; margin-top: .5em;" rules="all" 
|+ ''']=] .. lang:getCanonicalName() .. [=[ numbers'''
]=] .. (up_display and [=[|- style="text-align: center; background:#dddddd;"
|
| style="font-size:smaller;" | ]=] .. up_display .. [=[

|
]=] or "") .. [=[|- style="text-align: center;"
| style="min-width: 6em; font-size:smaller; background:#dddddd;" | ]=] .. prev_display .. [=[

! style="min-width: 6em; font-size:larger;" | ]=] .. cur_display .. [=[

| style="min-width: 6em; font-size:smaller; background:#dddddd;" | ]=] .. next_display .. [=[

]=] .. (down_display and [=[|- style="text-align: center; background:#dddddd;"
|
| style="font-size:smaller;" | ]=] .. down_display .. [=[

|
]=] or "") .. [=[|-
| colspan="3" | ]=] .. table.concat(forms, "<br/>") .. [=[

|}]=]	
end


function export.show_box_manual(frame)
	local m_links = require("Module:links")
	local num_type = frame.args["type"]
	
	local args = {}
	--cloning parent's args while also assigning nil to empty strings
	for pname, param in pairs(frame:getParent().args) do
		if param == "" then args[pname] = nil
        else args[pname] = param
        end
	end
	
	local lang = args[1] or (mw.title.getCurrentTitle().nsText == "Template" and "und") or error("Language code has not been specified. Please pass parameter 1 to the template.")
	local sc = args["sc"]; 
	local headlink = args["headlink"]
	local wplink = args["wplink"]
	local alt = args["alt"]
	local tr = args["tr"]
	
	local prev_symbol = args[2]; if prev_symbol and mw.text.trim(prev_symbol) == "" then prev_symbol = nil end
	local cur_symbol = args[3];  if cur_symbol and mw.text.trim(cur_symbol) == "" then cur_symbol = nil end
	local next_symbol = args[4]; if next_symbol and mw.text.trim(next_symbol) == "" then next_symbol = nil end
	
	local prev_term = args[5]; if prev_term and mw.text.trim(prev_term) == "" then prev_term = nil end
	local next_term = args[6]; if next_term and mw.text.trim(next_term) == "" then next_term = nil end
	

	local cardinal_term = args["card"]; local cardinal_alt = args["cardalt"]; local cardinal_tr = args["cardtr"]
	
	local ordinal_term = args["ord"]; local ordinal_alt = args["ordalt"]; local ordinal_tr = args["ordtr"]
	
	local adverbial_term = args["adv"]; local adverbial_alt = args["advalt"]; local adverbial_tr = args["advtr"]
	
	local multiplier_term = args["mult"]; local multiplier_alt = args["multalt"]; local multiplier_tr = args["multtr"]
	
	local distributive_term = args["dis"]; local distributive_alt = args["disalt"]; local distributive_tr = args["distr"]
	
	local collective_term = args["coll"]; local collective_alt = args["collalt"]; local collective_tr = args["colltr"]
	
	local fractional_term = args["frac"]; local fractional_alt = args["fracalt"]; local fractional_tr = args["fractr"]
	
	local optional1_title = args["opt"]
	local optional1_term = args["optx"]; local optional1_alt = args["optxalt"]; local optional1_tr = args["optxtr"]
	
	local optional2_title = args["opt2"]
	local optional2_term = args["opt2x"]; local optional2_alt = args["opt2xalt"]; local optional2_tr = args["opt2xtr"]
	

	lang = require("Module:languages").getByCode(lang) or error("The language code \"" .. lang .. "\" is not valid.")
	sc = (sc and (require("Module:scripts").getByCode(sc) or error("The script code \"" .. sc .. "\" is not valid.")) or nil)
	
	require("Module:debug").track("number list/" .. lang:getCode())
	
	if sc then
		require("Module:debug").track("number list/sc")
	end
	
	if headlink then
		require("Module:debug").track("number list/headlink")
	end
	
	if wplink then
		require("Module:debug").track("number list/wplink")
	end
	
	if alt then
		require("Module:debug").track("number list/alt")
	end
	
	if cardinal_alt or ordinal_alt or adverbial_alt or multiplier_alt or distributive_alt or collective_alt or fractional_alt or optional1_alt or optional2_alt then
		require("Module:debug").track("number list/xalt")
	end
	
	local is_reconstructed = lang:getType() == "reconstructed" or (lang:getType() ~= "appendix-constructed" and mw.title.getCurrentTitle().nsText == "Appendix")
	alt = alt or (is_reconstructed and "*" or "") .. mw.title.getCurrentTitle().subpageText
	
	if num_type == "cardinal" then
		cardinal_term = (is_reconstructed and "*" or "") .. mw.title.getCurrentTitle().subpageText
		cardinal_alt = alt
		cardinal_tr = tr
	elseif num_type == "ordinal" then
		ordinal_term = (is_reconstructed and "*" or "") .. mw.title.getCurrentTitle().subpageText
		ordinal_alt = alt
		ordinal_tr = tr
	end
	
	local header = lang:getCanonicalName() .. " " .. num_type .. " numbers"
	
	if headlink then
		header = "[[" .. headlink .. "|" .. header .. "]]"
	end
	
	local previous = ""
	
	if prev_term or prev_symbol then
		previous = m_links.full_link({lang = lang, sc = sc, term = prev_term, alt = "&nbsp;&lt;&nbsp;&nbsp;" .. prev_symbol, tr = "-"})
	end
	
	local current = m_links.full_link({lang = lang, sc = sc, alt = cur_symbol, tr = "-"})
	
	local next = ""
	
	if next_term or next_symbol then
		next = m_links.full_link({lang = lang, sc = sc, term = next_term, alt = next_symbol .. "&nbsp;&nbsp;&gt;&nbsp;", tr = "-"})
	end
	
	local forms = {}
	
	if cardinal_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[cardinal number|Cardinal]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = cardinal_term, alt = cardinal_alt, tr = cardinal_tr}))
	end
	
	if ordinal_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[ordinal number|Ordinal]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = ordinal_term, alt = ordinal_alt, tr = ordinal_tr}))
	end
	
	if adverbial_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[adverbial number|Adverbial]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = adverbial_term, alt = adverbial_alt, tr = adverbial_tr}))
	end
	
	if multiplier_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[multiplier|Multiplier]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = multiplier_term, alt = multiplier_alt, tr = multiplier_tr}))
	end
	
	if distributive_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[distributive number|Distributive]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = distributive_term, alt = distributive_alt, tr = distributive_tr}))
	end
	
	if collective_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[collective number|Collective]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = collective_term, alt = collective_alt, tr = collective_tr}))
	end
	
	if fractional_term then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''[[fractional|Fractional]]'' : " .. m_links.full_link({lang = lang, sc = sc, term = fractional_term, alt = fractional_alt, tr = fractional_tr}))
	end
	
	if optional1_title then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''" .. optional1_title .. "'' : " .. m_links.full_link({lang = lang, sc = sc, term = optional1_term, alt = optional1_alt, tr = optional1_tr}))
	end
	
	if optional2_title then
		table.insert(forms, " &nbsp;&nbsp;&nbsp; ''" .. optional2_title .. "'' : " .. m_links.full_link({lang = lang, sc = sc, term = optional2_term, alt = optional2_alt, tr = optional2_tr}))
	end
	
	local footer = ""
	
	if wplink then
		footer =
			"[[w:" .. lang:getCode() .. ":Main Page|" .. lang:getCanonicalName() .. " Wikipedia]] article on " ..
			m_links.full_link({lang = lang, sc = sc, term = "w:" .. lang:getCode() .. ":" .. wplink, alt = alt, tr = tr})
	end
	
	return [=[{| class="floatright" cellpadding="5" cellspacing="0" style="background: #ffffff; border: 1px #aaa solid; border-collapse: collapse; margin-top: .5em;" rules="all" 
|+ ''']=] .. header .. [=['''
|-
| style="width: 64px; background:#dddddd; text-align: center; font-size:smaller;" | ]=] .. previous .. [=[

! style="width: 98px; text-align: center; font-size:larger;" | ]=] .. current .. [=[

| style="width: 64px; text-align: center; background:#dddddd; font-size:smaller;" | ]=] .. next .. [=[

|-
| colspan="3" | ]=] .. table.concat(forms, "<br/>") .. [=[

|-
| colspan="3" style="text-align: center; background: #dddddd;" | ]=] .. footer .. [=[

|}]=]
end

return export