Module:WikiProject

--[[
This implements {{WPBannerMeta}}

]]
require('Module:No globals')

local p = {}

local cfg = {
	["subst_warning"] = {
		["image"] = '[[File:Stop hand nuvola.svg|40px]]',
		["type"] = 'content',
		-- Lua 5.2 gives us access to \z. Pipe dream upgrade.
		["text"] = '<p>It seems that the <i>WikiProject %s</i> banner has ' ..
			'been [[Wikipedia:Substitution|substituted]] on this page ' ..
			'instead of being [[Wikipedia:Transclusion|transcluded]]. ' ..
			'Please undo the edit and type <code>&#123;&#123;%s&#125;&#125;' ..
			'</code> instead.</p>',
		["categories"] = '<includeonly>[[Category:WikiProject banners with formatting errors|SUBST]]' ..
			'[[Category:Pages with incorrectly substituted templates]]</includeonly>'
	},
	["namespace_warning"] = {
		["image"] = '[[File:Ambox important.svg|40px]]',
		["type"] = 'content',
		-- Lua 5.2 gives us access to \z. Pipe dream upgrade.
		["text"] = '<p>The WikiProject banner below should be moved to this ' ..
			'%s\'s <b>[[%s|talk page]]</b>.</p><p style="font-size:95%%">If ' ..
			'this is a demonstration of the template, please set the ' ..
			'parameter %s to prevent this page being miscategorised. %s</p>' ..
			'<b>Note:</b> When merging or renaming banners, categories, ' ..
			'projects or taskforces, you may need to update ' ..
			'[[WP:AALERTS/LIST]] for [[WP:AALERTS|Article Alerts]] to work ' ..
			'correctly (ask for help at [[WT:AALERTS]]).',
		["on_template_page"] = 'If this message appears on the template ' ..
			'page itself, please check that the value of the %s parameter ' ..
			'is correctly set to <kbd>%s</kbd>.',
		["categories"] = '<includeonly>[[Category:WikiProject banners with ' ..
			'formatting errors|%s]]</includeonly>'
	}
}

-- Make it cleaner to initialize 'trivial' variables.
local function arg_or_default(args, from_arg, default)
	if args[from_arg] and args[from_arg] ~= '' then
		return args[from_arg]
	else
		return default
	end
end

local function wikilink(link, display)
	if display then
		return '[[' .. link .. '|' .. display .. ']]'
	else
		return '[[' .. link .. ']]'
	end
end

local function warn_on_subst(subst, project, banner_name, suppress_categories)
	if subst ~= 'SUBST' then
		return ''
	end
	
	local target_name
	if banner_name then
		target_name = mw.title.new( banner_name ).text
	else
		target_name = 'WikiProject ' .. project
	end
	
	local warning = require('Module:Message box').main( 'ombox', {
		image = cfg.subst_warning.image,
		type = cfg.subst_warning.type,
		text = string.format(cfg.subst_warning.text, project, target_name)
	})

	local categories
	if suppress_categories then
		categories = ''
	else
		-- Is the intent of <includeonly> only to add it to the banner invocation
		-- and not elsewhere? TODO: Check docs.
		categories = cfg.subst_warning.categories
	end
	
	return warning .. categories
end

-- move the if category check out of this function
local function is_banner_page(banner_name, project)
	
	local target_name
	if banner_name then
		target_name = banner_name
	else
		target_name = 'Template:WikiProject ' .. project
	end
	
	local current_title = mw.title.getCurrentTitle()
	local prefixed_title = current_title.prefixedText
	local root_title = current_title.nsText .. ':' .. current_title.rootText
	
	return target_name == prefixed_title or target_name == root_title
end

local function warn_on_namespace(category, project, banner_name, suppress_categories)
	local current_title = mw.title.getCurrentTitle()
	local current_namespace = current_title.nsText
	local current_subjectspace = current_title.subjectNsText
	
	if current_namespace ~= current_subjectspace or -- if talk page or
		current_namespace == 'User' then -- if user page
			-- then don't warn
			-- why user pages?
		return ''
	end
	if category == 'no' then return '' end -- if no cat then don't warn?
	if is_banner_page(banner_name, project) then return '' end -- if a banner (sub)page don't warn

	local frame = mw.getCurrentFrame()
	local para_category = frame:expandTemplate{
		title = 'para',
		args = {'category', 'no'}
	}
	local para_banner_name = frame:expandTemplate{
		title = 'para',
		args = {'BANNER_NAME'}
	}

	local on_template_page
	if current_namespace == 'Template' then
		on_template_page = string.format(
			cfg.namespace_warning.on_template_page,
			para_banner_name,
			current_title.nsText .. ':' .. current_title.rootText
		)
	else
		on_template_page = ''
	end
	
	local warning = require('Module:Message box').main( 'ombox', {
		image = cfg.namespace_warning.image,
		type = cfg.namespace_warning.type,
		text = string.format(
			cfg.namespace_warning.text,
			require('Module:Pagetype')._main({}), -- empty args table for pagetype
			current_title.talkPageTitle.fullText,
			para_category,
			on_template_page
		)
	})

	local category
	if suppress_categories then
		category = ''
	else
		local sortkey
		if current_namespace == 'Template' then
			sortkey = "PARAMETER"
		else
			sortkey = "LOCATION"
		end
		
		category = string.format(
			cfg.namespace_warning.categories,
			sortkey
		)
	end
		
	return warning .. category
	
