local getArgs = require('Module:Arguments').getArgs

local p = {}

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

local function error(text)
	return '*' .. require('Module:Error').error{ 'Invalid task config: ' .. text, tag = 'p' }
end 

local function has_value(array, val)
    for index, value in ipairs(array) do
        if value == val then
            return true
        end
    end
    return false
end

local function lowerfirst(s) 
	return s:sub(1,1):lower()..s:sub(2)
end 

local function get_namespaces(namespaces_list)
	local list = {}
	for token in string.gmatch(namespaces_list, "%d+") do
		local ns_id = tonumber(token)
		table.insert(list, ns_id ~= 0 and lowerfirst(mw.site.namespaces[ns_id]['name']) or 'main')
	end
	return table.concat(list, ', ')
end 	


function p._main(args)
	
	-- check for invalid parameters
	for param, value in pairs(args) do
		if not has_value({'bot','task','min_edits','duration','title_regex','title','namespace','action','summary','summary_regex','notify','email'}, param) then
			return error('unknown parameter: ' .. param .. ' used')
		end
	end
	
	if not args.bot or not args.task then
		return error('required parameters "bot" and/or "task" missing')
	end
	
	-- set defaults if not given
	args.min_edits = args.min_edits or 1
	args.duration = args.duration or '3 days'
	args.action = args.action or 'edit'
	
	local str = string.format("* [[User:%s|%s]]: %s. '''Frequency''': ≥ %s %s%s in the last %s. ", 
		args.bot, args.bot,
		args.task, 
		args.min_edits, 
		args.action == 'edit' and 'edit' or args.action .. ' action',
		args.min_edits == 1 and '' or 's', 
		args.duration
	)
	
	if args.title_regex then
		str = str .. string.format("'''Pages''': matching <code>%s</code> %s", args.title_regex, 
			args.namespace and string.format("in namespace: %s.", get_namespaces(args.namespace)) or '')
		if args.title then
			return error('do not use both title_regex and title parameters')
		end 
	elseif args.title then
		str = str .. string.format("'''Page''': [[%s]] ", args.title)
	elseif args.namespace then
		str = str .. string.format("In namespace: %s. ", get_namespaces(args.namespace))
	end
	
	if args.summary_regex then
		str = str .. string.format("With summary matching <code>%s</code>. ", args.summary_regex)
		if args.summary then
			return error('do not use both summary_regex and summary parameters')
		end 
	elseif args.summary then
		str = str .. string.format("With summary <code>%s</code>. ", args.summary)
	end
	
	if args.notify then
		local alertpage = mw.title.new(args.notify, 3).fullText
		str = str .. string.format("<b>Will notify</b> [[%s|%s]] in case of issues. ", alertpage, args.notify)
	end
	
	if args.email then
		if string.find(args.email, '@') then
			str = str .. string.format("<b>Will notify</b> by email in case of issues. ", args.email)
		else 
			local userPage = mw.title.new(args.email, 2).fullText
			str = str .. string.format("<b>Will notify</b> [[%s|%s]] by email in case of issues. ", userPage, args.email)
		end
	end
	
	return str
end

return p