local p = {}

local getArgs = require('Module:Arguments').getArgs
local locmap = require('Module:Location map')

local function switcherSeparate(s)
	if s == nil then return {} end
	local retval = {}
	for i in string.gmatch(s .. '#', '([^#]*)#') do
		i = mw.text.trim(i)
		retval[#retval + 1] = (i ~= '' and i)
	end
	return retval
end

function p.container
	local caption_list = {}
	if not args then
		args = getArgs
	end
	if not map then
		if args[1] then
			map = {}
			for mapname in string.gmatch do
				map[#map + 1] = locmap.getMapParams
			end
			if args['caption'] or args['설명'] then
				if ( args['caption'] or args['설명'] ) == "" then
					while #caption_list < #map do
						caption_list[#caption_list + 1] = args['caption'] or args['설명']
					end
				else
					for caption in mw.text.gsplit do
						caption_list[#caption_list + 1] = caption
					end
				end
			end
			if #map == 1 then map = map[1] end
		else
			map = locmap.getMapParams
		end
	end
	if type(map) == 'table' then
		local placeslist = mw.text.gsplit
		local permaplaces = {}
		local numbermaps = #map
		local count = 0
		for i = 1,numbermaps do
			permaplaces[i] = {}
		end
		for place in placeslist do
			table.insert
			count = count + 1
		end
		local altmaps = switcherSeparate(args.AlternativeMap or args['대체지도'])
		if #altmaps > #map then
			error(string.format)
		end
		local overlays = switcherSeparate(args.overlay_image)
		if #overlays > #map then
			error(string.format)
		end
		if #caption_list > #map then
			error(string.format)
		end
		local outputs = {}
		args.autoSwitcherLabel = true
		for k,v in ipairs(map) do
			args.AlternativeMap = altmaps[k]
			args.overlay_image = overlays[k]
			args.caption = caption_list[k]
			args.places = table.concat(permaplaces[k])
			outputs[k] = p.container
		end
		return ''
	else
		return locmap.top .. ((args.places or args['장소']) and (args.places or args['장소']):gsub or '') .. locmap.bottom
	end
end

local function manyMakeArgs
	if n == 1 then
		return {
			lat = fullArgs.lat1 or fullArgs.lat or fullArgs['위도1'] or fullArgs['위도'],
			long = fullArgs.long1 or fullArgs.long or fullArgs['경도1'] or fullArgs['경도'],
			coordinates = fullArgs.coordinates1 or fullArgs.coordinates or fullArgs['좌표1'] or fullArgs['좌표'],
			lat_deg = fullArgs.lat1_deg or fullArgs.lat_deg or fullArgs['위도도1'] or fullArgs['위도1도'] or fullArgs['위도도'],
			lat_min = fullArgs.lat1_min or fullArgs.lat_min or fullArgs['위도분1'] or fullArgs['위도1분'] or fullArgs['위도분'],
			lat_sec = fullArgs.lat1_sec or fullArgs.lat_sec or fullArgs['위도초1'] or fullArgs['위도1초'] or fullArgs['위도초'],
			lat_dir = fullArgs.lat1_dir or fullArgs.lat_dir or fullArgs['남북1'] or fullArgs['남북'],
			lon_deg = fullArgs.lon1_deg or fullArgs.lon_deg or fullArgs['경도도1'] or fullArgs['경도1도'] or fullArgs['경도도'],
			lon_min = fullArgs.lon1_min or fullArgs.lon_min or fullArgs['경도분1'] or fullArgs['경도1분'] or fullArgs['경도분'],
			lon_sec = fullArgs.lon1_sec or fullArgs.lon_sec or fullArgs['경도초1'] or fullArgs['경도1초'] or fullArgs['경도초'],
			lon_dir = fullArgs.lon1_dir or fullArgs.lon_dir or fullArgs['동서1'] or fullArgs['동서'],
			outside = fullArgs.outside1 or fullArgs.outside or fullArgs['바깥쪽1'] or fullArgs['바깥쪽'],
			mark = fullArgs.mark1 or fullArgs.mark or fullArgs['표시1'] or fullArgs['표시'],
			marksize = fullArgs.mark1size or fullArgs.marksize or fullArgs['표시크기1'] or fullArgs['표시크기'] or fullArgs['표시1크기'],
			link = fullArgs.link1 or fullArgs.link or fullArgs['링크1'] or fullArgs['링크'],
			label = fullArgs.label1 or fullArgs.label or fullArgs['꼬리표1'] or fullArgs['꼬리표'],
			label_size = fullArgs.label1_size or fullArgs.label_size or fullArgs['꼬리표크기1'] or fullArgs['꼬리표1크기'] or fullArgs['꼬리표크기'],
			label_width = fullArgs.label1_width or fullArgs.label_width or fullArgs['꼬리표너비1'] or fullArgs['꼬리표1너비'] or fullArgs['꼬리표너비'],
			position = fullArgs.position1 or fullArgs.pos1 or fullArgs.position or fullArgs.pos or fullArgs['위치1'] or fullArgs['위치'],
			background = fullArgs.background1 or fullArgs.bg1 or fullArgs.background or fullArgs.bg or fullArgs['배경1'] or fullArgs['배경']
	else
		return {
			lat = fullArgs['lat' .. n] or fullArgs['위도' .. n],
			long = fullArgs['long' .. n] or fullArgs['경도' .. n],
			coordinates = fullArgs['coordinates' .. n] or fullArgs['좌표' .. n],
			lat_deg = fullArgs['lat' .. n .. '_deg'] or fullArgs['위도도' .. n] or fullArgs['위도' .. n .. '도'],
			lat_min = fullArgs['lat' .. n .. '_min'] or fullArgs['위도분' .. n] or fullArgs['위도' .. n .. '분'],
			lat_sec = fullArgs['lat' .. n .. '_sec'] or fullArgs['위도초' .. n] or fullArgs['위도' .. n .. '초'],
			lat_dir = fullArgs['lat' .. n .. '_dir'] or fullArgs['남북' .. n],
			lon_deg = fullArgs['lon' .. n .. '_deg'] or fullArgs['경도도' .. n] or fullArgs['경도' .. n .. '도'],
			lon_min = fullArgs['lon' .. n .. '_min'] or fullArgs['경도분' .. n] or fullArgs['경도' .. n .. '분'],
			lon_sec = fullArgs['lon' .. n .. '_sec'] or fullArgs['경도초' .. n] or fullArgs['경도' .. n .. '초'],
			lon_dir = fullArgs['lon' .. n .. '_dir'] or fullArgs['동서' .. n],
			outside = fullArgs['outside' .. n] or fullArgs['바깥' .. n ],
			mark = fullArgs['mark' .. n] or fullArgs['표시' .. n],
			marksize = fullArgs['mark' .. n .. 'size'] or fullArgs['표시' .. n .. '크기'] or fullArgs['표시크기' .. n],
			link = fullArgs['link' .. n] or fullArgs['링크' .. n],
			label = fullArgs['label' .. n] or fullArgs['꼬리표' .. n],
			label_size = fullArgs['label' .. n .. '_size'] or fullArgs['꼬리표' .. n .. '크기'] or fullArgs['꼬리표크기' .. n],
			label_width = fullArgs['label' .. n .. '_width'] or fullArgs['꼬리표' .. n .. '너비'] or fullArgs['꼬리표너비' .. n],
			position = fullArgs['position' .. n] or fullArgs['pos' .. n] or fullArgs['위치' .. n],
			background = fullArgs['background' .. n] or fullArgs['bg' .. n] or fullArgs['배경' .. n]
	end
end

function p.many
	if not args then
		args = getArgs
	end
	if not args[1] then
		args[1] = 'World'
	end
	if not map then
		map = {}
		for mapname in string.gmatch do
			map[#map + 1] = locmap.getMapParams
		end
		if #map ~= 1 then
			local outputs = {}
			args.autoSwitcherLabel = true
			for k,v in ipairs(map) do
				outputs[k] = p.many
			end
			return ''
		end
		map = map[1]
	end
	local marks = {}
	local markhigh
	if args.markhigh or args['표시높이'] then
		mw.log('Removed parameter markhigh used.')
		local parent = frame:getParent
		if parent then
			mw.log('Parent is ' .. parent:getTitle)
		end
		mw.logObject
		markhigh = true
	end
	for k, v in pairs(args) do -- @todo change to uargs once we have that
		if v then
			-- 영어 변수 처리
			if string.sub == '_deg' then
				k = string.sub
			end
			if string.sub == 'lat' then
				k = tonumber(string.sub)
				if k then
					table.insert
				end
			elseif string.sub == 'coordinates' then
				k = tonumber(string.sub)
				if k then
					table.insert
				end
			end

			-- 한국어 변수 처리
			if k and string.find then
				k = string.gsub
				k = tonumber(k)
				if k then
					table.insert
				end
			elseif k and string.find then
				k = string.gsub
				k = tonumber(k)
				if k then
					table.insert
				end
			elseif k and string.find then
				k = string.gsub
				k = string.gsub
				k = tonumber(k)
				if k then
					table.insert
				end
			elseif k and string.find then
				k = string.gsub
				k = tonumber(k)
				if k then
					table.insert
				end
			end
		end
	end
	table.sort(marks)
	if marks[1] ~= 1 and (args.lat or args['위도'] or args.lat_deg or args['위도도'] or args.coordinates or args['좌표']) then
		table.insert
	end
	local body = ''
	for _, v in ipairs(marks) do
		-- don't try to consolidate this into the above loop. ordering of elements from pairs is unspecified
		body = body .. tostring( locmap.mark(frame, manyMakeArgs, map) )
		if args['mark' .. v .. 'high'] or args['표시' .. v .. '높이'] or args['표시높이' .. v] then
			mw.log('Removed parameter mark' .. v .. 'high used.')
			local parent = frame:getParent
			if parent then
				mw.log('Parent is ' .. parent:getTitle)
			end
			mw.logObject
			markhigh = true
		end
	end
	args.label = nil -- there is no global label
	return locmap.top .. body .. locmap.bottom .. (markhigh and '제거된 변수를 사용하는 문서' or '')
end

function p.load
	if not args then
		args = getArgs
	end
	local dataModule = mw.loadData(frame.args[1])
	if not map then
		map = {}
		for mapname in string.gmatch do
			map[#map + 1] = locmap.getMapParams
		end
		if #map ~= 1 then
			local outputs = {}
			args.autoSwitcherLabel = true
			for k,v in ipairs(map) do
				outputs[k] = p.load
			end
			return ''
		end
		map = map[1]
	end
	local marks = {}
	if dataModule.marks then
		for k,markArgs in ipairs(dataModule.marks) do
			marks[k] = tostring(locmap.mark)
		end
	end
	if dataModule.secondaryModules then
		for _,modname in ipairs(dataModule.secondaryModules) do
			for _,markArgs in ipairs(mw.loadData(modname).marks) do
				marks[#marks + 1] = tostring(locmap.mark)
			end
		end
	end
	return locmap.top .. table.concat(marks) .. locmap.bottom
end