require('strict')

--[=[
Implements [[Template:PD-1996]] and country-specific derivatives thereof
]=]

local p = {} --p stands for package

local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local PD = require('Module:PD')
local country_data = require('Module:PD-1996/data')

local function PD_1996_condition(args)
	if not args then
		args = {}
	end
	local home_country = args.home_country or PD.license_grammar({"its", "their"}) .. " home country"
	local uraa_date = args.uraa_date or "January 1, 1996 for most countries"
	local text = " in the '''[[w:public domain|public domain]]''' in the United States because " .. PD.license_grammar({"it was", "they were"}) .. " first published outside the United States (and ''not'' published in the U.S. within 30 days), '''and''' " .. PD.license_grammar({"it was", "they were"}) .. " first published before 1989 without complying with U.S. copyright formalities (renewal and/or copyright notice) '''and''' " .. PD.license_grammar({"it was", "they were"}) .. " in the public domain in " .. home_country .. " on the [[w:Uruguay Round Agreements Act|URAA]] date (" .. uraa_date .. ")."
	return text
end

function p._PD_1996(args)
	local film = args.film
	local anon = yesno(args.anon) or false
	local deathyear
	local pubyear
	if anon then
		deathyear = nil
		pubyear = PD.getPublicationYear({args[1], args.pubyear})
	else
		deathyear = PD.getAuthorDeathYear({args[1], args.deathyear})
		pubyear = PD.getPublicationYear({args[2], args.pubyear})
	end
	
	local basecat
	local category
	local text
	if anon then
		basecat = "PD-anon"
		category = args.category or PD.category_with_year_floor(basecat, {80, 70, 60, 50}, pubyear) .. "-1996"
		text = PD.license_scope() .. " anonymous or pseudonymous, and " .. PD.license_grammar({"is", "are"}) .. " " .. PD_1996_condition()
		if pubyear then
			text = text .. " " .. PD.license_grammar({"It is", "They are"}) .. " also in the public domain in other countries and areas where the [[w:List of countries' copyright length based on publication and creation dates|copyright terms of anonymous or pseudonymous works]] are '''" .. PD.currentyear - pubyear - 1 .. " years or less since publication'''."
		end
	else
		basecat = "PD"
		category = args.category or PD.category_with_deathyear_floor(basecat, deathyear) .. "-1996"
		text = PD.license_scope() .. PD_1996_condition()
	end
	text = text .. PD.shorter_term_text(deathyear, film)
	
	-- Should we use a different license?
	if pubyear and pubyear < PD.PD_US_cutoff then
		if anon then
			return require('Module:PD-anon-US')._PD_anon_US({['pubyear'] = pubyear, ['category'] = args.category})
		else
			return require('Module:PD-US')._PD_US({['deathyear'] = deathyear, ['category'] = args.category})
		end
	elseif pubyear and pubyear > 1988 then
		return PD.error_text(basecat .. "-1996 does not apply to works published after 1988.", basecat .. "-1996")
	else
		return PD.license({
			['image'] = PD.PD_image,
			['text'] = text,
			['category'] = category
		})
	end
end

function p.PD_1996(frame)
	return p._PD_1996(getArgs(frame))
end

-- Country-specific templates

local function info_text(args)
	local author_text
	if args.deathyear or args.rehabyear then
		author_text = "The author"
		if args.gpw then
			author_text = author_text .. ", who fought in the Great Patriotic War,"
		end
		if args.deathyear then
			author_text = author_text .. " died in '''" .. args.deathyear .. "'''"
			if args.rehabyear then
				author_text = author_text .. " and"
			end
		end
		if args.rehabyear then
			author_text = author_text .. " was rehabilitated"
			if not (args.deathyear and args.deathyear > args.rehabyear) then
				author_text = author_text .. " posthumously"
			end
			author_text = author_text .. " in '''" .. args.rehabyear .. "'''"
		end
		author_text = author_text .. "."
	elseif args.anon then
		author_text = PD.license_scope() .. " anonymous or pseudonymous due to unknown authorship."
	end
	
	local work_text
	if args.posthumous or args.pubyear or args.creationyear then
		work_text = PD.license_scope({past = true})
		if args.creationyear then
			work_text = work_text .. " created in '''" .. args.creationyear
		end
		if args.posthumous or args.pubyear then
			if args.creationyear then
				work_text = work_text .. " and"
			end
			work_text = work_text .. " published"
			if args.posthumous and not (args.deathyear and args.pubyear and args.deathyear > args.pubyear) then
				work_text = work_text .. " posthumously"
			end
			if args.pubyear then
				work_text = work_text .. " in '''" .. args.pubyear .. "'''"
			end
		end
		work_text = work_text .. "."
	end
	
	if author_text and work_text then
		return author_text .. " " .. work_text
	elseif author_text or work_text then
		return (author_text or "") .. (work_text or "")
	else
		return nil
	end
end

function p._PD_1996_also(args)
	args.deathyear = PD.getAuthorDeathYear({args[1], args.deathyear})
	args.pubyear = PD.getPublicationYear({args[2], args.pubyear})
	args.rehabyear = tonumber(args.rehabyear)
	args.creationyear = tonumber(args.creationyear)
	args.posthumous = yesno(args.posthumous) or false
	args.anon = yesno(args.anon) or false
	args.gpw = yesno(args.gpw) or false
	
	local country = args.country
	
	-- Is the country specified?
	if not country then
		return PD.error_text("[[Module:PD-1996]] error: _PD_1996_also requires a country to be specified.", nil)
	end
	
	local template
	if country == "India" or country == "South Africa" or country == "South Africa speech" then
		template = "PD " .. country
	else
		template = "PD-" .. country
	end
	
	-- Do we have data for the country?
	if not country_data[country] then
		return PD.error_text("[[Module:PD-1996]] error: " .. country .. " is not yet a country supported by this module.", nil)
	end
	
	-- Should we use a different license?
	if args.pubyear and args.pubyear < PD.PD_US_cutoff then
		return require('Module:PD-US')._PD_US({['deathyear'] = args.deathyear, ['category'] = args.category})
	elseif args.pubyear and args.pubyear > 1988 then
		return PD.error_text(template .. " does not apply to works published after 1988.", template)
	end
	
	-- Is the license inappropriate?
	local country_error_text
	if country_data[country]['error_text'] then
		country_error_text = country_data[country]['error_text'](args)
	end
	if country_error_text then
		return PD.error_text(country_error_text, template)
	end
	
	-- text
	local text = country_data[country]['text'] .. "\n----\n" .. PD.license_scope() .. " also" .. PD_1996_condition({['home_country'] = country, ['uraa_date'] = country_data[country]['uraa_date']}) .. " This is the combined effect of " .. country .. " having joined the [[w:Berne Convention for the Protection of Literary and Artistic Works|Berne Convention]] in " .. country_data[country]['berne_year'] .. ", and of [https://www.copyright.gov/title17/92chap1.html#104a 17 USC 104A] with its critical date of January 1, 1996."
	text = table.concat({text, country_data[country]['uraa_text'], info_text(args)}, "\n\n") .. PD.shorter_term_text(args.deathyear, args.film)
	
	return PD.license({
		['image'] = PD.PD_image,
		['image_r'] = country_data[country]['image_r'],
		['text'] = text,
		['category'] = args.category or template,
		['warning'] = country_data[country]['warning']
	})
end

function p.PD_1996_also(frame)
	return p._PD_1996_also(getArgs(frame))
end

return p