require('strict')

local function getCatForId( id )
    local title = mw.title.getCurrentTitle()
    local namespace = title.namespace
    if namespace == 0 then
        return '[[Categoria:!Artigos enciclopédicos com identificadores  ' .. id .. ']]'
    --elseif namespace == 2 and not title.isSubpage then
    --    return '[[Categoría:Wikipedia:Páxinas de usuario con identificadores ' .. id .. ']]'
    --else
    --    return '[[Categoría:Wikipedia:Páxinas diversas con identificadores ' .. id .. ']]'
    end
end

local function append(str, c, length)
    while str:len() < length do
        str = c .. str
    end
    return str
end

--Links

local function arlhsLink( id )
    return '[http://wlol.arlhs.com/lighthouse/' .. id .. '.html ' .. id .. ']' .. getCatForId( 'ARLHS' )
end

local function asnLink ( id )
    return '[http://aviation-safety.net/database/record.php?id=' .. id .. ' ' .. id .. ']' .. getCatForId( 'ASN' )
end

local function bavarikonLink ( id )
    return '[http://www.bavarikon.de/object/odb:BSB-' .. id .. ' ' .. id .. ']' .. getCatForId( 'Bavarikon' )
end

local function dareLink( id )
    return '[http://dare.ht.lu.se/places/' .. id .. ' ' .. id .. ']' .. getCatForId( 'DARE' )
end

local function docomomoLink ( id )
    return '[http://www.docomomoiberico.com/index.php?option=com_k2&view=item&id=' .. id .. ' ' .. id .. ']' .. getCatForId( 'DOCOMOMO' )
end

local function ctbuhLink ( id )
    return '[http://www.skyscrapercenter.com/building.php?building_id=' .. id .. ' ' .. id .. ']' .. getCatForId( 'CTBUH' )
end

local function egliseinfoLink( id )
    return '[http://egliseinfo.catholique.fr/lieu/' .. id .. ' ' .. id .. ']' .. getCatForId( 'EgliseInfo' )
end

local function emporisLink ( id )
    return '[https://www.emporis.com/en/wm/bu/?id=' .. id .. ' ' .. id .. ']' .. getCatForId( 'Emporis' )
end

local function geonamesLink ( id )
    return '[http://sws.geonames.org/' .. id .. ' ' .. id .. ']' .. getCatForId( 'GeoNames' )
end

local function gnisLink ( id )
    return '[https://geonames.usgs.gov/pls/gnispublic/f?p=gnispq:3:::NO::P3_FID:' .. id .. ' ' .. id .. ']' .. getCatForId( 'GNIS' )
end

local function googlemapsLink ( id )
        return '[https://maps.google.com/?cid=' .. id .. ' ' .. id .. ']' .. getCatForId( 'GoogleMaps' )
end

local function govLink ( id )
    return '[http://gov.genealogy.net/item/show/' .. id .. ' ' .. id .. ']' .. getCatForId( 'GOV' )
end

local function gridLink ( id )
    return '[https://www.grid.ac/institutes/' .. id .. ' ' .. id .. ']' .. getCatForId( 'GRID' )
end

local function iataLink ( id )
    return '[https://www.flightradar24.com/data/airports/' .. id .. ' ' .. id .. ']' .. getCatForId( 'IATA' )
end

local function icaoLink ( id )
    return '[https://www.flightradar24.com/data/airports/' .. id .. 'per ' .. id .. ']' .. getCatForId( 'ICAO' )
end

local function imoLink( id )
    return '[https://www.marinetraffic.com/ais/details/ships/' .. id .. ' ' .. id .. ']' ..getCatForId( 'IMO' )
end

local function jcylLink( id )
    return '[http://servicios.jcyl.es/pweb/datos.do?numero=' .. id .. '&tipo=inmueble&ruta= ' .. id .. ']' ..getCatForId( 'JCyL' )
end

local function mbareaLink ( id )
    return '[https://musicbrainz.org/area/' .. id .. ' ID]' .. getCatForId( 'MusicBrainz' )
end

local function mbplaceLink ( id )
    return '[https://musicbrainz.org/place/' .. id .. ' ID]' .. getCatForId( 'MusicBrainz' )
end

local function mmsiLink ( id )
    return '[https://www.marinetraffic.com/ais/details/ships/' .. id .. ' ' .. id .. ']' .. getCatForId( 'MMSI' )
end

local function mtlighthouseLink ( id )
    return '[http://www.marinetraffic.com/en/ais/details/lights/' .. id .. '/ ' .. id .. ']' .. getCatForId( 'MarineTraffic' )
