Implementează {{Este un fotbalist}}


local Wikidata = require('Modul:Wikidata')
local DateUtils = require('Modul:DateUtils')
local wdu_b = require('Modul:WikidataUtils/Biography')
local wdu_f = require('Modul:WikidataUtils/Football')
local getArgs = require('Modul:Arguments').getArgs
local StringUtils = require('Modul:StringUtils')
local illWd = require('Modul:Ill-wd').fromArgs
local fc = require('Modul:FootballClub')
local p = {}

local isDead = wdu_b.isDead
local isFemale = wdu_b.isFemale

local function isActive(qId)
	if isDead(qId) then return false end
	local occupations = Wikidata.findClaimsForProperty(qId, 'P106')
	if occupations then for _,occupation in ipairs(occupations) do
		if Wikidata.hasValueSnak(occupation) and Wikidata.isClaimTrue(occupation)
			and occupation.mainsnak.datavalue.value['numeric-id'] == 937857 then
				if occupation.qualifiers and occupation.qualifiers['P582'] then
					for _,endTimeQual in ipairs(occupation.qualifiers['P582']) do
						if Wikidata.isValueSnak(endTimeQual) then
							local careerEndTime = DateUtils.parseWikidataDate(endTimeQual.datavalue.value.time, endTimeQual.datavalue.value.precision)
							local now = os.date("*t")
							if DateUtils.compare(now, careerEndTime) > 0 then
								return false
							end
						end
					end
				end
		end
	end end
	return true
end

local function getPositions(qId)
	local out = {}
	local posns = Wikidata.findBestClaimsForProperty(qId, 'P413')
	if posns then for _,posn in ipairs(posns) do
		if Wikidata.isClaimTrue(posn) and Wikidata.hasValueSnak(posn) then
			table.insert(out, Wikidata.findLabel(posn.mainsnak.datavalue.value.id, 'ro'))
		end
	end end
	return out
end

local function getCurrentTeamClaim(qId)
	local teams = Wikidata.findBestClaimsForProperty(qId, 'P54')
	if teams then for _,team in ipairs(teams) do
		if Wikidata.hasBetterRank(team, 'preferred') and Wikidata.hasValueSnak(team) then
			return team
		end
	end end
	return nil
end

local function isPlayerLoaned(crtTeamClaim)
	if not crtTeamClaim then return false end
	if crtTeamClaim.qualifiers and crtTeamClaim.qualifiers['P1642'] then
		for _,eachAcqTrans in ipairs(crtTeamClaim.qualifiers['P1642']) do
			if Wikidata.isValueSnak(eachAcqTrans) and eachAcqTrans.datavalue.value['numeric-id'] == 2914547 then
				return true
			end
		end
	end
	return false
end

local function getLenderTeamClaim(qId)
	local teams = Wikidata.findClaimsForProperty(qId, 'P54')
	if teams then for _,team in ipairs(teams) do
		if Wikidata.hasBetterRank(team, 'normal') and Wikidata.hasValueSnak(team) and not wdu_f.isNationalTeam(team.mainsnak.datavalue.value.id) then
			if team.qualifiers['P580'] and not team.qualifiers['P582'] and not isPlayerLoaned(team) then
				return team
			end
		end
	end end
	return nil
end

local function getCurrentTeam(qId)
	local crtTeamClaim = getCurrentTeamClaim(qId)
	if not crtTeamClaim then return nil end
	local out = illWd(crtTeamClaim.mainsnak.datavalue.value.id, fc.labelFromArgs(crtTeamClaim.mainsnak.datavalue.value.id))
	local crtLeagueId = Wikidata.loadOneValueInChain({crtTeamClaim.mainsnak.datavalue.value.id, 'P118', 'raw'})
	if crtLeagueId and mw.ustring.len(crtLeagueId) > 0 then
		out = out .. ' în ' .. illWd(crtLeagueId)
	end
	if isPlayerLoaned(crtTeamClaim) then
		local lenderTeamClaim = getLenderTeamClaim(qId)
		if lenderTeamClaim then
			out = out .. (isFemale(qId) and ', împrumutată de la ' or ', împrumutat de la ') .. Wikidata.printSnak(lenderTeamClaim.mainsnak)
		end
	end
	return out .. Wikidata.outputReferences(crtTeamClaim)
end

