function OnSunny01SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local targetPoint = target:GetOrigin()
		
	local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.range)
	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability)	
	OnSunny01Damage(keys,caster,caster,target,damage)
	local count = 1
	local lastTarget = target
	local attackedTarget = {}
	for k,v in pairs(targets) do
		if count <= keys.max_count and THTD_IsValid(v) and v ~= lastTarget then		
			count = count + 1
			OnSunny01Damage(keys,caster,lastTarget,v, damage*(1+keys.damage_up/100)^count)
			lastTarget = v	
			table.insert(attackedTarget, v)		
		else
			break
		end
	end

	local fairyArea = nil
	local hero = caster:GetOwner()
	if hero~=nil and hero:IsNull()==false then
		local fairyList = GetHeroFairyList(hero)    	
		for k,v in pairs(fairyList) do
			if v.sunny == caster then
				fairyArea = v
				break
			end
		end
	end

	if fairyArea ~= nil then 
		local pos1 = fairyArea.sunny:GetAbsOrigin()
		local pos2 = fairyArea.star:GetAbsOrigin()
		local pos3 = fairyArea.luna:GetAbsOrigin()
		local center, radius = GetCircleCenterAndRadius(pos1,pos2,pos3) 
		local targetsTotal = {}
		local fairyTargets = THTD_FindUnitsInRadius(caster,center,radius)		
		for _,v in pairs(fairyTargets) do
			if v~=nil and v:IsNull()==false and v:IsAlive() and IsUnitInFairy(fairyArea,v) then									
				targetsTotal[v:GetEntityIndex()] = v
			end
		end
		local targetAdds = {}
		for k,v in pairs(targetsTotal) do
			local isExist = false
			for k2,v2 in pairs(attackedTarget) do
				if v2 == v then 
					isExist = true
					break
				end
			end
			if isExist == false then 
				table.insert(targetAdds,v)
			end
		end
		targetsTotal = {}	
		
		local factor = 0
		for i=1,keys.max_count+1 do
			factor = factor + (1+keys.damage_up/100)^(i-1)
		end
		local lastTarget = caster
		for k,v in pairs(targetAdds) do
			OnSunny01Damage(keys,caster,lastTarget,v,damage*factor)	
			lastTarget = v
		end
		targetAdds = {}
	end	
end

function OnSunny01Damage(keys,caster,target1,target2,damage)
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/sunny/ability_sunny_01_laser.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControlEnt(effectIndex , 0, target2, 5, "attach_hitloc", Vector(0,0,0), true)
	ParticleManager:SetParticleControlEnt(effectIndex , 1, target1, 5, "attach_hitloc", Vector(0,0,0), true)
	ParticleManager:SetParticleControlEnt(effectIndex , 9, target2, 5, "attach_hitloc", Vector(0,0,0), true)
	ParticleManager:DestroyParticleSystem(effectIndex,false)

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

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

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/sunny/ability_sunny_02.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex , 0, targetPoint+Vector(0,0,32))
	ParticleManager:DestroyParticleSystemTime(effectIndex,keys.duration_time)

	local fairyArea = nil
	local hero = caster:GetOwner()
	if hero~=nil and hero:IsNull()==false then
		local fairyList = GetHeroFairyList(hero)    	
		for k,v in pairs(fairyList) do
			if v.sunny == caster then
				fairyArea = v
				break
			end
		end
	end
	local center = nil
	local radius = nil
	if fairyArea ~= nil then 
		local pos1 = fairyArea.sunny:GetAbsOrigin()
		local pos2 = fairyArea.star:GetAbsOrigin()
		local pos3 = fairyArea.luna:GetAbsOrigin()
		center, radius = GetCircleCenterAndRadius(pos1,pos2,pos3) 		
	end

	local time = keys.duration_time
	caster:SetContextThink(DoUniqueString("ability_sunny_02_debuff"), 
		function()
			if GameRules:IsGamePaused() then return 0.03 end
			if time <= 0 then return nil end
			if THTD_IsValid(caster) == false then
				ParticleManager:DestroyParticleSystem(effectIndex,true)
				return nil 
			end

			local targetsTotal = {}
			local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.range)	
			if center ~= nil and radius ~= nil then 			
				local fairyTargets = THTD_FindUnitsInRadius(caster,center,radius)		
				for _,v in pairs(fairyTargets) do
					if v~=nil and v:IsNull()==false and v:IsAlive() and IsUnitInFairy(fairyArea,v) then					
						targetsTotal[v:GetEntityIndex()] = v
					end
				end	
			end	
			for k,v in pairs(targets) do
				targetsTotal[v:GetEntityIndex()] = v
			end
			targets = {}
			for k,v in pairs(targetsTotal) do
				local m = v:FindModifierByName("modifier_sunny_02_debuff")
				if m ~= nil then
					m:SetDuration(0.4, false)
				else 
					keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_sunny_02_debuff", {Duration = 0.4})
				end
			end

			time = time - 0.25
			return 0.25
		end,
	0)
end

function OnCreatedSunny02Buff(keys)	
	keys.target:AddDamageIncomingPure(keys.damage_up, "thtd_sunny_02_damage_up")
end

function OnDestroySunny02Buff(keys)		
	keys.target:AddDamageIncomingPure("thtd_sunny_02_damage_up")
end
