-- 2 DONE!
-- 3  DONE!
-- 4  DONE!
-- 5 [[image:flag of .[svg|gif|png|jpg]|\d+px]] DONE!

local p = {}
local getArgs = require("Module:Arguments").getArgs
local data = mw.loadData("Module:ISO 3166/data/National")

------------F I N D N A M E------------										-- Finds the name in the database

local function findname
	local sqry = p.strip(qry)
	if cdata["name"] and sqry==p.strip(cdata["name"])
		or cdata["isoname"] and sqry==p.strip(cdata["isoname"])
		or not cdata["nocode"] and sqry==code
		or sqry==cdata["alpha3"] or sqry==cdata["numeric"]
		then
		return true
	end
	for _,tname in pairs(cdata["isonames"] or {}) do
		if sqry==p.strip(tname) then
			return true
		end
	end
	for _,tname in pairs(cdata["altnames"] or {}) do
		if sqry==p.strip(tname) then
			return true
		end
	end
	return false
end

------------I S O N A M E------------										-- Find the ISO name of a country/region

local function isoname
	if data[code]["isonames"] then
		local name = data[code]["isodisplaynames"] and data[code]["isodisplaynames"][lang]
			 or data[code]["isonames"][lang]
			 or data[code]["isodisplaynames"] and data[code]["isodisplaynames"][data[code]["defaultlang"] or data["defaultlang"]]
			 or data[code]["isonames"][data[code]["defaultlang"] or data["defaultlang"]]
			 or data[code]["isodisplaynames"] and data[code]["isodisplaynames"]["en"]
			 or data[code]["isonames"]["en"]
		if name then return name end
		for _,iname in pairs(data[code]["isonames"]) do return iname end
		return data[code]["isodisplayname"] or data[code]["isoname"]
	else
		return data[code]["isodisplayname"] or data[code]["isoname"]
	end
end

------------S T R I P------------											-- Removes junk from the input

