--[[
rawimage
 
Main entry point for Lua function to replace {{raw image}}
 
Usage:
    To invoke the module directly, use
        {{ #Invoke:RawImage | rawimage | pagename }}
    But generally it is preferable to go through the template:
        {{ raw image | pagename }}
     
    'pagename' should be the name of a page in Page: namespace, with or without the "Page:" in front.
    
    
NOTE FOR EDITORS: rawimage displays images centred in the user's standard thumb size.
It is not recommended to offer further display options, as this would disincentivise
the replacement of raw images.
]]

local getArgs = require('Module:Arguments').getArgs
local error = require('Module:Error').error
local p = {}

function p.rawimage(frame)
	-- Template frame will never contain args, so skip it for performance
	local args = getArgs(frame, {
		wrappers = 'Template:raw image'
	})

	-- Bail out if no page was specified
	if args[1] == nil then
		return error({message = 'Invoke with {{raw image|' .. mw.title.getCurrentTitle().text .. '}}'})
	end		

	-- Check if a (valid) specific page has been given.
	local page = mw.title.new(args[1], 'Page')
	if page == nil then
        return error({message = 'Invalid page name ' .. args[1] .. 'specified. Invoke with {{raw image|' .. mw.title.getCurrentTitle().text .. '}}'})
	end

	-- Add link if an IA identifier was provided
	local iacat = ''
	local iaworklink = ''
	local iaziplink = ''
	local iatxt = ''
	if args.ia ~= nil then
		iacat = '[[Category:' .. 'Raw images with Internet Archive link' .. ']]'
		iaworklink = 'https://archive.org/details/' .. args.ia
		iatxt = 'The original scan of this work is available at the '
			.. '[' .. iaworklink .. ' Internet Archive]'

		if args.iazip ~= nil then
			iaziplink = 'https://archive.org/download/' .. args.ia .. '/' .. args.iazip .. '_jp2.zip/'
		else
			iaziplink = 'https://archive.org/download/' .. args.ia .. '/' .. args.ia .. '_jp2.zip/'
		end

		iatxt = iatxt .. '([' .. iaziplink .. ' all files]).'

		if args.iaimg ~= nil then
			local iaimglink = iaziplink .. args.iazip .. '_jp2%2F' .. args.iaimg
			iatxt = iatxt .. ' A [' .. iaimglink .. ' high-resolution scan of this page] is available.'
			iacat = '[[Category:' .. 'Raw images with Internet Archive page link' .. ']]'
		end
	else
		iacat = '[[Category:' .. 'Raw images without Internet Archive link' .. ']]'
	end

	-- Set the category to be used in output depending on namespace
	local category = ''
    if mw.title.getCurrentTitle():inNamespace('Page') then
        category = 'Pages with raw images'
    else
        category = 'Texts with raw images'
    end
	category = '[[Category:' .. category .. ']]'
	category = category .. iacat

    if page.isSubpage then
        -- this page is a subpage, so compose the name of the hi-res file.
        local pagebase = page.baseText
        local pagenum = page.subpageText
        local hiRes = mw.title.new(pagebase .. '-' .. pagenum .. '.png', 'File')

		-- Set defaults for the output
		local image = '[[File:' .. pagebase .. '|page=' .. pagenum .. '|frameless|center|360px]]'
		local text = '(Upload an image to replace this placeholder.)'

        -- Check if the hi-res version exists and use that if available
        if hiRes.fileExists then
        	image = '[[File:' .. hiRes.text .. '|frameless|center|360px]]'
        	text = '([[:File:' .. hiRes.text .. '|Improve this image]])'
        end

		if iatxt ~= '' then
			text = iatxt
		end

		-- Format the output
        local righttxt = frame:expandTemplate{title='right',
        	args={frame:expandTemplate{title='x-smaller block', args={text}}}}
        local outtxt = frame:expandTemplate{title='block center',
        	args={image .. righttxt}}
        return outtxt .. category
    else
        -- Not a subpage, so this page corresponds to a single-page image
        return '[[File:' .. page.text .. '|frameless|center|360px]]' .. category
    end
end

return p