模組:Vgclass

require('strict')
local getArgs = require"Module:Arguments".getArgs
local p = {}

-- 電子遊戲專題橫幅
local vgBannerNameList = {
	"[Ww]iki[Pp]roject[ _][Vv]ideo[ _][Gg]ames",
	"[电電]子[游遊][戏戲][专專][题題]",
	"[Ww]PVG",
	"[wW]pvg",
}

-- banner shell
local bannerShellNameList = {
	"[Ww]ikiProject[ _]banner[ _]shell",
	"[Ww]PBannerShell",
	"[Ww]PBS",
	"[Ww]ikiProjectBanners",
	"[Mm]ultiple[ _]wikiprojects",
	"[Ww]ikiProjectBannerShell",
	"多[个個][专專][题題]",
	"[专專][题題][横橫]幅",
	"[维維]基[专專][题題][横橫]幅",
}

local function getTalkPageContent(pageTitle)
	if pageTitle == nil then return end
	if #pageTitle == 0 then return end
	pageTitle = pageTitle or mw.getCurrentFrame():expandTemplate{title = 'FULLPAGENAME'}
	local pageObj = mw.title.new(pageTitle)
	if pageObj.exists == false then return end
	
	local talkPageContent
	--[[ Extensive
	if pageObj.redirectTarget then
		talkPageContent = pageObj.redirectTarget.talkPageTitle:getContent()
	else
		talkPageContent = pageObj.talkPageTitle:getContent()
	end
	--]]
	talkPageContent = pageObj.talkPageTitle:getContent()  -- 获取讨论页内容
	
	return talkPageContent
end

local function getTemplateString(templateNameList, talkPageContent)
	-- 获取专题模板字符串起始位置
	local templateName, templatePattern, templateBeginIndex
	for _, value in ipairs(templateNameList) do
		templateName = value
		templatePattern = "{{%s*" .. templateName .. "%s*[|}]"
		templateBeginIndex = mw.ustring.find(talkPageContent, templatePattern)
		if templateBeginIndex then break end
	end
	
	-- 提取模板字符串(或返回nil)
	if templateBeginIndex then
		templatePattern = "({{%s*" .. templateName .. ".-}})"
		return mw.ustring.match(talkPageContent, templatePattern, templateBeginIndex)
	end
	
	return
end

local function getClassRating(templateStr)
	local class = mw.ustring.match(templateStr, "|%s*class%s*=%s*(%w+)%s*[|}]") or 'na'
	class = mw.ustring.lower(class)
	
	-- 标准化等级代码
	local classList = mw.loadData('Module:Class/data') 
	for _, v in ipairs(classList) do
		if class == v.code then return v.code end
		if class == v.name then return v.code end
		if class == v.name2 then return v.code end
		for _, w in ipairs(v.alias) do
			if class == w then return v.code end
		end
	end
	
	return 'na'
end

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

function p._getClass(args)
	local talkPageContent = getTalkPageContent(args[1])
	if talkPageContent == nil then return end
	
	-- 尋找電子遊戲專題模板
	local templateStr = getTemplateString(vgBannerNameList, talkPageContent)
	if templateStr == nil then return end  -- 非電子遊戲專題內容,不敢評!
	local rating = getClassRating(templateStr)  -- 提取電子遊戲專題評級
	if rating == 'na' then
		-- 尋找 banner shell 及其評級
		templateStr = getTemplateString(bannerShellNameList, talkPageContent)
		if templateStr == nil then return 'na' end
		return getClassRating(templateStr)
	else
		return rating
	end
end

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

function p._main(args)
	local class = p._getClass(args)
	local classIcon
	
	if class then
		classIcon = mw.getCurrentFrame():expandTemplate{ title = 'Class/icon', args = { class, ['style'] = "padding-right: 0.25em; ".. (args.style or ''), ['css-class'] = "vgc-class" } }
	else
		classIcon = ''
	end
	
	return classIcon .. "[[" .. args[1] .. "]]"
end

return p