function p.strip(text)
	local accents = {["À"]="A",["Á"]="A",["Â"]="A",["Ã"]="A",					-- accent list
		["Ä"]="A",["Å"]="A",["Ç"]="C",["È"]="E",["É"]="E",
		["Ê"]="E",["Ë"]="E",["Ì"]="I",["Í"]="I",["Î"]="I",
		["Ï"]="I",["Ñ"]="N",["Ò"]="O",["Ó"]="O",["Ô"]="O",
		["Õ"]="O",["Ö"]="O",["Ø"]="O",["Ù"]="U",["Ú"]="U",
		["Û"]="U",["Ü"]="U",["Ý"]="Y"
	local remove = {"NATION OF","COUNTRY OF","TERRITORY OF",					-- text to be removed list
		"FLAG OF","FLAG","KINGDOM OF","STATE OF"," STATE ",
		"PROVINCE OF","PROVINCE","TERRITORY"
	local patterns = {[".+:"]="",["|.+"]="",["%(.-%)"]="",						-- patterns to follow (order may matter)
		["%..*"]="",["^THE "]="",["%_"]=" ",["%-"]=" ",
		["%d%d?%d?PX"]="",
	
	text = mw.ustring.upper(text)												-- Case insensitivity
	text = mw.ustring.gsub								-- Deaccent
	
	for pattern,value in pairs(patterns) do										-- Follow patterns
	text = mw.ustring.gsub 
	end
	
	for _,words in pairs(remove) do												-- Remove unneeded words
	text = mw.ustring.gsub 
	end
	
	text = mw.ustring.gsub										-- Remove non alpha-numeric
	
	return text
	
end

------------P . C A L L S T R I P -----------								-- Calls P.strip but using Module:Arguments

function p.callstrip(frame)
	
	local args = getArgs(frame)
	
	return p.strip(args[1]) or ""

end

------------P . L U A C O D E-----------									-- Makes the ISO code of a country

function p.luacode(args)

	if string.find then
		args[1], args[2] = string.match(args[1] or '',"^([^%-]*)%-(.*)$")
	end
	
	if args[1] then args[1] = p.strip(args[1]) end
	if args[2] then args[2] = p.strip(args[2]) end
	
	if args["codetype"]=="3" then
		args["codetype"]="alpha3"
	end
	
	local eot = args.error or ""
	
	local catnocountry = (args.nocat and args.nocat == 'true') and '' 
		or ''
	local catnosubdivision = (args.nocat and args.nocat == 'true') and '' 
		or ''
	
	if not args[1] then
		if mw.title.getCurrentTitle.namespace ~= 0 then catnocountry = '' end
		return catnocountry, ''
	end
	
	if not args[2] then															--3166-1 code
		for alpha2,cdata in pairs(data) do
			if findname then
				if args["codetype"]=="numeric" or args["codetype"]=="alpha3" then
					return cdata[args["codetype"]]
				else
					return alpha2
				end
			end
		end
		if mw.title.getCurrentTitle.namespace ~= 0 then catnocountry = '' end
		return catnocountry
	else																		--3166-2 code
		for alpha2,cdata in pairs(data) do 
			if findname then
				if mw.ustring.match then							-- For England, Wales etc.
					alpha2 = "GB"
				end
				local sdata = mw.loadData("Module:ISO 3166/data/"..alpha2)
				local empty = true
				for scode,scdata in pairs(sdata) do
					if type(scdata)=="table" then
						empty = false
						if findname then
							return alpha2.."-"..scode
						end
					end
				end
				if mw.title.getCurrentTitle.namespace ~= 0 then catnosubdivision = '' end
				return catnosubdivision
			end
		end
		if mw.title.getCurrentTitle.namespace ~= 0 then catnocountry = '' end
		return catnocountry
	end

end

------------P . C O D E-----------											-- Calls P.Luacode but using Module:Arguments

function p.code(frame)

	return p.luacode(getArgs(frame)) or ""

end

------------P . N U M E R I C-----------									-- Calls P.Luacode but using Module:Arguments and setting it to output a numeric value

function p.numeric(frame)

	local args = getArgs(frame)
	
	args["codetype"]="numeric"
	
	return p.luacode(args) or ""
	
end
	
------------P . L U A N A M E-----------									-- Makes the ISO/common name of a country
	
function p.luaname(args)

	local code1 = p.luacode(args)
	local code2 = ''
	
	if string.find then
		code1, code2 = string.match(code1,"^([^%-]*)%-(.*)$")
	end
	
	if string.find then
		if code2=="" then														--3166-1 alpha-2 code
			if data[code1] then
				return (args.isoname or args.lang) and isoname
					or (data[code1]["displayname"] or data[code1]["name"])
			else
				return ''
			end
		else																	--3166-2 code
			local sdata
			if data[code1] then
				sdata = mw.loadData("Module:ISO 3166/data/"..code1)
			else
				return ''
			end
			if sdata[code2] then
				return (args.isoname or args.lang) and isoname
					or (sdata[code2]["displayname"] or sdata[code2]["name"])
			else
				return ''
			end
		end
	end
	
end

------------P . N A M E-----------											-- Calls P.Luaname but using Module:Arguments

function p.name(frame)

	return p.luaname(getArgs(frame)) or ""

end
	
------------P . G E O C O O R D I N S E R T-----------						-- Wrapper for Module:Coordinates.coordinsert
function p.geocoordinsert(frame)
	-- 
	-- |country=..|subdivision1=...|subdivision2=...
	-- |type=...|scale=...|dim=...|source=...|globe=...
	-- }}
	local args = frame.args
	local subdivisionqueried = false
	local catnocountry = (args.nocat and args.nocat == 'true') and '' 
		or ''
	local catnosubdivision = (args.nocat and args.nocat == 'true') and ''
		or '' or ''
	local tracking = ''
	local targs = {}
	targs[1] = args[1] or ''
	for i, v in pairs(args) do
		if i == 'country' and not mw.ustring.find then
			local country = v
			local k, region = 1, ''
			-- look for a valid subdivision
			while region == '' and k < 3 do
				local subdivision = args['subdivision' .. k] or ''
				if subdivision ~= '' then
					region = p.luacode
					subdivisionqueried = true
				end
				k = k + 1
			end
			-- subdivision lookup failed or never attempted, try country only
			if region == '' then
				region = p.luacode
				if mw.title.getCurrentTitle.namespace ~= 0 then catnocountry, catnosubdivision = '', '' end
				if region == '' then
					tracking = tracking .. catnocountry
				elseif subdivisionqueried == true then
					tracking = tracking .. catnosubdivision
				end
			end
			-- something worked, add it to the targs
			if region ~= '' then
				targs[#targs + 1] = 'region:' .. region
			end
		elseif i == 'type' or i == 'scale' or i == 'dim' 
				or i == 'source' or i == 'globe' then
			targs[#targs + 1] = i .. ':' .. v
		end
	end
	-- call Module:Coordinates.coordinsert if there is something to insert
	if #targs > 1 then
		local coordinsert = require('Module:Coordinates').coordinsert
		return coordinsert({args = targs}) .. tracking
	end
	-- otherwise, just return the coordinates
	return targs[1] .. tracking
end