end

local function mtportLink ( id )
    return '[http://www.marinetraffic.com/en/ais/details/ports' .. id .. ' ' .. id .. ']' .. getCatForId( 'MarineTraffic' )
end

local function naturaLink ( id )
    return '[http://natura2000.eea.europa.eu/Natura2000/SDF.aspx?site=' .. id .. ' ' .. id .. ']' .. getCatForId( 'Natura2000' )
end

local function npcaLink ( id )
    return '[https://www.npca.org/parks/' .. id .. ' ' .. id .. ']' .. getCatForId( 'NPCA' )
end

local function npfLink ( id )
    return '[https://www.nationalparks.org/explore-parks/' .. id .. ' ' .. id .. ']' .. getCatForId( 'NPF' )
end

local function nrhpLink ( id )
    return '[http://focus.nps.gov/AssetDetail/NRIS/' .. id .. ' ' .. id .. ']' .. getCatForId( 'NRHP' )
end

local function openAIPLink ( id )
    return '[https://www.openaip.net/node/'.. id .. ' ' .. id .. ']' .. getCatForId( 'openAIP' )
end

local function oprLink ( id )
    return '[http://www.patrimoine-religieux.fr/annuaire_opr/com.bsw.directory.View/View.html?local=fr&idEdifice=' .. id .. ' ' .. id .. ']' .. getCatForId( 'OPR' )
end

local function osmLink ( id )
    return '[https://www.openstreetmap.org/relation/' .. id .. ' ' .. id .. ']' .. getCatForId( 'OSM' )
end

local function peakbaggerLink ( id )
    return '[http://www.peakbagger.com/peak.aspx?pid=' .. id .. ' ' .. id .. ']' .. getCatForId( 'Peakbagger' )
end

local function peakfinderLink ( id )
    return '[http://www.peakfinder.com/showpeakbyid.asp?MtnId=' .. id .. ' ' .. id .. ']' .. getCatForId( 'Peakfinder' )
end

local function pleiadesLink ( id )
    return '[http://pleiades.stoa.org/places/' .. id  .. ' ' .. id .. ']' .. getCatForId( 'Pleiades' )
end

local function rijksmonumentLink ( id )
    return '[https://monumentenregister.cultureelerfgoed.nl/monuments?MonumentId=' .. id  .. ' ' .. id .. ']' .. getCatForId( 'Rijksmonument' )
end

local function sipcaLink ( id )
    return '[http://www.sipca.es/censo/' .. id .. '/.html ' .. id .. ']' .. getCatForId('SIPCA')
end

local function sncziELink (id)
    return '[http://sig.mapama.es/93/ClienteWS/snczi/default.aspx?nombre=EMBALSE&claves=REF_CEH&valores=' .. id  .. ' ' .. id .. ']' .. getCatForId( 'SNCZI-IPE' )
end

local function sncziPLink (id)
    return '[http://sig.mapama.es/93/ClienteWS/snczi/default.aspx?nombre=PRESA&claves=DGAGUA.PRESAS.CODPRESA&valores=' .. id  .. ' ' .. id .. ']' .. getCatForId( 'SNCZI-IPE' )
end

local function structuraeLink ( id )
    return '[http://en.structurae.de/structures/data/index.cfm?ID=' .. id .. ' ' .. id .. ']' .. getCatForId( 'Structurae' )
end

local function summitpostLink ( id )
    return '[http://www.summitpost.org/page/' .. id .. ' ' .. id .. ']' .. getCatForId( 'SummitPost' )
end

local function tgnLink ( id )
    return '[http://vocab.getty.edu/page/tgn/' .. id .. ' ' .. id .. ']' .. getCatForId( 'TGN' )
end

local function theatricaliatheatreLink ( id )
    return '[http://theatricalia.com/place/' .. id .. ' ' .. id .. ']' .. getCatForId( 'Theatricalia' )
end

local function trismegistosLink ( id )
    return '[http://www.trismegistos.org/place/' .. id .. ' ' .. id .. ']' .. getCatForId( 'Trismegistos' )
end

local function whcLink ( id )
    return '[http://whc.unesco.org/en/list/' .. id .. ' ' .. id .. ']' .. getCatForId( 'WHC' )
end

local function woeidLink ( id )
    return '[https://www.flickr.com/places/info/' .. id  .. ' ' .. id .. ']' .. getCatForId( 'WOEID' )
end

