local p = {}

-- Loading the pictures module --
local absolutetime = os.time() -- now
local current_year = os.date("%Y", absolutetime)
local pictures_curr = mw.loadData("Module:POTD/" .. current_year)
local pictures_prev = mw.loadData("Module:POTD/" .. current_year - 1)

-- Building a gallery for requested number of pictures --
local function gallerator(scope)
	local buildgallery = ""
	local range = 0
	local iter = 1
	if scope == "all" then
		range = 365
	elseif tonumber(scope) then
		range = tonumber(scope)
		iter = range < 0 and -1 or 1
	end
	
	absolutetime = os.time() -- now
	local daymonth = ""
	
	for days = iter, range, iter do
		absolutetime = absolutetime + (86400 * iter) -- one day
		daymonth = os.date( "%d/%m" , absolutetime ) 
		if pictures_curr.file[daymonth] == nil then
			if pictures_prev.file[daymonth] == nil then
			    buildgallery = buildgallery .. '\n' .. 'File:Empty set.svg' .. '|' .. daymonth
			else
			    buildgallery = buildgallery .. '\n' .. 'File:' .. pictures_prev.file[daymonth] .. '|' .. daymonth .. ': ' .. (pictures_prev.footer[daymonth] or '')
			end
		else 
			buildgallery = buildgallery .. '\n' .. 'File:' .. pictures_curr.file[daymonth] .. '|' .. daymonth .. ': ' .. (pictures_curr.footer[daymonth] or '')
		end
	end
	return buildgallery
end

-- Matching pictures with day of year --
function p.todayspics(frame)
	local args = frame:getParent().args
	local option = args[1] or ""
	
	-- Which date is today? --
	local daymonth = os.date("%d/%m", absolutetime)
	
	-- Which picture and description should we use today? --
	if option:find("%d%d/%d%d") then
		daymonth = option
	end
	while not pictures_curr.file[daymonth] and not pictures_prev.file[daymonth] do
		absolutetime = absolutetime - 86400 -- previous day
		daymonth = os.date("%d/%m", absolutetime)
	end
	local filename = pictures_curr.file[daymonth]
	local description = pictures_curr.footer[daymonth] or ''
    --- Take file from previous year if needed
	if filename == nil then
	    filename = pictures_prev.file[daymonth]
	    description = pictures_prev.footer[daymonth]
	end
	
	-- Any template in footer? Expand it
	if string.match(description, '%b{}') then
		description = frame:preprocess(description)
	end
	
	-- Which output is desired, text or description? --
	local show = ""
	if option == "filename" then
		show = filename
	elseif option == "description" then
		show = description
	else
		show = '[[File:' .. filename .. '|350x350px]]<br />' .. description
	end
	
	-- Which pictures should we display in addition of today's? --
	local gallery = ""
	if option == "all" or tonumber(option) then
		gallery = frame:extensionTag{name = 'gallery', content = gallerator(option), args = {mode='packed-hover', heights='150px'}}
	end
	
	return show .. gallery
end

-- Generates an archive of a year. Use it {{subst:#invoke:POTD|archive|<YYYY>}}
function p.archive(frame)
	local year = frame.args[1]
	if mw.title.new("Module:POTD/" .. year).exists then
		local pictures = mw.loadData("Module:POTD/" .. year)
		local buildgallery = {}
		local absolutetime = os.time({year=year, month=1, day=1})
		for iter = 0, 365 do
			day = absolutetime + (86400 * iter)
			daymonth = os.date("%d/%m", day)
			if pictures.file[daymonth] ~= nil then
				table.insert(buildgallery, 'File:' .. pictures.file[daymonth] .. '|' .. daymonth .. ': ' .. (pictures.footer[daymonth] or ''))
			end
		end
		return frame:extensionTag{name = 'gallery', content = table.concat(buildgallery, '\n'), args = {mode='packed', heights='150px'}}
	end
	return
end
	
return p