local getArgs = require('وحدة:Arguments').getArgs

local p = {}

local subpages = {}

function p.add_subpage(subpage)
	for i = 1, #subpages, 1 do
		if subpages[i] == subpage then
			return
		end
	end
	subpages[#subpages + 1] = subpage
end

function p.get_subpages(args)
	if args.baseText == args.subpageText then
		return
	end
	
    local title = mw.title.new(args.baseText)
	local content = title.getContent(title)

	if content == nil then
		return
	end

	for link in string.gmatch(content, '%[%[.-%]%]') do
		link = string.gsub(link, '%[%[تصنيف:.-%]%]', '')
		link = string.gsub(link, '%[%[تصنيف:.-%]%]', '')
		link = string.gsub(link, '%[%[ملف:.-%]%]', '')
		link = string.gsub(link, '%[%[ملف:.-%]%]', '')
		link = string.gsub(link, '%[%[صورة:.-%]%]', '')
		link = string.gsub(link, '%[%[صورة:.-%]%]', '')
		link = string.gsub(link, '%|.-%]%]', ']]')
		link = string.gsub(link, '%[%[/', '[[' .. args.baseText .. '/')
		link = string.gsub(link, "%/%]%]", "]]")
		
		link = string.sub(link, 3, string.len(link) - 2)

		if string.sub(link, 1, string.len(args.baseText) + 1) == args.baseText .. '/' then
			p.add_subpage(link)
		end
	end
end

function p.subpage_navbar(frame)
	local args = getArgs(frame)
	local title = mw.title.getCurrentTitle()
	args.baseText = title.baseText
	args.subpageText = title.subpageText
	return p._subpage_navbar(args)
end

function p._subpage_navbar(args)
	local result = {}
	
	result[#result + 1] = '<div style="display: table; width: 100%; margin-top: 1em; border: 1px solid #a2a9b1; background: #f8f9fa;">'
	result[#result + 1] = '<div style="display: table-cell; width: 33%; padding: 0.33em 0.5em 0.33em 0.5em; text-align: right;">'
	result[#result + 1] = p.subpage_previous(args) .. '</div>'
	result[#result + 1] = '<div style="display: table-cell; width: 34%; padding: 0.33em 0.5em 0.33em 0.5em; text-align: center;">'
	result[#result + 1] = '[[' .. args.baseText .. ']]</div>'
	result[#result + 1] = '<div style="display: table-cell; width: 33%; padding: 0.33em 0.5em 0.33em 0.5em; text-align: left;">'
	result[#result + 1] = p.subpage_next(args) .. '</div></div>'
	return table.concat(result)
end

function p.subpage_next(frame)
	local args = getArgs(frame)
	local title = mw.title.getCurrentTitle()
	args.baseText = title.baseText
	args.subpageText = title.subpageText
	return p._subpage_next(args)
end

function p._subpage_next(args)
	if #subpages == 0 then
		p.get_subpages(args)
	end
	
	for i = 1, #subpages - 1, 1 do
		subpage = subpages[i]
		subpage = string.gsub(subpage, '.*/', '')
		if subpage == args.subpageText then
			return '[[' .. subpages[i + 1] .. '|' .. string.gsub(subpages[i + 1], '.*/', '') .. ' &rarr;]]'
		end
	end
	return ''
end

function p.subpage_previous(frame)
	local args = getArgs(frame)
	local title = mw.title.getCurrentTitle()
	args.baseText = title.baseText
	args.subpageText = title.subpageText
	return p._subpage_previous(args)
end

function p._subpage_previous(args)
	if #subpages == 0 then
		p.get_subpages(args)
	end
	
	for i = 2, #subpages, 1 do
		subpage = subpages[i]
		subpage = string.gsub(subpage, '.*/', '')
		if subpage == args.subpageText then
			return '[[' .. subpages[i - 1] .. '|&larr; ' .. string.gsub(subpages[i - 1], '.*/', '') .. ']]'
		end
	end
	return ''
end

return p