local p = {}

local Mapframe = require('Module:Mapframe')

-- This function was imported from [[Module:Maplink]], should probably be "required" instead
function parseCoords(coords)
	local parts = mw.text.split((mw.ustring.match(coords,'[_%.%d]+[NS][_%.%d]+[EW]') or ''), '_')

	local lat_d = tonumber(parts[1])
	local lat_m = tonumber(parts[2]) -- nil if coords are in decimal format
	local lat_s = lat_m and tonumber(parts[3]) -- nil if coords are either in decimal format or degrees and minutes only
	local lat = lat_d + (lat_m or 0)/60 + (lat_s or 0)/3600
	if parts[#parts/2] == 'S' then
		lat = lat * -1
	end

	local long_d = tonumber(parts[1+#parts/2])
	local long_m = tonumber(parts[2+#parts/2]) -- nil if coords are in decimal format
	local long_s = long_m and tonumber(parts[3+#parts/2]) -- nil if coords are either in decimal format or degrees and minutes only
	local long = long_d + (long_m or 0)/60 + (long_s or 0)/3600
	if parts[#parts] == 'W' then
		long = long * -1
	end

	return lat, long
end

-- Entry point for templates
function p.main(frame)
	local parent = frame.getParent(frame)
	local output = p._main(parent.args)
	return frame:preprocess(output)
end

-- Entry point for modules
function p._main(_args)
	local targs = {}
	local pins = {}
	local del = _args['delimiter'] or '<>'
	local post_title_offset = _args['has_jurisdiction_metadata'] and 1 or 0
	local jurisdiction_filter = _args['jurisdiction_filter']
	for k,v in pairs(_args) do
		if k then
			k = tostring(k)
			if k:match('^%s*%d+%s*$') then
				local vals = mw.text.split(v, '%s*' .. del .. '%s*')
				local title = vals[1					] or ''
				local coord = vals[2 + post_title_offset] or ''
				local mtype = vals[3 + post_title_offset] or _args['type'] or 'point'
				local markr = vals[4 + post_title_offset] or _args['marker'] or ''
				local msize = vals[5 + post_title_offset] or _args['marker-size'] or ''
				local mcolr = vals[6 + post_title_offset] or _args['marker-color'] or ''
				local lat,lon = parseCoords(coord)
				lat,lon = tonumber(lat),tonumber(lon)
				if jurisdiction_filter then
					local jurisdiction = vals[2]
					if mw.ustring.match(jurisdiction, jurisdiction_filter, 1) then
						table.insert(pins, {lat or 90, lon or -180, title, coord, mtype, markr, msize, mcolr})
					end
				else
					table.insert(pins, {lat or 90, lon or -180, title, coord, mtype, markr, msize, mcolr})
				end
			elseif k == 'delimiter' or k == 'type' or k == 'marker' 
				or k == 'marker-size' or k == 'marker-color' or k:match('%d%s$') then
			else
				targs[k] = v
			end
		end
	end
	table.sort(pins, function (a, b) 
			return a[1] > b[1] or (a[1] == b[1] and a[2] < b[2])
			end)
	for k,v in ipairs(pins) do
		if k == 1 then k = '' end
		targs['title' .. k] = v[3] ~= '' and v[3]
		targs['coord' .. k] = v[4] ~= '' and v[4]
		targs['type' .. k] = v[5] ~= '' and v[5]
		targs['marker' .. k] = v[6] ~= '' and v[6]
		targs['marker-size' .. k] = v[7] ~= '' and v[7]
		targs['marker-color' .. k] = v[8] ~= '' and v[8]
	end
	
	return Mapframe._main(targs)
end

return p