-- JFD_Egypt_Functions
-- Author: JFD
-- DateCreated: 4/14/2017 12:16:59 AM
--==========================================================================================================================
-- UTILITY FUNCTIONS
--==========================================================================================================================
--HasLeaderTrait
function HasLeaderTrait(leaderType, traitType)
	for row in GameInfo.LeaderTraits() do
		if (row.LeaderType == leaderType and row.TraitType == traitType) then return true end
	end
	return false
end

--GetStrongestNavalUnit
local unitQuadriremeID		= GameInfo.Units["UNIT_QUADRIREME"].Index
local unitTesserakonteresID = GameInfo.Units["UNIT_JFD_TESSERAKONTERES"].Index
function GetStrongestNavalUnit(player)
	local unitID = nil
	local unitCombat = 0
	for row in GameInfo.Units() do
		if (row.Domain == "DOMAIN_SEA" and row.Combat > 0 and row.Combat > unitCombat and ((not row.TraitType) or row.TraitType == "TRAIT_LEADER_UNIT_JFD_TESSERAKONTERES")) then
			if (((row.PrereqTech and player:GetTechs():HasTech(GameInfo.Technologies[row.PrereqTech].Index)) or (row.PrereqCivic and player:GetCulture():HasCivic(GameInfo.Civics[row.PrereqCivic].Index))) or (not row.PrereqTech and not row.PrereqCivic)) then
				if ((not row.StrategicResource) or (row.StrategicResource and player:GetResources():HasResource(GameInfo.Resources[row.StrategicResource]))) then  
					unitID = row.Index
					unitCombat = row.Combat
					--if unitID == unitQuadriremeID then unitID = unitTesserakonteresID end
				end
			end
		end
	end	
	return unitID
end	
--==========================================================================================================================
-- CORE FUNCTIONS
--==========================================================================================================================
-- EGYPT (PTOLEMY): PEARL OF THE MEDITERRANEAN
----------------------------------------------------------------------------------------------------------------------------
local traitPearlMediterranean = "TRAIT_LEADER_JFD_PEARL_MEDITERRANEAN"

--JFD_EgyptPtolemy_CityProductionCompleted
local districtHarborID = GameInfo.Districts["DISTRICT_HARBOR"].Index
function JFD_EgyptPtolemy_CityProductionCompleted(playerID, cityID, orderType, itemType)
	local player = Players[playerID]
	local playerConfig = PlayerConfigurations[playerID]
	local leaderType = playerConfig:GetLeaderTypeName()
	local city = player:GetCities():FindID(cityID)
	if (not city) then return end
	if (not HasLeaderTrait(leaderType, traitPearlMediterranean)) then return end
	if orderType ~= 1 then return end 
	if GameInfo.Buildings[itemType].PrereqDistrict ~= "DISTRICT_HARBOR" then return end
	local unitID = GetStrongestNavalUnit(player)
	if (not unitID) then return end
	local districtLocation = player:GetCities():GetCapitalCity():GetDistricts():GetDistrict(districtHarborID)
	player:GetUnits():Create(unitID, districtLocation:GetX(), districtLocation:GetY())    
end
Events.CityProductionCompleted.Add(JFD_EgyptPtolemy_CityProductionCompleted)
--==========================================================================================================================
--==========================================================================================================================