local function wwdbLink ( id )
    return '[http://www.worldwaterfalldatabase.com/waterfall/' .. id .. ' ' .. id .. ']' .. getCatForId( 'WWDB' )
end

local function wwfecoLink ( id )
    return '[http://www.worldwildlife.org/ecoregions/' .. id .. ' ' .. id .. ']' .. getCatForId( 'WWFeco' )
end

-------
local function getIdsFromWikidata( item, property )
    local ids = {}
    if not item.claims[property] then
        return ids
    end
    for _, statement in pairs( item.claims[property] ) do
        if statement.mainsnak.datavalue then
            table.insert( ids, statement.mainsnak.datavalue.value )
        end
    end
    return ids
end

local function matchesWikidataRequirements( item, reqs )
    for _, group in pairs( reqs ) do
        local property = 'p' .. group[1]
        local qid = group[2]
        if item.claims[property] ~= nil then
            for _, statement in pairs ( item.claims[property] ) do
                if statement.mainsnak.datavalue ~= nil then
                    if statement.mainsnak.datavalue.value['numeric-id'] == qid then
                        return true
                    end
                end
            end
        end
    end
    return false
end

local function createRow( id, label, rawValue, link, withUid )
    if link then
        if withUid then
            return '*<span style="white-space:nowrap;">' .. label .. ' <span class="uid">' .. link .. '</span></span>\n'
        else
            return '*<span style="white-space:nowrap;">' .. label .. ' ' .. link .. '</span>\n'
        end
    else
        return '* <span class="error">O identificador ' .. id .. ' ' .. rawValue .. ' não é válido.</span>[[Categoria:!Artigos enciclopédicos com erros nos identificadores]]\n'
    end
end

--Orde: nome do parámetro, etiqueta, ID da propiedade en Wikidata e nome da función de formato
local conf = {
    --Lugares/Estruturas
    { 'ARLHS', '[[Sociedade de Radioamadores de Faróis|ARLHS]]', 2980, arlhsLink},
    { 'ASN', '[[Aviation Safety Network|ASN]]', 1755, asnLink},
    { 'Bavarikon', '[[:de:Bavarikon|Bavarikon]]', 4005, bavarikonLink},
    { 'DARE', '[[:d:Q24575107|DARE]]', 1936, dareLink},
    { 'DOCOMOMO', '[[Fundação DOCOMOMO Ibérico|DOCOMOMO]]', 3758, docomomoLink},
    { 'CTBUH', '[[:d:Q45027467|CTBUH]]', 1305, ctbuhLink},
    { 'EgliseInfo', 'EgliseInfo', 1644, egliseinfoLink},
    { 'Emporis', '[[Emporis]]', 455, emporisLink},
    { 'GeoNames', '[[GeoNames]]', 1566, geonamesLink},
    { 'GNIS', '[[Geographic Names Information System|GNIS]]', 590, gnisLink},
    { 'GoogleMaps', '[[Google Maps]]', 3749, googlemapsLink},
    { 'GOV', 'GOV', 2503, govLink},
    { 'GRID', '[[Global Research Identifier Database|GRID]]', 2427, gridLink},
    { 'IATA', '[[Associação de Transporte Aéreo Internacional|IATA]]', 238, iataLink},
    { 'ICAO', '[[Organização de Aviação Civil Internacional|ICAO]]', 239, icaoLink},
    { 'IMO', '[[Organização Marítima Internacional|IMO]]', 458, imoLink},
    { 'JCyL', '[[Junta de Castela e Leão|JCyL]]', 3177, jcylLink},
    { 'MMSI', 'MMSI', 587, mmsiLink},
    { 'MTLighthouse', '[[:en:MarineTraffic|MarineTraffic]]', 3601, mtlighthouseLink},
    { 'MTPort', '[[:en:MarineTraffic|MarineTraffic]]', 1624, mtportLink},
    { 'MusicBrainzArea', '[[MusicBrainz]]', 982, mbareaLink },
    { 'MusicBrainzPlace', '[[MusicBrainz]]', 1004, mbplaceLink },
    { 'Natura2000', '[[Rede Natura 2000|Natura 2000]]', 3425, naturaLink},
    { 'NPCA', '[[Associação de Conservação de Parques Nacionais|NPCA]]', 3515, npcaLink},
    { 'NPF', '[[National Park Foundation|NPF]]', 3516, npfLink},
    { 'NRHP', '[[:en:National Park Foundation|NRHP]]', 649, nrhpLink},
    { 'openAIP', 'openAIP', 4121, openAIPLink},
    { 'OPR', '[[Observatório do Patrimônio Religioso|OPR]]', 3371, oprLink},
    { 'OSM', '[[OpenStreetMap|OSM]]', 402, osmLink },
    { 'Peakbagger', '[[:d:Q28736250|Peakbagger]]', 3109, peakbaggerLink},
    { 'Peakfinder', '[[:d:Q28925511|Peakfinder]]', 3770, peakfinderLink},
    { 'Pleiades', '[[:d:Q24423804|Pleiades]]', 1584, pleiadesLink},
    { 'Rijksmonument', '[[Rijksmonument]]', 359, rijksmonumentLink},
    { 'SIPCA', '[[:es:Sistema de Información de Patrimonio Cultural Aragonés|SIPCA]]', 3580, sipcaLink},
    { 'SNCZI-E', '[[:es:Sistema Nacional de Cartografía de Zonas Inundables|SNCZI]]-[[:es:Inventario de Presas y Embalses de España|IPE]]', 4568, sncziELink},
    { 'SNCZI-P', '[[:es:Sistema Nacional de Cartografía de Zonas Inundables|SNCZI]]-[[:es:Inventario de Presas y Embalses de España|IPE]]', 4558, sncziPLink},
    { 'Structurae', '[[Structurae]]', 454, structuraeLink },
    { 'SummitPost', '[[:d:Q27313283|SummitPost]]', 3309, summitpostLink},
    { 'TGN', '[[Getty Thesaurus of Geographic Names|TGN]]', 1667, tgnLink},
    { 'TheatricaliaPlace', '[[:d:Q24056151|Theatricalia]]', 2468, theatricaliatheatreLink},
    { 'Trismegistos', '[[:d:Q22094624|Trismegistos]]', 1958, trismegistosLink},
    { 'WHC', '[[Património da Humanidade|WHC]]', 757, whcLink},
    { 'WOEID', '[[WOEID]]', 1281, woeidLink},
    { 'WWDB', '[[World Waterfall Database|WWDB]]', 3326, wwdbLink},
    { 'WWFeco', '[[Fundo Mundial para a Natureza|WWFeco]]', 1294, wwfecoLink}
}