local function isCoach(qId)
	local occupations = Wikidata.findClaimsForProperty(qId, 'P106')
	if occupations then for _,occupation in ipairs(occupations) do
		if Wikidata.hasValueSnak(occupation) and Wikidata.isClaimTrue(occupation)
			and occupation.mainsnak.datavalue.value['numeric-id'] == 628099 then
				return true
		end
	end end
	return false
end

local function getCurrentCoachedTeam(qId)
	local teams = Wikidata.findBestClaimsForProperty(qId, 'P6087')
	if teams then for _,team in ipairs(teams) do
		if Wikidata.hasBetterRank(team, 'preferred') and Wikidata.hasValueSnak(team) then
			return Wikidata.printSnak(team.mainsnak)
		end
	end end
	return nil
end

local function getRelevantTeams(qId, propId, includeNationalTeams)
	local teams = Wikidata.findClaimsForProperty(qId, propId)
	local timeSpentAtTeams = {}
	local endTimeAtTeams = {}
	if teams then for _,team in ipairs(teams) do
		if Wikidata.isClaimTrue(team) and Wikidata.hasValueSnak(team) and team.qualifiers and team.qualifiers['P580'] and team.qualifiers['P582'] and (includeNationalTeams or not wdu_f.isNationalTeam(team.mainsnak.datavalue.value.id)) then
			local startTime, endTime
			for __,eachStartTimeQual in ipairs(team.qualifiers['P580']) do
				if Wikidata.isValueSnak(eachStartTimeQual) then
					startTime = DateUtils.parseWikidataDate(eachStartTimeQual.datavalue.value.time, eachStartTimeQual.datavalue.value.precision)
				end
			end
			for __,eachEndTimeQual in ipairs(team.qualifiers['P582']) do
				if Wikidata.isValueSnak(eachEndTimeQual) then
					endTime = DateUtils.parseWikidataDate(eachEndTimeQual.datavalue.value.time, eachEndTimeQual.datavalue.value.precision)
				end
			end
			if not endTime or not startTime then
				break
			end
			local crtTimeDiff = timeSpentAtTeams[team.mainsnak.datavalue.value.id] or 0
			crtTimeDiff = crtTimeDiff + DateUtils.daysBetween(startTime, endTime)
			timeSpentAtTeams[team.mainsnak.datavalue.value.id] = crtTimeDiff
			endTimeAtTeams[team.mainsnak.datavalue.value.id] = endTime
		end
	end end
	local teamIds = {}
	for tk,tv in pairs(timeSpentAtTeams) do table.insert(teamIds, tk) end
	table.sort(teamIds, function(t1, t2) 
		return timeSpentAtTeams[t2] < timeSpentAtTeams[t1]
	end)
	local ret = {}
	for idx = 1,3 do
		if idx <= #teamIds then table.insert(ret, illWd(teamIds[idx], fc.labelFromArgs(teamIds[idx], endTimeAtTeams[teamIds[idx]]))) end
	end

	return ret
end

local function getRelevantCoachedTeams(qId)
	return getRelevantTeams(qId, 'P6087', true)
end

local function getRelevantPlayingTeams(qId)
	return getRelevantTeams(qId, 'P54', false)
end
p.getRelevantPlayingTeams = getRelevantPlayingTeams

local function getNationalTeams(qId)
	local teams = Wikidata.findClaimsForProperty(qId, 'P54')
	local nationalTeamIds = {}
	local nationalCaps = {}
	if teams then for _,team in ipairs(teams) do
		if Wikidata.isClaimTrue(team) and Wikidata.hasValueSnak(team) and wdu_f.isNationalTeam(team.mainsnak.datavalue.value.id) then
			if team.qualifiers and team.qualifiers['P1350'] then
				local matchesPlayed = 0
				for _,matchesPlayedQual in ipairs(team.qualifiers['P1350']) do
					if Wikidata.isValueSnak(matchesPlayedQual) then
						matchesPlayed = matchesPlayed + tonumber(matchesPlayedQual.datavalue.value.amount)
					end
				end
				if matchesPlayed > 0 then 
					table.insert(nationalCaps, illWd(team.mainsnak.datavalue.value.id, wdu_f.getNationalTeamName(team.mainsnak.datavalue.value.id)))
				end
			end
		end
	end end
	if #nationalCaps > 0 then
		return 'Pe plan internațional, are prezențe la națională pentru ' .. mw.text.listToText(nationalCaps, ', ', ' și ')
	end
	return nil
end
p.getNationalTeams = getNationalTeams

