---------PromotionID
local FrighteningID = GameInfo.UnitPromotions["PROMOTION_FRIGHTENING"].ID
local FrightenedID = GameInfo.UnitPromotions["PROMOTION_TEMP_FRIGHTENED"].ID


function NewAttackDeBuff( attPlayerID,attUnitID, _, _, _, defPlayerID, defUnitID )
----------Defines and status checks
--print ("I am working")
	local attPlayer	= Players[attPlayerID]
	if not attPlayer then 
	   return
	end
	
	local attUnit = attPlayer:GetUnitByID(attUnitID)	
	if not attUnit then 
	   return
	end
	
	local defPlayer	= Players[defPlayerID]	
	if not defPlayer then 
	   return
	end
	
	local defUnit = defPlayer:GetUnitByID(defUnitID)	
	if not defUnit then 
	   return
	end
--print ("I find it")

-------Attacking bring enemy debuff
	if attUnit:IsHasPromotion(FrighteningID) then
    		defUnit:SetMoves(0)
			print ("Cool")
		if not defUnit:IsHasPromotion(FrightenedID) then
   			defUnit:SetHasPromotion(FrightenedID, true)
   		end
------------------Notification
		if attPlayer:IsHuman() then
			--print ("Nice shot!")
-------------------------------------------------Map XY Grids
			local attX = attUnit:GetX()
			local attY = attUnit:GetY()
			local defX = defUnit:GetX()
			local defY = defUnit:GetY()
    		local heading = Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_ENEMY_FARED_SHORT");
			local text = Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_ENEMY_FARED", defUnit:GetName());
			attPlayer:AddNotification(NotificationTypes.NOTIFICATION_GENERIC , text, heading, defX, defY)
			return
		end
		if defPlayer:IsHuman() then
			--print ("So poor you!")
-------------------------------------------------Map XY Grids
			local attX = attUnit:GetX()
			local attY = attUnit:GetY()
			local defX = defUnit:GetX()
			local defY = defUnit:GetY()
    		local heading = Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_ARMY_FARED_SHORT");
			local text = Locale.ConvertTextKey("TXT_KEY_NOTIFICATION_ARMY_FARED", defUnit:GetName());
			defPlayer:AddNotification(NotificationTypes.NOTIFICATION_GENERIC , text, heading, defX, defY)
			return
		end
	end
end
GameEvents.CombatResult.Add( NewAttackDeBuff )


---------------Remove Debuff Frightened
function RemoveFrightened(playerID)
local player = Players[playerID]

--print ("I am looking for")
	if not player:IsAlive() or (player:GetNumUnits() == 0) then
		return
	else
		for unit in player:Units() do
			if ( unit:IsHasPromotion(FrightenedID)) and ((unit:GetCurrHitPoints()) == (unit:GetMaxHitPoints())) then
   			 unit:SetHasPromotion(FrightenedID, false)
--print ("I removed one")
   			end
		end
	end
end


GameEvents.PlayerDoTurn.Add(RemoveFrightened)