-- Check that the Wikidata item has this property-->value before adding it
local reqs = {}

local p = {}

function p.authorityControlGeo( frame )
    local parentArgs = frame:getParent().args
    --Create rows
    local elements = {}
    local title = mw.title.getCurrentTitle()
    local namespace = title.namespace
    if namespace == 0 then --Só no espazo de nomes principal
        --redirect PND to GND
        if (parentArgs.GND == nil or parentArgs.GND == '') and parentArgs.PND ~= nil and parentArgs.PND ~= '' then
            parentArgs.GND = parentArgs.PND
        end

        --Wikidata fallback if requested
        local item = mw.wikibase.getEntityObject()
        if item ~= nil and item.claims ~= nil then
            for _, params in pairs( conf ) do
                if params[3] ~= 0 then
                    local val = parentArgs[params[1]]
                    if not val or val == '' then
                        local canUseWikidata = nil
                        if reqs[params[1]] ~= nil then
                            canUseWikidata = matchesWikidataRequirements( item, reqs[params[1]] )
                        else
                            canUseWikidata = true
                        end
                        if canUseWikidata then
                            local wikidataIds = getIdsFromWikidata( item, 'P' .. params[3] )
                            if wikidataIds[1] then
                                parentArgs[params[1]] = wikidataIds[1]
                            end
                        end
                    end
                end
            end
        end

        --Configured rows
        local rct = 0
        for k, params in pairs( conf ) do
            local val = parentArgs[params[1]]
            if val and val ~= '' then
                table.insert( elements, createRow( params[1], params[2] .. ':', val, params[4]( val ), true ) )
                rct = rct + 1
            end
        end
        local Navbox = require('Módulo:Navbox')
        --local elementscats = ''
        --if rct > 13 then
        --    elementscats  = '[[Categoria:!CA com  ' .. rct .. ' elementos]]'
        --end
   
        if #elements ~= 0 then
            return Navbox._navbox( {
                name  = 'Taxonbar',
                bodyclass = 'hlist',
                listclass = 'plainlinks',
                groupstyle = 'width: 15%; text-align:center;',
                group1 = 'Identificadores geográficos', --.. elementscats,
                list1 = table.concat( elements )
                } )
        else
            return ""
        end
    end
end

return p