Documentația acestui modul poate fi creată la Modul:Monumente/doc

globalFrame = nil
lmi = {};
local coords = require("Modul:Coordonate")
local getArgs = require('Modul:Arguments').getArgs

function lmi.stripLinks(text)
	local replacedText = mw.ustring.gsub(mw.text.killMarkers(text), "%[%[([^%]|]*)|?([^%]]*)%]%]", "%2")
	return replacedText
end

function lmi.reportErrorLink(code)
	local query = "action=edit&section=new&preloadtitle=" .. mw.uri.encode("Eroare la [[Cod:LMI:" .. code .. "|]]") .. "&preload=P:MI/PRE"
	--local uri = mw.uri.fullUrl("Proiect:Monumente istorice/Erori", query)  -- too slow
	local uri = "//ro.wikipedia.org/w/index.php?title=Proiect:Monumente_istorice/Erori&" .. query 
	return "[" .. tostring(uri) .. " Raportează eroare]"
end

function lmi.isCopyrighted(code, century, copyright)
	if string.find(code, "-I-", 1, true) ~= nil then
		return false
	elseif copyright == "no" or copyright == "nu" then
		return false
	elseif copyright and copyright ~= "" then
		year = tonumber(copyright)
		if year == nil then
			return true
		elseif year + 70 < tonumber(os.date("%Y", os.time())) then
			return false
		else
			return true
		end
	elseif century and string.find(century, "sec", 1, true) ~= nil and string.find(century, "XX", 1, true) == nil then
		return false
	elseif century and string.find(century, "sec", 1, true) == nil and string.match(century, "19%d%d", 1, true) == nil then
		return false
	else
		return true
	end
end

