
function OnMerlin01SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local targetPoint = keys.target_points[1]

	caster:EmitSound("Sound_THTD.thtd_merlin_01")

	local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.radius)
	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability)
	for k,v in pairs(targets) do
		keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_merlin_01_debuff", nil)

		UpdatePrismriverComboName(caster,v)
		
		local comboName = GetPrismriverComboName(v)		
		if comboName == "lunasalyricamerlin" then
			OnLunasaLyricaMerlin(keys,caster,v)
			ResetPrismriverComboName(v)
		elseif comboName == "lyricalunasamerlin" then
			OnLyricaLunasaMerlin(keys,caster,v)	
			ResetPrismriverComboName(v)		
		end		

		local DamageTable = {
			ability = keys.ability,
	        victim = v, 
	        attacker = caster, 
	        damage = damage, 
	        damage_type = keys.ability:GetAbilityDamageType(), 
	        damage_flags = DOTA_DAMAGE_FLAG_NONE
		}
		UnitDamageTarget(DamageTable)
	end

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/merlin/ability_merlin_01.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, targetPoint)
	ParticleManager:DestroyParticleSystem(effectIndex,false)
end

function OnMerlin01Created(keys)
	keys.target:AddDamageOutgoingAll(keys.outgoing_percent, "thtd_merlin_01_damage_up")
end

function OnMerlin01Destroy(keys)
	keys.target:AddDamageOutgoingAll("thtd_merlin_01_damage_up")
end

function OnMerlin02AttackLanded(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	if keys.ability:GetLevel() < 1 then return end
	
	local abilty_01 = caster:FindAbilityByName("thtd_merlin_01")
	abilty_01:ApplyDataDrivenModifier(caster, target, "modifier_merlin_01_debuff", nil)

	local DamageTable = {
		ability = keys.ability,
		victim = target, 
		attacker = caster, 
		damage = caster:THTD_GetAbilityPowerDamage(keys.ability), 
		damage_type = keys.ability:GetAbilityDamageType(), 
		damage_flags = DOTA_DAMAGE_FLAG_NONE
   	}
	UnitDamageTarget(DamageTable)

	UpdatePrismriverComboName(caster,target)
end

function OnLunasaLyricaMerlin(keys,caster,target)
	local targets = THTD_FindFriendlyUnitsInRadius(caster,caster:GetOrigin(),2500)
	for k,v in pairs(targets) do
		local unitName = v:GetUnitName()
		if unitName == "lunasa" or unitName == "merlin" or unitName == "lyrica" then
			keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_merlin_01_buff", nil)
		end
	end
end

function OnLyricaLunasaMerlin(keys,caster,target)
	local targets = THTD_FindFriendlyUnitsInRadius(caster,caster:GetOrigin(),2500)
	for k,v in pairs(targets) do
		local unitName = v:GetUnitName()
		if unitName == "lunasa" or unitName == "merlin" or unitName == "lyrica" then
			v:GiveMana(keys.mana_regen)
		end
	end
end
