模块:重庆轨道交通车站

---@type fun(frame: frame, options: { trim: boolean?, removeBlanks: boolean?, valueFunc: (fun(key: any, value: any): any)?, frameOnly: boolean?, parentOnly: boolean?, parentFirst: boolean?, wrappers: string[]?, readOnly: boolean?, noOverwrite: boolean? }?): { [string]: string }
local getArgs = require('Module:Arguments').getArgs

local NavboxFunc = require("Module:Navbox")._navbox

local adj = require('Module:Adjacent stations')

local p = {}

local operational = {
	'0', '1', '2', '3', '4', '5', '江跳', '6', '9', '10', '18'
}

local construction = {
	'7', '15', '17', '24', '27', '璧铜', --'永川'
}

local projected = {
	'8', '12', '21'
}

local branches = {
	['3'] = { '空港' },
	['6'] = { '国博' },
}

local aliases = {
	['3a'] = '3',
	['6i'] = '6',
}

local bounds = {
	['5'] = { '江跳' },
	['江跳'] = { '5' },
}

---@param frame frame
---@param l string
---@return string
local function getLineStyle(frame, l)
	return adj._color({ 'CRT', l }, frame)
end

---@param frame frame
---@param l string
---@return string
local function getLineTitle(frame, l)
	return adj._line({ 'CRT', l }, frame)
end

---@param l string
---@return string
local function getLineName(l)
	if l == '0' then
		return '环线'
	elseif tonumber(l) then
		return l .. '号线'
	else
		return l .. '线'
	end
end

---@param l string
---@return string
local function getLineFullName(l)
	if l == '江跳' then
		return '重庆市郊铁路' .. getLineName(l)
	else
		return '重庆轨道交通' .. getLineName(l)
	end
end

---@param frame frame
---@param l string
---@return string
local function getLineList(frame, l)
	if branches[l] then
		local child_args = {
			'child',
			groupstyle = 'color:#3a3a3a; background:#ececec; width:4em; text-align:center;',
			liststyle  = 'width:auto;',
			list0      = frame:expandTemplate { title = '重庆轨道交通车站/' .. getLineName(l) }
		}
		for i, b in ipairs(branches[l]) do
			local branchName = getLineName(b)
			child_args['group' .. i] = branchName
			child_args['list' .. i] = frame:expandTemplate { title = '重庆轨道交通车站/' .. branchName }
		end
		return NavboxFunc(child_args)
	else
		return frame:expandTemplate { title = '重庆轨道交通车站/' .. getLineName(l) }
	end
end

