Documentation[créer] [purger]
local p = {}

--[[
	getPOIGeoJson
	
	Utility method for generating the GeoJSON for a Point of Interest.
]]--
function getPOIGeoJson( location, properties )
	
	--[[ Build the GeoJSON block ]]--
	local geojson = [[ {
		"type": "Feature",
		"geometry": {
			"type": "Point",
			"coordinates": [
				]] .. location.longitude .. [[,
				]] .. location.latitude .. [[
			]
		},
		"properties": {
			]];
		
	--[[ Time to add optional properties ]]--
	local featureProps = {}
	
	--[[ if "title" available, add it ]]--
	if properties.title then
	    table.insert(featureProps, [[ "title": "]] .. mw.text.encode( properties.title ) .. [["]] );
	end
	
	--[[ if "description" available, add it ]]--
	if properties.description then
	    table.insert(featureProps, [[ "description": "]] .. mw.text.encode( properties.description ) .. [["]] );
	end
	
	--[[ if "marker-color" available, add it, otherwise use default ]]--
	local markerColor = properties['marker-color'] or '#3366cc'
    table.insert(featureProps, [[ "marker-color": "]] .. markerColor .. [["]] );
	
	--[[ Join the list of properties ]]--
	geojson = geojson .. table.concat(featureProps, ',')

	--[[ Wrap the GeoJSON block ]]--
	geojson = geojson .. [[
		}
    } ]];
    return geojson
end

--[[
	getExternalGeoshapeGeoJson
	
	Utility method for generating the GeoJSON for a Geoshape.
]]--
function getExternalGeoshapeGeoJson( wikidataId, properties )
	
	--[[ Build the GeoJSON block ]]--
	local geojson = [[ {
		"type": "ExternalData",
		"service": "geoshape",
		"ids": "]] .. wikidataId .. [[",
		"properties": {
			]];
		
	--[[ Time to add optional properties ]]--
	local featureProps = {}
	
	--[[ if "title" available, add it ]]--
	if properties.title then
	    table.insert(featureProps, [[ "title": "]] .. mw.text.encode( properties.title ) .. [["]] );
	end
	
	--[[ if "description" available, add it ]]--
	if properties.description then
	    table.insert(featureProps, [[ "description": "]] .. mw.text.encode( properties.description ) .. [["]] );
	end
	
	--[[ if "description" available, add it ]]--
	if properties.description then
	    table.insert(featureProps, [[ "description": "]] .. mw.text.encode( properties.description ) .. [["]] );
	end
	
	--[[ if "fill" available, add it, otherwise use default ]]--
	local fill = properties['fill'] or '#fc3'
    table.insert(featureProps, [[ "fill": "]] .. fill .. [["]] );
    
	--[[ if "stroke" available, add it, otherwise use default ]]--
	local stroke = properties['stroke'] or '#ac6600'
    table.insert(featureProps, [[ "stroke": "]] .. stroke .. [["]] );
	
	--[[ Join the list of properties ]]--
	geojson = geojson .. table.concat(featureProps, ',')

	--[[ Wrap the GeoJSON block ]]--
	geojson = geojson .. [[
		}
    } ]];
    return geojson
end
--[[
	getExternalGeoshapeGeoJson
	
	Utility method for generating the GeoJSON for a Geoline.
]]--
function getExternalGeolineGeoJson( wikidataId, properties )
	
	--[[ Build the GeoJSON block ]]--
	local geojson = [[ {
		"type": "ExternalData",
		"service": "geoline",
		"ids": "]] .. wikidataId .. [[",
		"properties": {
			]];
		
	--[[ Time to add optional properties ]]--
	local featureProps = {}
	
	--[[ if "title" available, add it ]]--
	if properties.title then
	    table.insert(featureProps, [[ "title": "]] .. mw.text.encode( properties.title ) .. [["]] );
	end
	
	--[[ if "description" available, add it ]]--
	if properties.description then
	    table.insert(featureProps, [[ "description": "]] .. mw.text.encode( properties.description ) .. [["]] );
	end
	
	--[[ if "description" available, add it ]]--
	if properties.description then
	    table.insert(featureProps, [[ "description": "]] .. mw.text.encode( properties.description ) .. [["]] );
	end
	
	--[[ if "fill" available, add it, otherwise use default ]]--
	-- local fill = properties['fill'] or '#fc3'
    -- table.insert(featureProps, [[ "fill": "]] .. fill .. [["]] );
    
	--[[ if "stroke" available, add it, otherwise use default ]]--
	local stroke = properties['stroke'] or '#ac6600'
    table.insert(featureProps, [[ "stroke": "]] .. stroke .. [["]] );
	
	--[[ Join the list of properties ]]--
	geojson = geojson .. table.concat(featureProps, ',')

	--[[ Wrap the GeoJSON block ]]--
	geojson = geojson .. [[
		}
    } ]];
    return geojson
