local cfg = mw.loadData('Module:Navbar/configuration')

local function get_title_arg
	local title_arg = 1
	if is_collapsible then title_arg = 2 end
	if template then title_arg = 'template' end
	return title_arg
end

local function choose_links
	-- The show table indicates the default displayed items.
	-- view, talk, edit, hist, move, watch
	-- TODO: Move to configuration.
	local show = {true, true, true, false, false, false}
	if template then
		show[2] = false
		show[3] = false
		local index = {t = 2, d = 2, e = 3, h = 4, m = 5, w = 6,
			talk = 2, edit = 3, hist = 4, move = 5, watch = 6}
		-- TODO: Consider removing TableTools dependency.
		for _, v in ipairs(require ('Module:TableTools').compressSparseArray(args)) do
			local num = index[v]
			if num then show[num] = true end
		end
	end

	local remove_edit_link = args.noedit
	if remove_edit_link then show[3] = false end
	
	return show
	
end

local function add_link
	local l
	if link_description.url then
		l = {'[', '', ']'}
	else
		l = {'', ''}
	end
	ul:tag('li')
end

local function make_list
	
	local title = mw.title.new(mw.text.trim(title_text), cfg.title_namespace)
	if not title then
		error(cfg.invalid_title .. title_text)
	end
	local talkpage = title.talkPageTitle and title.talkPageTitle.fullText or ''
	
	-- TODO: Get link_descriptions and show into the configuration module.
	-- link_descriptions should be easier...
	local link_descriptions = {
			['link'] = title.fullText, ['url'] = false },
			['link'] = talkpage, ['url'] = false },
			['link'] = 'Special:EditPage/' .. title.fullText, ['url'] = false },
			['link'] = 'Special:PageHistory/' .. title.fullText, ['url'] = false },
			['link'] = mw.title.new('Special:Movepage'):fullUrl('target='..title.fullText), ['url'] = true },
			['link'] = title:fullUrl('action=watch'), ['url'] = true }

	local ul = mw.html.create('ul')
	if has_brackets then
		ul:addClass(cfg.classes.brackets)
	end
	
	for i, _ in ipairs(displayed_links) do
		if displayed_links[i] then add_link end
	end
	return ul:done
	
end

function p._navbar(args)
	
	-- TODO: We probably don't need both fontstyle and fontcolor...
	local font_style = args.fontstyle
	local font_color = args.fontcolor
	local is_collapsible = args.collapsible
	local is_mini = args.mini
	local is_plain = args.plain
	
	local collapsible_class = nil
	if is_collapsible then
		collapsible_class = cfg.classes.collapsible
		if not is_plain then is_mini = 1 end
		if font_color then
			font_style = (font_style or '') .. '; color: ' .. font_color .. ';'
		end
	end
	
	local navbar_style = args.style
	local div = mw.html.create:tag('div')
	div

	if is_mini then div:addClass(cfg.classes.mini) end

	local box_text = (args.text or cfg.box_text) .. ' '
	 -- the concatenated space guarantees the box text is separated
	if not (is_mini or is_plain) then
		div
	end
	
	local template = args.template
	local displayed_links = choose_links
	local has_brackets = args.brackets
	local title_arg = get_title_arg
	local title_text = args[title_arg] or (':' .. mw.getCurrentFrame:getParent:getTitle)
	local list = make_list
	div:node(list)

	if is_collapsible then
		local title_text_class
		if is_mini then
			title_text_class = cfg.classes.collapsible_title_mini
		else
			title_text_class = cfg.classes.collapsible_title_full
		end
		div:done
	end
	
	local frame = mw.getCurrentFrame
	-- hlist -> navbar is best-effort to preserve old Common.css ordering.
	return frame:extensionTag{
		name = 'templatestyles', args = { src = cfg.hlist_templatestyles }
		name = 'templatestyles', args = { src = cfg.templatestyles }
end

function p.navbar(frame)
	return p._navbar(require('Module:Arguments').getArgs(frame))
end