Documentation for this module may be created at Module:Item/doc

local p = {}

p.cleanFilename = function ( frame )
	if string.lower( string.sub( frame.args.filename, 1, 4 ) ) == 'file' then
		return string.sub( frame.args.filename, 6 )
	end
	return frame.args.filename
end

p.cite = function ( frame )
	if frame.args[1] == nil and frame.args[1] == '' then
		return ''
	end
	local id = frame.args[1]
	local args = { where = '_pageName = "' .. id .. '"', limit = 1 }
	local results = mw.ext.cargo.query( 'items', '_pageName, title, date', args )
	mw.logObject(results)
	if results[ 1 ] then
		local item = results[ 1 ]
		return item._pageName .. ': [[' .. item._pageName .. '|' .. item.title .. ']] (' .. item.date .. ')'
	end
end

p.main = function ( frame )
	-- Title.
	local title = '(Untitled)'
	if frame.args.title ~= nil and frame.args.title ~= '' then
		title = frame.args.title
	end

	-- Parent item.
	local parentItem = ''
	local parentItemLink = ''
	if frame.args.parent_item ~= nil and frame.args.parent_item ~= '' then
		parentItem = frame.args.parent_item
		parentItemLink = '[[' .. parentItem .. ']]'
		local args = { where = '_pageName = "' .. parentItem .. '"', limit = 1 }
		local results = mw.ext.cargo.query( 'items', 'title', args )
		if results[ 1 ] then
			parentItemLink = '[[' .. parentItem .. '|' .. parentItem .. ': ' .. results[ 1 ].title .. ']]'
		end
	end

	-- Description.
	local description = ''
	if frame.args.description ~= nil and frame.args.description ~= '' then
		description = frame.args.description
	end

	-- Image.
	local image = ''
	if frame.args.image ~= nil and frame.args.image ~= '' then
		image = p.cleanFilename( { args = { filename = frame.args.image } } )
	end

	-- Date.
	local date = ''
	if frame.args.date ~= nil and frame.args.date ~= '' then
		date = frame.args.date;
	end

	-- Tags.
	local tags = ''
	if frame.args.tags ~= nil and frame.args.tags ~= '' then
		tags = '[[' .. table.concat( mw.text.split( frame.args.tags, ';' ), ']]; [[' ) .. ']]';
	end

	-- Other files.
	local files = ''
	if frame.args.files ~= nil and frame.args.files ~= '' then
		local filesDiv = mw.html.create( 'div' )
		filesDiv:attr( 'class', 'mdl-item-files' )
		filesDiv:wikitext( frame.args.files )
		files = tostring( filesDiv )
	end

	-- Store Cargo data.
	if mw.title.getCurrentTitle():inNamespace( 0 ) then
		mw.getCurrentFrame():callParserFunction( '#cargo_store', {
			'_table=items',
			title       = title,
			parent_item = parentItem,
			description = description,
			image = image,
			date = date
		} )
	end

	local out = ''
	if image ~= '' then
		out = out .. '[[File:' .. image .. '|300px|right]]'
	end
	local url = mw.title.getCurrentTitle():canonicalUrl()
	local qrcode = mw.getCurrentFrame():callParserFunction( '#qrlite', { url, format = 'png', size = '4' } )
	out = out .. '<div class="mdl-item-qrcode">' .. qrcode .. '</div>\nItem details:\n\n'
		.. '* <strong>Title:</strong> ' .. title .. '\n'
		.. '* <strong>Date:</strong> ' .. date .. '\n'
		.. '* <strong>Tags:</strong> ' .. tags .. '\n'
		.. '* <strong>Description:</strong> ' .. description .. '\n'
		.. '* <strong>Parent item:</strong> ' .. parentItemLink .. '\n'

	if files ~= '' then
		out = out .. '== Attached files ==\n' .. files .. '\n'
	end

	-- Child items (the current_title arg is for testing puposes).
	local currentTitle = nil
	if frame.args.current_title ~= nil and frame.args.current_title ~= '' then
		currentTitle = frame.args.current_title
	elseif mw.title.getCurrentTitle() then
		currentTitle = mw.title.getCurrentTitle().text
	end
	if currentTitle ~= nil then
		local args = {
			where = 'parent_item = "' .. currentTitle .. '"',
			orderBy = 'date ASC'
		}
		local childItemsResults = mw.ext.cargo.query( 'items', '_pageName, title, description, date', args )
		if #childItemsResults > 0 then
	        out = out .. '== Items ==\nThe following items are part of this one.\n{|'
		end
		for r = 1, #childItemsResults do
	        local childItem = childItemsResults[r]
	        out = out .. '|-\n| ' .. ( childItem.date or '<em>Undated</em>' ) .. ' || [[' .. childItem._pageName .. ']] || ' .. childItem.title .. '\n'
		end
    	if #childItemsResults > 0 then
    		out = out .. '|}'
		end
	end

	local styles = mw.getCurrentFrame():extensionTag( 'templatestyles', nil, { src = 'Module:Item/styles.css' } )
	return styles .. out
end

-- =p.main({args={parent_item='BUFF1'}})
return p