-- placeholder for subpage, will be written later
-- rewrite {{九龍新界巴士簡單列表}} etc. into module to reduce load
local getArgs = require('Module:Arguments').getArgs
local alldata = {
	klnt = require('Module:香港巴士/klnt');
	hki = require('Module:香港巴士/hki');
	crh = require('Module:香港巴士/crh');
	lantau = require('Module:香港巴士/lantau');
	aex = require('Module:香港巴士/aex')
}
local comp = {
	kmb = "九龍巴士"; city = "城巴";
	mtr = "港鐵巴士"; lantau = "新大嶼山巴士";
	mtrshut = "港鐵接駁巴士"; crh = "過海隧道巴士";
	longwin = "龍運巴士"; disney = "迪士尼樂園巴士";
	ltshut = "大嶼山北部穿梭巴士"; 	airshut = "機場快綫穿梭巴士";
	reshut = "香港居民巴士"; db = "愉景灣巴士";
}
local p = {}

local dir = {'↺', '→', '⇄', '→<br/>←', '↺<br/>←', '→<br/>↺', '↺<br/>↺'}

function split(inputstr, sep)
        if sep == nil then
                sep = "%s"
        end
        local t={}
        for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
                table.insert(t, str)
        end
        return t
end

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

function p._main(frame, args)
	local grp = ''
	if args.l == "klnt" or args.l == "九龍" or args.l == "新界" or args.l == "九龍新界" then grp = "klnt"
	elseif args.l == "hki" or args.l == "港島" or args.l == "香港島" then grp = "hki"
	elseif args.l == "crh" or args.l == "過海" then grp = "crh"
	elseif args.l == "lantau" or args.l == "大嶼山" then grp = "lantau"
	elseif args.l == "aex" or args.l == "機場" then grp = "aex"
	end
	local data = alldata[grp]
	local out = ""
	-- Main module code goes here.
	-- return args
	if args[1] == nil or args[1] == "" then
		return '\n|-\n| colspan=4 |  沒有任何巴士路線'
	end
	for i = 1, 100, 1 do
		if not(args[i]) then break end
		local special = string.find('' .. args[i], "_s")
		local route = split(args[i], "_s")[1]
		local spi = split(args[i], "_s")[2] or ''
		out = out .. '\n|-\n| style="text-align: right; padding-right: 10px" |'
		if data[route] then
			out = out .. "[[" .. comp[data[route].company] .. route
			if string.match(route, "^%d$") then out = out .. "號" end
			if data[route].company == "mtr" or data[route].company == "mtrshut"
			or data[route].company == "airshut" then out = out .. "綫"
			else out = out .. "線" end
			out = out .. "|" .. route .. "]]"
			out = out .. '\n| style="text-align: right; white-space: nowrap" | '
			if special then
				local dirstr = ''
				if data[route]['special' .. spi .. '_direction'] then
					dirstr = dir[data[route]['special' .. spi .. '_direction']+1] end
				out = out .. frame:preprocess( data[route]['special' .. spi .. '_endpoint1'] )
						  .. '\n| style="text-align: center; padding: 0px 8px 0px 8px" |'
						  .. dirstr
						  .. '\n| style="white-space: nowrap" |'
						  .. frame:preprocess( data[route]['special' .. spi .. '_endpoint2'] )
						  .. '\n| style="padding-left: 10px" |'
						  .. frame:preprocess( data[route]['special' .. spi .. '_status'] )
			else
				local dirstr = ''
				if data[route]['direction'] then
					dirstr = dir[data[route]['direction']+1] end
				out = out .. frame:preprocess( data[route]['endpoint1'] )
						  .. '\n| style="text-align: center; padding: 0px 8px 0px 8px" |'
						  .. dirstr
						  .. '\n| style="white-space: nowrap" |'
						  .. frame:preprocess( data[route]['endpoint2'] )
						  .. '\n| style="padding-left: 10px" |'
						  .. frame:preprocess( data[route]['status'] )
			end
		else
			out = out .. '\n|' .. route .. '|| colspan="3" style="text-align: center" | (沒有資料,請加入至[[Module:香港巴士/' .. grp .. ']]。)'
		end
	end
	return out
end

p.list = p.main

function p.tbl(frame)
	local args = getArgs(frame)
	local grp = ''
	if args.l == "klnt" or args.l == "九龍" or args.l == "新界" or args.l == "九龍新界" then grp = "klnt"
	elseif args.l == "hki" or args.l == "港島" or args.l == "香港島" then grp = "hki"
	elseif args.l == "crh" or args.l == "過海" then grp = "crh"
	elseif args.l == "lantau" or args.l == "大嶼山" then grp = "lantau"
	elseif args.l == "aex" or args.l == "機場" then grp = "aex"
	end
	local data = alldata[grp]
	local out = ""
	-- Main module code goes here.
	-- return args
	if args[1] == nil or args[1] == "" then
		return '\n|-\n| colspan=3 | 沒有任何巴士路線'
	end
	for i = 1, 100, 1 do
		if not(args[i]) then break end
		local special = string.find('' .. args[i], "_s")
		local route = split(args[i], "_s")[1]
		local spi = split(args[i], "_s")[2] or ''
		out = out .. '\n|-\n| style="text-align: right" |'
		if data[route] then
			out = out .. "[[" .. comp[data[route].company] .. route
			if string.match(route, "^%d$") then out = out .. "號" end
			if data[route].company == "mtr" or data[route].company == "mtrshut"
			or data[route].company == "airshut" then out = out .. "綫"
			else out = out .. "線" end
			out = out .. "|" .. route .. "]]"
			out = out .. '||'
			if special then
				out = out .. data[route]['special' .. spi .. '_normal'] .. "||"
						  .. frame:preprocess( data[route]['special' .. spi .. '_status'] )
			else
				out = out .. data[route]['normal'] .. "||"
						  .. frame:preprocess( data[route]['status'] )
			end
		else
			out = out .. '\n|' .. route .. '|| colspan="2" style="text-align: center" | (沒有資料,請加入至[[Module:香港巴士/' .. grp .. ']]。)'
		end
	end
	return out
end

function p.data(frame)
	local args = getArgs(frame)
	local grp = ''
	if args.l == "klnt" or args.l == "九龍" or args.l == "新界" or args.l == "九龍新界" then grp = "klnt"
	elseif args.l == "hki" or args.l == "港島" or args.l == "香港島" then grp = "hki"
	elseif args.l == "crh" or args.l == "過海" then grp = "crh"
	elseif args.l == "lantau" or args.l == "大嶼山" then grp = "lantau"
	elseif args.l == "aex" or args.l == "機場" then grp = "aex"
	end
	local data = alldata[grp]
	local out = ""
	
	local route = args[1]
	local prop = args[2]
	-- Main module code goes here.
	-- return args
	if route == nil or route == "" or prop == nil or prop == "" then
		return '沒有填寫巴士路線參數'
	end
	
	local out = ''
	if data[route] then
		if data[route][prop] then out = data[route][prop]
		else out = '未找到該路綫的' .. prop .. '資料,請複查[[Module:香港巴士/' .. grp .. ']]。' end
	else out = '未有資料,請加入至[[Module:香港巴士/' .. grp .. ']]。' end
	return out
end

return p