local function getPlayingInformation(qId)
	local out = ''
	local crtTeam = getCurrentTeam(qId)
	local posns = getPositions(qId)
	local nationalPresence = getNationalTeams(qId)
	if isActive(qId) then
		if nil == crtTeam then out = out .. (isFemale(qId) and ' liberă de contract' or ' liber de contract') end
		if #posns > 0 then
			out = out .. ', care joacă pe post de ' .. mw.text.listToText(posns, ', ', ' sau ')
		else
			out = out .. ', care joacă'
		end
		if crtTeam then
			out = out .. ' la ' .. crtTeam
		end
		if nationalPresence then
			out = out .. '. ' .. nationalPresence
		end
	else 
		out = out .. (isFemale(qId) and ' retrasă din activitate' or ' retras din activitate')
		if #posns > 0 then
			out = out .. ', care a jucat pe post de ' .. mw.text.listToText(posns, ', ', ' sau ')
			local relevantTeams = getRelevantPlayingTeams(qId)
			if #relevantTeams > 0 then
				out = out .. (#relevantTeams == 1 and ' la ' or ' la echipe ca ') .. mw.text.listToText(relevantTeams, ', ', ' și ')
			end
		end
		if nationalPresence then
			out = out .. '. ' .. nationalPresence
		end
		if isCoach(qId) then
			out = out .. '.'
			out = out .. ' După încheierea carierei, a devenit ' .. (isFemale(qId) and 'antrenoare' or 'antrenor')
			local crtCoachedTeam = getCurrentCoachedTeam(qId)
			if crtCoachedTeam then
				out = out .. ', și antrenează ' .. crtCoachedTeam
			else
				local relevantCoachedTeams = getRelevantCoachedTeams(qId)
				if #relevantCoachedTeams > 0 then
					out = out .. ', și a antrenat, printre altele, ' .. mw.text.listToText(relevantCoachedTeams, ', ', ' și ')
				end
			end
		end
	end
	out = out .. '.'
	return out
end

local function getCountries(qId)
	local countries = {}
	local countryClaims= Wikidata.findBestClaimsForProperty(qId, 'P27')
	if countryClaims then for _,countryClaim in ipairs(countryClaims) do
		if Wikidata.hasValueSnak(countryClaim) and Wikidata.isClaimTrue(countryClaim) then
			table.insert(countries, countryClaim.mainsnak.datavalue.value.id)
		end
	end end
	return countries
end

local function assembleCountries(qId, countries)
	if not countries or #countries == 0 then return '' end
	local adjectives = {}
	for _,country in ipairs(countries) do
		local demonyms = Wikidata.findClaimsForProperty(country, 'P1549')
		local crtadjective = nil
		if demonyms then for _,demonym in ipairs(demonyms) do
			if Wikidata.isClaimTrue(demonym) and Wikidata.hasValueSnak(demonym) and demonym.mainsnak.datavalue.value.language == 'ro' then
				if demonym.qualifiers and demonym.qualifiers['P518'] then
					for _,demonymMaybeGender in ipairs(demonym.qualifiers['P518']) do
						if Wikidata.isValueSnak(demonymMaybeGender) then
							if demonymMaybeGender.datavalue.value['numeric-id'] == 1775415 and isFemale(qId) then
								crtadjective = demonym.mainsnak.datavalue.value.text
							elseif demonymMaybeGender.datavalue.value['numeric-id'] == 499327 and not isFemale(qId) then
								crtadjective = demonym.mainsnak.datavalue.value.text
							end
						end
					end
				end
			end
		end end
		crtadjective = crtadjective or ('din ' .. Wikidata.findLabel(country, 'ro'))
		table.insert(adjectives, crtadjective)
	end
	return mw.text.listToText(adjectives, ', ', ' și ')
end

function p.fromArgs(qId)
	local out = ''
	out = out .. (isDead(qId) and 'a fost' or 'este')
	out = out .. ' '
	out = out .. (isFemale(qId) and 'o fotbalistă' or 'un fotbalist')
	local countries = getCountries(qId)
	out = out .. ' '
	if #countries > 0 then
		out = out .. assembleCountries(qId, countries)
	end
	
	out = out .. getPlayingInformation(qId)
	
	return out
end

function p.fromFrame(frame)
	local args = getArgs(frame)
	return p.fromArgs(args.q or mw.wikibase.getEntityIdForCurrentPage())
end
return p