end


--[[
	poi
	
	Public method for generating a mapframe with a Point of Interest.
]]--
function p.poi( frame )
	
	local methodArgs = frame.args;
	
    --[[ Reading arguments passed when invoking the method ]]--
	local latitude = methodArgs[1]
	local longitude = methodArgs[2]
	local zoom = methodArgs[3] or 10
	local title = methodArgs['title'] or ''
	local description = methodArgs['description'] or ''
	local width = methodArgs['w'] or 480
	local height = methodArgs['h'] or 360
	local align = methodArgs['align'] or 'right'
	
    --[[ Building arguments for the mapframe tag ]]--
	local tagArgs = {
		['latitude'] = latitude,
		['longitude'] = longitude,
		['zoom'] = zoom,
		['width'] = width,
		['height'] = height,
		['align'] = align
	}
	
    --[[ Generate GeoJSON ]]--
	local geojson = getPOIGeoJson(
		{ ['latitude'] = latitude, ['longitude'] = longitude },
		{ ['title'] = title, ['description'] = description }
	)
	
    --[[ Generate output ]]--
    return frame:extensionTag{
    	name = 'mapframe',
    	content = geojson,
    	args = tagArgs
	}
end

--[[
	poi
	
	Public method for generating a mapframe with a Point of Interest.
]]--
function p.geoshape( frame )
	
	local methodArgs = frame.args;
	
    --[[ Reading arguments passed when invoking the method ]]--
	local latitude = methodArgs[1]
	local longitude = methodArgs[2]
	local zoom = methodArgs[3] or 10
	local wikidataId = methodArgs[4]
	local title = methodArgs['title'] or ''
	local description = methodArgs['description'] or ''
	local width = methodArgs['w'] or 480
	local height = methodArgs['h'] or 360
	local align = methodArgs['align'] or 'right'
	
    --[[ Building arguments for the mapframe tag ]]--
	local tagArgs = {
		['latitude'] = latitude,
		['longitude'] = longitude,
		['zoom'] = zoom,
		['width'] = width,
		['height'] = height,
		['align'] = align
	}
	
    --[[ Generate GeoJSON ]]--
	local geojson = getExternalGeoshapeGeoJson(
		wikidataId,
		{ ['title'] = title, ['description'] = description }
	)
	
    --[[ Generate output ]]--
    return frame:extensionTag{
    	name = 'mapframe',
    	content = geojson,
    	args = tagArgs
	}
end

function p.geoline( frame )
	
	local methodArgs = frame.args;
	
    --[[ Reading arguments passed when invoking the method ]]--
	local latitude = methodArgs[1]
	local longitude = methodArgs[2]
	local zoom = methodArgs[3] or 10
	local wikidataId = methodArgs[4]
	local title = methodArgs['title'] or ''
	local description = methodArgs['description'] or ''
	local width = methodArgs['w'] or 480
	local height = methodArgs['h'] or 360
	local align = methodArgs['align'] or 'right'
	
    --[[ Building arguments for the mapframe tag ]]--
	local tagArgs = {
		['latitude'] = latitude,
		['longitude'] = longitude,
		['zoom'] = zoom,
		['width'] = width,
		['height'] = height,
		['align'] = align
	}
	
    --[[ Generate GeoJSON ]]--
	local geojson = getExternalGeolineGeoJson(
		wikidataId,
                { ['fill'] = "#fc3", ['stroke'] = "#ac6600" }
	)
	
    --[[ Generate output ]]--
    return frame:extensionTag{
    	name = 'mapframe',
    	content = geojson,
    	args = tagArgs
	}
end

return p