模組:沙盒/Artoria2e5/Infobox artist

--[[ Work in progress:
* [ ]  Fill up logic
* [ ]  Fill up aliases
]]--

-- This module implements {{藝人}} and {{Infobox artist}}.
-- Yes, I am very very greedy.
-- Open project: Wikidata integration

local infobox = require('Module:Infobox').infobox
local infoimg = requite('Module:InfoboxImage').main
local frame = nil

-- deps/subpages
-- Open project: Convert to table lookup (for plain switch tmpls) and go over 32K
-- 这谁有心情让代码更长的话改成查表吧……
local function title_return(arg1)
    return frame:expandTemplate{title = '藝人/title_return', args = {arg1}}
end
local function type_return(arg1)
    return frame:expandTemplate{title = '藝人/type_return', args = {arg1}}
end
local function hCard_class(arg1)
	return frame:expandTemplate{title = '藝人/hCard_class', args = {arg1}}
end
local function color_selector(arg1)
	return frame:expandTemplate{title = '藝人/color_selector', args = {arg1}}
end
local function lang_selector(arg1)
	return frame:expandTemplate{title = '藝人/lang', args = {arg1}}
end
local function tracking(arg1)
	return frame:expandTemplate{title = '藝人/tracking', args = {arg1}}
end
-- end deps


-- TODO: PoC, not filled up yet
local arg_aliases = {
	['honorific_prefix'] = {'honorific prefix', '前缀尊称', '前綴尊稱'},
	['name'] = {'名字', '姓名'},
	['honorific_suffix'] = {'honorific suffix', '后缀尊称', '後綴尊稱'},
	['type'] = {'类型', '類型', 'Background'},
	['embed'] = {'嵌入'},
	['image'] = {'imagename', 'image_name', 'Img', '圖片', '图片', '圖像', '图像'},
	['landscape'] = {'Landscape', 'landscape', '補正', '补正'},
	['imagesize'] = {'image_size', 'Img_size', '圖片尺寸', '图片尺寸', '圖像大小', '图像大小'},
	['Img_alt'] = {'alt', '圖片替代', '图片替代', '圖像替代', '图像替代'},
	['caption'] = {'imagecaption', 'image_caption', 'Img_capt', '圖片簡介', '图片简介', '圖像說明', '图像说明'},
	['realname'] = {'real_name', 'Real_name', 'full name', '本名', '原名'},
	
}

local function main(args, frame)
	-- Fallback magic
	local oldArgsMeta = getmetatable(args) or {}
	local newArgsMeta = {}
	for k, v in ipairs(oldArgsMeta) do
        newArgsMeta[k] = v
    end
    newArgsMeta.__index = function (t, k)
    	if oldArgsMeta.__index ~= nil then
    	    local val = oldArgsMeta.__index(t, k)
    	    if val ~= nil then
	    		return val
	        end
        end
		for _, v in ipairs(arg_aliases[k] or {}) do
		    if t[v] ~= nil and t[v] ~= '' then
		    	return t[v]
	    	end
		end
		return nil
	end
	setmetatable(args, newArgsMeta)
	
	-- Prepare to call infobox
	local ibargs = {}
	ibargs['child'] = args['embed']:lower()
	ibargs['decat'] = 'yes'
	ibargs['bodyclass'] = 'vcard'
	ibargs['bodystyle'] = 'width:275px; font-size: 95%;'
    ibargs['title'] = title_return(args['type'] or
    	                           (yes(ibargs['child']) and '演艺生涯' or 'none'))
    -- Note: although '' is not falsy, getArgs cleans that out to match mw behavior.
    ibargs['above'] = table.concat(table_filter({
    	(args['honorific_prefix'] and
    		'<span class="honorific-prefix" style="font-size: small">' .. args['honorific_prefix'] .. '</span>'),
    	'<span class="' .. hcard_class(args['type'] or '艺人') .. '">' .. (args['name'] or mw.title.getCurrentTitle().baseText) .. '</span>',
    	(args['honorific_suffix'] and
    		'<span class="honorific-suffix" style="font-size: small">' .. args['honorific_suffix'] .. '</span>'),
    }, (function(v,k,t) return v end)), '<br />')
    ibargs['abovestyle'] = 'text-align: center; font-size: 120%; background:' .. color_selector(args['type'] or '艺人')
    ibargs['headerstyle'] = ((not yes(ibargs['child'])) and color_selector(args['type'] or '艺人') or '')
    ibargs['labelstyle'] = 'white-space: nowrap'
    ibargs['image'] = infoimg {
    	["image"] = args["image"],
    	["size"] = (yes(args['landscape']) and ((args['imagesize'] and min(300,tonumber(imagesize)) or 300) .. 'x200px') or (args['imagesize'] or '')),
    	["sizedefault"] = "frameless",
    	["upright"] = "1",
    	["alt"] = args['Img_alt'],
    	["suppressplaceholder"] = yes
    }
	ibargs["caption"] = args["caption"]
	
	ibargs["class1"] = "title role"
	ibargs["header1"] = "<includeonly>" .. (not yes(args['embed']) and (type_return(args['type'] or '艺人或艺术家') or ''))
	
	ibargs["class2"] = "additional-name"
	ibargs["label2"] = "本名"
	ibargs["data2"] = args['realname']
	
	ibargs["label3"] = "原文名"
	ibargs["data3"] = nil -- todo
    
    -- 我想了想,没有用 infobox{ ... } 这个语法的我真仁慈啊!(大雾)
	return infobox(ibargs)
end

local function table_filter(t, filterIter)
    local out = {}
    for k, v in ipairs(t) do
        if filterIter(v, k, t) then out[k] = v end
    end
    return out
end

local function yes(s)
	return s:lower() == "yes"
end

local function _entry (frame_)
	local args = getArgs(frame_)
	frame = frame_
	return p[fname](args)
end

local function _luacall(args, frame_)
	frame = frame_
	return main(args)
end


return {
	["infobox_artist"] = _entry,
	["_artist_actual"] = _luacall
}