end

local function status_class(project_status)
	local status_classes = {
		['inactive-wikiproject'] = { 'inactive' },
		['semi-active-wikiproject'] = { 'semi-active', 'semiactive' },
		['defunct-wikiproject'] = { 'defunct' },
		['active-wikiproject'] = { 'active' }
	}
	
	for v, k_table in ipairs(status_classes) do
		for _, k in ipairs(k_table) do
			if v == k then 
				return v
			end
		end
	end
end

-- Provides the list of task forces (for now)
local function task_forces(args)
	local task_forces = {}
	for k, v in ipairs(args) do
		if mw.ustring.find(k, 'tf (%d+)') then
			table.insert(task_forces, mw.ustring.find(k, 'tf[ _](%d+)'))
		end
	end
	return task_forces
end


local function render_task_force(task_force, all_task_forces)
	local text = task_force.text
	local image = task_force.image
	local portal = task_force.portal
	local link = task_force.link
	local name = task_force.name
	 -- pretty sure we don't want to deal with nested here
--	local nested = task_force.nested
	-- pretty sure we don't want to deal with exists here
--	local exists = task_force.exists
	local quality = task_force.quality
	local importance = task_force.importance
	local assessment_category = task_force.assessment_category
	local main_category = task_force.main_category
	
	local image_size = all_task_forces.image_size
	
	
end




local function nested_names(args)

	local nested_names = {}

	
	
	for i = 1, 5 do
		if args['tf_' .. i] and args['tf_' .. i] == 'yes' and args['TF_' .. i .. '_NESTED']
		and args['TF_' .. i .. '_NESTED'] ~= '' then
			table.insert(
				nested_names,
				mw.ustring.format(
					'/ %s&nbsp;',
					wikilink(args['TF_' .. i .. '_LINK'], args['TF_' .. i .. '_LINK'])
				)
			)
		end
	end
	table.insert(
		nested_names,
		hook_nested
	)
	return table.concat(nested_names)
end

local function banner(project_status, project_link, project_name)
	local banner = mw.html.create()
	banner:tag('table')
		:addClass('tmbox tmbox-notice mw-collapsible innercollapse wpb')
		:addClass(status_class(project_status))
		:css('height', '0')
		
	local title_row = mw.html.create('tr')
	title_row:addClass('wpb-header')
		:tag('td')
			:cssText('text-align:right; padding:0.3em 1em 0.3em 0.3em; width:50%; font-weight:bold;')
			:wikitext(wikilink(project_link, project_name) .. '')
			:done()
		:tag('th')
			:cssText('text-align:left; width:50%; padding:0.3em;')
			
			:done()

end

function p._main(args)
	
	-- Initialize sufficient stuff to do warnings.
	local subst = arg_or_default(args, 'substcheck', '¬')
	local project = arg_or_default(args, 'PROJECT', nil)
	if not project then error('PROJECT must be defined') end
	
	-- banner_name should maybe default to 'WikiProject' .. project instead
	-- or possibly 'Template:WikiProject' .. project...?
	local banner_name = arg_or_default(args, 'BANNER_NAME', nil)
	-- TODO: When done, remove suppress_categories (or convert).
	local suppress_categories = arg_or_default(args, 'suppress', nil)
	
	local subst_warning = warn_on_subst(subst, project, banner_name, suppress_categories)
	
	local category = arg_or_default(args, 'category', '¬')
	local namespace_warning = warn_on_namespace(category, project, banner_name, suppress_categories)
	
--	if category ~= 'no' and not is_banner_page(banner_name, project) then
--		local banner = banner(args) or '' -- TODO
--	else
--		local template_page_banner = template_page_banner(args) or '' -- TODO
--	end

	local project_status = arg_or_default(args, 'PROJECT_STATUS', 'active')
	local project_link = arg_or_default(args, 'PROJECT_LINK', 'Wikipedia:WikiProject ' .. project)
	
	local banner = banner(project_status, project_link)


	return subst_warning ..
		namespace_warning
--		banner ..
--		template_page_banner
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	return p._main(args, frame)
end

return p