-- Replacement for [[قالب:منذ زمن]]
local getArgs = require('وحدة:Arguments').getArgs
local numberSpell = require('وحدة:NumberSpell')._main
local yesno = require('وحدة:Yesno')

local p = {}

function p.main( frame )
	local args = getArgs( frame, {
		valueFunc = function( k, v )
			if v then
				v = v:match( '^%s*(.-)%s*$' ) -- Trim whitespace.
				if k == 'منذ' or v ~= '' then
					return v
				end
			end
			return nil
		end
	})
	return p._main( args )
end

function p._main( args )
	-- Initialize variables
	local lang = mw.language.getContentLanguage()
	local ago
	local auto_magnitude_num
	local min_magnitude_num
	local result
	local result_unit
	local magnitude = args.magnitude
	local min_magnitude = args.min_magnitude
	local purge = args.purge
	local spell_out = args.spellout
	local spell_out_max = args.spelloutmax
	
	-- Generate the "ago" string. If ago is the blank string, do nothing - this allows overriding of args.ago
	-- in cases where the module is used to generate something like "where he has worked for the past 20 years."
	ago = args.ago
	if ago and ago ~= '' then
		ago = ' ' .. ago
	elseif not ago then
		ago = ' منذ'
	end

	-- Add a purge link if something (usually "yes") is entered into the purge parameter
	if purge then
		purge = ' <span class="plainlinks">([' .. mw.title.getCurrentTitle():fullUrl('action=purge') .. ' purge])</span>'
	else
		purge = ''
	end

	-- Check that the entered timestamp is valid. If it isn't, then give an error message.
	local noError, inputTime = pcall( lang.formatDate, lang, 'U', args[1], true )
	if not noError then
		return '<strong class="error">Error: first parameter cannot be parsed as a date or time.</strong>'
	end

	-- Store the difference between the current time and the inputted time, as well as its absolute value.
	local timeDiff = lang:formatDate( 'U', nil, true ) - inputTime
	local absTimeDiff = math.abs( timeDiff )

	if magnitude then
		auto_magnitude_num = 0
		min_magnitude_num = timeText[magnitude]
	else
		-- Calculate the appropriate unit of time if it was not specified as an argument.
		local autoMagnitudeData = {
			{ denom = 63115200, amn = 31557600 },
			{ denom = 5356800, amn = 2678400 },
			{ denom = 172800, amn = 86400 },
			{ denom = 7200, amn = 3600 },
			{ denom = 120, amn = 60 }
		}
		for i, t in ipairs( autoMagnitudeData ) do
			if absTimeDiff / t.denom >= 1 then
				auto_magnitude_num = t.amn
				break
			end
		end
		auto_magnitude_num = auto_magnitude_num or 1
		if min_magnitude then
			min_magnitude_num = timeText[min_magnitude]
		else
			min_magnitude_num = -1
		end
	end

	if not min_magnitude_num then
		-- Default to seconds if an invalid magnitude is entered.
		min_magnitude_num = 1
	end

	local magnitude_num = math.max( min_magnitude_num, auto_magnitude_num )
	local result_num = math.floor ( absTimeDiff / magnitude_num )

	local punctuation_key, suffix
	if timeDiff >= 0 then -- Past
		if result_num == 1 then
			punctuation_key = 1
		else
			punctuation_key = 2
		end
		suffix = ago
	else -- Future
		if result_num == 1 then
			punctuation_key = 3
		else
			punctuation_key = 4
		end
		suffix = ' زمن'
	end
	result_unit = timeUnits[ magnitude_num ][ punctuation_key ]

	-- Convert numerals to words if appropriate.
	spell_out_max = tonumber( spell_out_max ) -- Would cause script errors if not a number.
	local result_num_text
	if ( spell_out == 'auto' and 1 <= result_num and result_num <= 9 and result_num <= ( spell_out_max or 9 ) )
		or ( yesno( spell_out ) and 1 <= result_num and result_num <= 100 and result_num <= ( spell_out_max or 100 ) )
	then
		result_num_text = numberSpell( result_num )
	else
		result_num_text = tostring( result_num )
	end

	result = result_num_text .. ' ' .. result_unit .. suffix -- Spaces for suffix have been added in earlier.
	return result .. purge
end

-- Table to convert entered text values to numeric values.
timeText = {
	['seconds'] = 1,
	['minutes'] = 60,
	['hours'] = 3600,
	['days'] = 86400,
	['weeks'] = 604800,
	['months'] = 2678400,
	['years'] = 31557600
}

-- Table containing tables of possible units to use in output.
timeUnits = {
	[1] = { 'ثانية', 'ثوان', "second's", "ثوان'" },
	[60] = { 'دقيقة', 'دقائق', "minutes'", "دقائق'" },
	[3600] = { 'ساعة', 'ساعات', "hour's", "ساعات'" },
	[86400] = { 'يوم', 'أيام', "day's", "أيام'" },
	[604800] = { 'أسبوع', 'أسابيع', "week's", "أسابيع'" },
	[2678400] = { 'شهر', 'شهور', "month's", "شهور'" },
	[31557600] = { 'سنة', 'سنوات', "year's", "سنوات'" }
}

return p