---@param frame frame
---@return string
function p.navbox(frame)
	-- Make map
	---@type { [string]: boolean }
	local operational_map  = {}
	---@type { [string]: boolean }
	local construction_map = {}
	---@type { [string]: boolean }
	local projected_map    = {}

	for _, l in ipairs(operational) do
		operational_map[l] = false
	end
	for _, l in ipairs(construction) do
		construction_map[l] = false
	end
	for _, l in ipairs(projected) do
		projected_map[l] = false
	end

	-- Scan args
	local args = getArgs(frame)
	local num_int = 0
	local num_arg = 0
	local single_line = ''
	local cats = {}

	if args.construction or args.projected then
		-- do nothing
	else
		for a, l in pairs(aliases) do
			if args[a] then
				args[a] = nil
				args[l] = '1'
			end
		end
		for l, _ in pairs(operational_map) do
			if args[l] then
				operational_map[l] = true
				num_int = num_int + 1
				num_arg = num_arg + 1
				single_line = l
				if not args.nocat then
					table.insert(cats, '[[Category:' .. getLineFullName(l) .. '车站]]')
				end
				if bounds[l] then
					for _, b in ipairs(bounds[l]) do
						operational_map[b] = true
					end
				end
			end
		end
		for l, _ in pairs(construction_map) do
			if args[l] then
				construction_map[l] = true
				num_arg = num_arg + 1
				single_line = l
				if not args.nocat then
					table.insert(cats, '[[Category:' .. getLineFullName(l) .. '车站]]')
				end
			end
		end
		for l, _ in pairs(projected_map) do
			if args[l] then
				projected_map[l] = true
				num_arg = num_arg + 1
				single_line = l
				if not args.nocat then
					table.insert(cats, '[[Category:' .. getLineFullName(l) .. '车站]]')
				end
			end
		end
		if num_arg == 0 then
			for l, _ in pairs(operational_map) do
				operational_map[l] = true
			end
			for l, _ in pairs(construction_map) do
				construction_map[l] = true
			end
			for l, _ in pairs(projected_map) do
				projected_map[l] = true
			end
		end
	end

	-- Build navbox
	local navbox_args = {
		name       = '重庆轨道交通车站',
		state      = 'autocollapse',
		bodystyle  = 'width:100%; vertical-align:middle;',
		titlestyle = 'background:#00843d; color:white;',
		groupstyle = 'background:#ececec; width:4em; text-align:center;',
		liststyle  = 'width:auto;'
	}

	if args.construction or args.projected then
		if args.construction then
			navbox_args.title =
			'[[重庆轨道交通|<span style="color:white">重庆轨道交通</span>]]在建线路[[重庆轨道交通车站列表|<span style="color:white">车站</span>]]'
			for i, l in ipairs(construction) do
				navbox_args['group' .. i .. 'style'] = frame:expandTemplate { title = 'box-shadow border', args = { 'r', '#' .. getLineStyle(frame, l), '5px' } }
				navbox_args['group' .. i] = getLineTitle(frame, l)
				navbox_args['list' .. i] = getLineList(frame, l)
			end
		else
			navbox_args.title =
			'[[重庆轨道交通|<span style="color:white">重庆轨道交通</span>]]规划线路[[重庆轨道交通车站列表|<span style="color:white">车站</span>]]'
			for i, l in ipairs(projected) do
				navbox_args['group' .. i .. 'style'] = frame:expandTemplate { title = 'box-shadow border', args = { 'r', '#' .. getLineStyle(frame, l), '5px' } }
				navbox_args['group' .. i] = getLineTitle(frame, l)
				navbox_args['list' .. i] = getLineList(frame, l)
			end
		end
	else
		if num_arg > 0 then
			navbox_args.state = 'expanded'
			if num_int > 1 and not args.nocat then
				table.insert(cats, '[[Category:重庆轨道交通换乘车站|' .. num_int .. ']]')
			end
		end
		if num_arg == 1 then
			local line_name = getLineFullName(single_line)
			navbox_args.name = '重庆轨道交通车站/' .. getLineName(single_line)
			navbox_args.title = '[[' .. line_name .. '|<span style="color:white">' .. line_name .. '</span>]]车站'
			navbox_args.titlestyle = 'background:#' .. adj._color({ 'CRT', single_line }, frame) .. '; color:white'
			navbox_args.list0 = getLineList(frame, single_line)
		else
			navbox_args.title =
			'[[重庆轨道交通|<span style="color:white">重庆轨道交通</span>]][[重庆轨道交通车站列表|<span style="color:white">车站</span>]]'
			local i = 1
			for _, l in ipairs(operational) do
				if operational_map[l] then
					navbox_args['group' .. i .. 'style'] = frame:expandTemplate { title = 'box-shadow border', args = { 'r', '#' .. getLineStyle(frame, l), '5px' } }
					navbox_args['group' .. i] = getLineTitle(frame, l)
					navbox_args['list' .. i] = getLineList(frame, l)
					i = i + 1
				end
			end
			local construction_args = {
				'child',
				title      = '在建线路',
				titlestyle = 'background:#ececec; color:#6b6b6b;',
				groupstyle = 'background:#ececec; width:4em; text-align:center;',
				liststyle  = 'width:auto;'
			}
			local j = 1
			for _, l in ipairs(construction) do
				if construction_map[l] then
					construction_args['group' .. j .. 'style'] = frame:expandTemplate { title = 'box-shadow border', args = { 'r', '#' .. getLineStyle(frame, l), '5px' } }
					construction_args['group' .. j] = getLineTitle(frame, l)
					construction_args['list' .. j] = getLineList(frame, l)
					j = j + 1
				end
			end
			if j > 1 then
				if num_arg > 0 then
					construction_args.state = 'expanded'
				end
				navbox_args['list' .. i] = NavboxFunc(construction_args)
				i = i + 1
			end
			local projected_args = {
				'child',
				title      = '规划线路',
				titlestyle = 'background:#ececec; color:#6b6b6b;',
				groupstyle = 'background:#ececec; width:4em; text-align:center;',
				liststyle  = 'width:auto;'
			}
			local k = 1
			for _, l in ipairs(projected) do
				if projected_map[l] then
					projected_args['group' .. k .. 'style'] = frame:expandTemplate { title = 'box-shadow border', args = { 'r', '#' .. getLineStyle(frame, l), '5px' } }
					projected_args['group' .. k] = getLineTitle(frame, l)
					projected_args['list' .. k] = getLineList(frame, l)
					k = k + 1
				end
			end
			if k > 1 then
				if num_arg > 0 then
					projected_args.state = 'expanded'
				end
				navbox_args['list' .. i] = NavboxFunc(projected_args)
				i = i + 1
			end
		end
	end

	return NavboxFunc(navbox_args) .. table.concat(cats)
end

return p