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

local p = {}

function getWorkByPageName( pageName )
	local args = {
		where = '_pageName = "' .. pageName .. '"',
		join = '',
		limit = 1
    }
    local results = mw.ext.cargo.query( 'works', '_pageName,type,title,authors,date,publisher,url,location', args )
    if #results == 0 then
    	return nil
    end
    return results[1]
end

p.cite = function( frame )
	if frame.args[1] == nil or frame.args[1] == '' then
		return ''
	end
	local ident = mw.text.trim( frame.args[1] )
	local work = getWorkByPageName( ident )
    if work == nil then
    	return '[Work not found: "' .. ident .. '"]'
    end
	local citation = work.authors .. ' [' .. work.url .. ' ' .. work.title .. '] ' .. work.date .. ' (' .. work.publisher .. ')'
	if frame.args.id ~= nil and frame.args.id ~= '' then
		citation = citation .. ', ID ' .. frame.args.id
	end
	return citation
end

function getSubpageContent( frame )
	local title = ''
	if frame.args.title then
		title = frame.args.title
	end
	local currentTitleParts = mw.text.split( mw.title.getCurrentTitle().text, '/' )
	local parentPage = currentTitleParts[1]
	local workType = 'written work'
	local args = {}
	local work = getWorkByPageName( parentPage )
	if work ~= nil then
		workType = work.type
	end
	local out = mw.html.create( 'div' )
		:attr( 'class', 'mdl-work-subpage' )
		:wikitext(
			"This page is part of a " .. workType .. " relating to Fremantle that is documented on Freopedia."
			.. " For more information, see [[" .. parentPage .. "]]."
		)
	local styles = mw.getCurrentFrame():extensionTag( 'templatestyles', nil, { src = 'Module:Infobox/styles.css' } )
	return styles .. tostring( out )
end

p.main = function( frame )
	local currentTitle = mw.title.getCurrentTitle()

	if string.match( currentTitle.text, '/' ) then
		return getSubpageContent( frame )
	end

	local type = 'written work'
	if frame.args.type ~= nil and frame.args.type ~= '' then
		type = frame.args.type
	end

	-- Image (without leading 'File:')
	local image = ''
	local imageDisplay = ''
	if frame.args.image ~= nil and frame.args.image ~= '' then
		image = frame.args.image
		imageDisplay = '[[File:' .. image .. '|right|300px]]'
	end

	-- Title
	local title = currentTitle.text
	if frame.args.title ~= nil and frame.args.title ~= '' then
		title = frame.args.title
	end

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

	local publisher = ''
	local publisherDisplay = ''
	if frame.args.publisher ~= nil and frame.args.publisher ~= '' then
		publisher = frame.args.publisher
		publisherDisplay = ' ' .. publisher
	end

	local location = ''
	local locationDisplay = ''
	if frame.args.location ~= nil and frame.args.location ~= '' then
		location = frame.args.location
		locationDisplay = ' (' .. location .. ')'
	end

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

	local url = currentTitle:localUrl()
	if frame.args.url ~= nil and frame.args.url ~= '' then
		url = frame.args.url
	end

	-- Wikidata
	local wikidata = ''
	local wikidataDisplay = ''
	if frame.args.wikidata ~= nil and frame.args.wikidata ~= '' then
		local wikidataItem = mw.ext.UnlinkedWikibase.getEntity( frame.args.wikidata );
		if wikidataItem ~= nil then
			wikidata = wikidataItem.id
			wikidataDisplay = 'Wikidata, [[wikidata:' .. wikidata .. '|' .. wikidata .. ']].'
		end
	end

	-- Fremantle BRN
	local fremantleBrn = ''
	local fremantleBrnDisplay = ''
	if frame.args.fremantle_brn ~= nil and frame.args.fremantle_brn ~= '' then
		fremantleBrn = frame.args.fremantle_brn
		fremantleBrnDisplay = 'Spydus, [https://fremantle.spydus.com/cgi-bin/spydus.exe/ENQ/WPAC/BIBENQ?SETLVL=&BRN=' .. fremantleBrn .. ' ' .. fremantleBrn .. '].'
	end

	-- Store data in Cargo (mainspace pages only)
	if currentTitle.namespace == 0 then
		mw.getCurrentFrame():callParserFunction( '#cargo_store', {
			'_table=works',
			wikidata = wikidata,
			fremantle_brn = fremantle_brn,
			image = image,
			title = title,
			authors = authors,
			publisher = publisher,
			location = location,
			date = date,
			url = url,
			type = type
		} )
	end

	local out = imageDisplay
		.. "This page is a record of a [[works|" .. type .. "]] relating to Fremantle.\n"
		.. "* '''Title:''' "  .. title .. "\n"
		.. "* '''Authors:''' " .. authorsDisplay .. "\n"
		.. "* '''Published:''' " .. date .. publisherDisplay .. locationDisplay .. "\n"
		.. "* '''Reference URL:''' " .. url .. "\n"
		.. "* '''Authority control:''' " .. wikidataDisplay .. ' ' .. fremantleBrnDisplay .. "\n"
	local styles = mw.getCurrentFrame():extensionTag( 'templatestyles', nil, { src = 'Module:Infobox/styles.css' } )
	return styles .. out
end

return p