-- Modulo per gestire le liste di monumenti di Wiki Loves Monuments

require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local mWikidata = require('Modulo:Wikidata')

local p = {}

-- Associa il nome della regione (come usato nelle sottopagine di
-- [[Progetto:Wiki Loves Monuments 2019/Monumenti]] con il nome (inglese)
-- usato nelle categorie su Commons e la presenza o meno di un concorso regionale
local regioni = {
    ['Abruzzo'] = {'Abruzzo', false},
    ['Basilicata'] = {'Basilicata', true},
    ['Calabria'] = {'Calabria', false},
    ['Campania'] = {'Campania', false},
    ['Emilia-Romagna'] = {'Emilia-Romagna', false},
    ['Friuli-Venezia Giulia'] = {'Friuli-Venezia Giulia', false},
    ['Lazio'] = {'Lazio', false},
    ['Liguria'] = {'Liguria', true},
    ['Lombardia'] = {'Lombardy', false},
    ['Marche'] = {'Marche', false},
    ['Molise'] = {'Molise', false},
    ['Piemonte'] = {'Piedmont', false},
    ['Puglia'] = {'Apulia', false},
    ['Sardegna'] = {'Sardinia', false},
    ['Sicilia'] = {'Sicily', false},
    ['Toscana'] = {'Tuscany', true},
    ['Trentino-Alto Adige'] = {'Trentino-South Tyrol', false},
    ['Umbria'] = {'Umbria', true},
    ['Valle d\'Aosta'] = {'Aosta Valley', false},
    ['Veneto'] = {'Veneto', true},
}

local function titleparts(inputstr)
    local t = {}
    for str in mw.ustring.gmatch(inputstr, "([^/]+)") do
        table.insert(t, str)
    end
    return t
end

-- se il monumento ha una categoria associata, restituisce quella.
-- Altrimenti, quella del comune.
local function main_category(qid)
	local category = mWikidata._getProperty({'P373', from = qid, snaktype = 'value'})
    if category then
		return category
	else
	    return mWikidata._getProperty({'P131', from = qid, showprop = 'P373', snaktype = 'value'})
	end
end

function p.categories(frame)
	local args = getArgs(frame)
	local qid = args[1]

	local categories = {}
	local lake_como = false

    -- categoria principale
	table.insert(categories, main_category(qid))

    -- alberi monumentali
	if mWikidata._instanceOf({'Q811534', from = qid}) then
		table.insert(categories, 'Images from Wiki Loves Monuments 2019 in Italy - veteran trees')
	end

    -- Wiki Loves Lake Como
    local provincia = tp[4]
    if provincia == 'Provincia di Como' or provincia == 'Provincia di Lecco' then
        table.insert(categories, 'Images from Wiki Loves Monuments 2019 in Italy - Lake Como')
        lake_como = true
    end

	-- foto per regione
    local tp = titleparts(mw.title.getCurrentTitle().fullText)
    local regione = tp[3]
    if regioni[regione] then
        table.insert(categories, 'Images from Wiki Loves Monuments 2019 in Italy - ' .. regioni[regione][1])
        if not regioni[regione][2] and not lake_como then
            table.insert(categories, 'Images from Wiki Loves Monuments 2019 in Italy - without regional award')
        end
    else
        table.insert(categories, 'Images from Wiki Loves Monuments 2019 in Italy - unknown region')
    end

	return table.concat(categories, '|')
end

return p