function lmi.uploadImageLink(code, commons, name, 
								lat, lon, 
								isCopyrighted, 
								label)
	local upload = "["
	if isCopyrighted == false then
		upload = upload .. "//commons.wikimedia"
	else
		upload = upload .. "//ro.wikipedia"
	end
	upload = upload .. ".org/w/index.php?title=Special:UploadWizard&campaign=wlm-ro&id=" .. code .. "&description=" .. mw.uri.encode(lmi.stripLinks(name)) .. "&uselang=ro"
	if lat ~= '' then
		upload = upload .. "&lat=" .. lat
	end
	if lon ~= '' then
		upload = upload .. "&lon=" .. lon
	end
	if commons ~= '' and isCopyrighted == false then
		local t = mw.text.split( commons, ':', true)
		upload = upload .. "&categories=" .. mw.uri.encode(t[#t])
	end
	upload = upload .. " " .. label .. "]"
	return upload
end

function lmi.uploadVideoLink(code, commons, name, lat, lon, isCopyrighted, label)
	if isCopyrighted then
		return lmi.uploadImageLink(code, commons, name, lat, lon, isCopyrighted, label)
	end
	local upload = "[//video2commons.toolforge.org/?filedesc="
	local location = ""
	if lat ~= '' then
		location = "{{Location|" .. lat .. "|" .. lon .. "}}"
	end
	local categories = ""
	if commons ~= '' then
		local t = mw.text.split( commons, ':', true)
		categories = "[[Category:" .. t[#t] .. "]]"
	end
	contents = mw.uri.encode([[=={{int:filedesc}}==
{{Information
|description={{ro|]] .. lmi.stripLinks(name) .. [[}}{{Monument istoric|]] .. code .. [[}}
|date=]] .. os.date("%F %T") .. [[
|source={{own}}
|author=~~~
|permission={{cc-by-sa-4.0}}
}}
]] .. location .. categories)
-- [[
-- {{Wiki Loves Monuments 2019|ro}}
-- ]] .. categories)
	upload = upload .. contents .. " " .. label .. "]"
    return upload
end

function lmi.link2image(code, image, plan, commons, isCopyrighted)
	local text = ''
	local imageLink = ''
	if image ~= '' then
		text = "[[" .. image .. "|150px"
		imageLink = image
	elseif plan ~= '' then
		text = "[[" .. plan .. "|150px"
		imageLink = plan
	else
		return ''
	end
	if commons ~= '' and isCopyrighted == false then
		return text .. "|link=" .. commons .. "|Alte imagini]]"
	else
		return text .. "]]"
	end
end

function lmi.imageCell(frame)
	local args = getArgs(frame)
	local code = args[1] or ''
	local image = args[2] or ''
	local plan = args[3] or ''
	local commons = args[4] or ''
	local name = args[5] or ''
	local lat = args[6] or ''
	local lon = args[7] or ''
	local century = args[8] or ''
	local copyright = args[9] or ''
	return lmi.buildImageCell(code, image, plan, commons, name, lat, lon, century, copyright)
end
	
function lmi.buildImageCell(code, image, plan, commons, name, lat, lon, century, copyright)
	local isCopyrighted = lmi.isCopyrighted(code, century, copyright)
	local imglink = lmi.link2image(code, image, plan, commons, isCopyrighted)
	local upload = "\n<div class=\"noprint plainlinks small\">Încarcă " .. 
					lmi.uploadImageLink(code, commons, name, lat, lon, isCopyrighted, "foto") .. " | " .. 
					lmi.uploadVideoLink(code, commons, name, lat, lon, isCopyrighted, "video") .. "</div>"
	if imglink ~= '' then return imglink else return imglink .. upload end
	--return "***" .. string.match(century, "19%d%d", 1, true) .. "***"
end

function lmi.link2RAN(frame)
	local code = frame.args[1]
	elements = mw.text.split(code, '.', true)
	if (table.maxn(elements) < 2) then
		return code
	end
	ranCode = elements[1] .. "." .. elements[2]
	return "[http://ran.cimec.ro/sel.asp?cod-sit-ran-" .. ranCode .. " " .. code .. "]"
end

local function truncate(nr, decimals)
		local n = tonumber(nr)
		if n ~= nil then 
			return math.floor(n*10^decimals) / (10^decimals) 
		else
			return nr
		end
end

function lmi.coord(frame)
	globalFrame = frame
	args = {}
	for i, v in ipairs(frame.args) do
		local decimals = 5
		args[i] = truncate(v, decimals)
	end
	args["name"] = mw.text.killMarkers(frame.args["name"])
	args["full"] = false
	--mw.logObject(args)
	return coords.coordImpl(args)
	--{{#invoke:Coordonate|simpleCoord|{{{Lat|{{{OsmLat|}}}}}}|N|{{{Lon|{{{OsmLon|}}}}}}|E|name={{{Cod|}}}}}
end


-- This function implements {{ElementLMI}}
function lmi.getElementLine(frame)
	globalFrame = frame
	local args = getArgs(frame)
	local htmlRow = mw.html.create('tr')
		:attr('id', args["Cod"])
	local codPostfix = ""
	--{{#if:{{{NotăCod|}}}{{{RefCod|}}}|{{#tag:ref|{{{NotăCod|}}}|name={{#if:{{{RefCod|}}}|{{{RefCod}}}|{{urlencode:{{{Cod|}}}}}}}}}|}}
	if args['NotăCod'] then
		local ref = frame:callParserFunction{ name = '#tag:ref', args = {
    			args['NotăCod'], 
    			name = args['RefCod'] or args['Cod']
			} }
		codPostfix = codPostfix .. ref
	end
	if args['FostCod'] then
		codPostfix = codPostfix .. "<br />(fost " .. args['FostCod'] .. ")"
	end
	if args['Cod92'] then
		codPostfix = codPostfix .. "<br /><small>(LMI92: " .. args['Cod92'] .. ")</small>"
	end
	if args['Cod55'] then
		codPostfix = codPostfix .. "<br /><small>(LMI55: " .. args['Cod55'] .. ")</small>"
	end
	if args['CodRan'] then
		codPostfix = codPostfix .. "<br /><small>(RAN: " .. args['CodRan'] .. ")</small>"
	end
	htmlRow:tag('td')
			:tag('span'):attr('class', 'nowrap'):wikitext(args['Cod']):done()
			:wikitext(codPostfix)
			:done()

	htmlRow:tag('td'):wikitext(args['Denumire'] ):done()
		:tag('td'):wikitext(args['Localitate']):done()
	
	--coords
	local decimals = 5
	args['Lat'] = truncate(args['Lat'] or args['OsmLat'] or nil, decimals)
	args['Lon'] = truncate(args['Lon'] or args['OsmLon'] or nil, decimals)
	local coordCell = args['Adresă'] or ''
	if args['Lat'] and args['Lon'] then
		cargs = {}
		cargs[1] = args['Lat'] 
		cargs[2] = "N"
		cargs[3] = args['Lon']
		cargs[4] = "E"
		cargs["name"] = mw.text.killMarkers(args['Denumire'])
		cargs["full"] = false
		coordCell = coordCell .. "<br/>" .. coords.coordImpl(cargs)
	end
	htmlRow:tag('td'):wikitext(coordCell):done()
	-- datare, creatori
	local dateCell = args['Datare'] or '' 
	if args['Datare'] and args['Creatori'] then 
		dateCell = dateCell .. "<br/>" 
	end
	dateCell = dateCell .. (args['Creatori'] or '')
	htmlRow:tag('td'):wikitext(dateCell):done()
    --image
	local imageCell = lmi.buildImageCell(args["Cod"] or '', args["Imagine"] or '', 
										 args["Plan"] or '', args["Commons"] or '', 
										 args['Denumire'] or '', 
										 args['Lat'] or '', args['Lon'] or '', 
										 args['Datare'] or '', args['Copyright'] or '')
	htmlRow:tag('td'):attr('align', 'center'):wikitext(imageCell):done()
	--error
	local errlink = lmi.reportErrorLink(args["Cod"] or '')
	htmlRow:tag('td'):attr("class", "noprint nomobile plainlinks small"):wikitext(errlink):done()
	htmlRow:allDone()
	return tostring(htmlRow)
end

-- This function implements {{ÎnceputTabelLMI}}
function lmi.getHeaderLine(frame)
	globalFrame = frame
	local htmlRow = mw.html.create('tr')
		:tag('th'):wikitext('[[Cod LMI]]'):done()
		:tag('th'):wikitext('Denumire'):done()
		:tag('th'):wikitext('Localitate'):done()
		:tag('th'):attr('class', 'unsortable'):wikitext('Localizare'):done()
		:tag('th'):attr('class', 'unsortable'):wikitext('Datare, Creatori'):done()
		:tag('th'):attr('class', 'unsortable'):wikitext('Imagine'):done()

	if frame:getParent().args["erori"] ~= nil and frame:getParent().args["erori"] ~= 1 then
		htmlRow:done()
	else
		htmlRow:tag('th'):attr('class', 'noprint nomobile unsortable'):wikitext('Erori'):done()
	end
	htmlRow:allDone()
	return tostring(htmlRow)
end

return lmi