function OnDaiyousei01SpellStart(keys)
	if SpawnSystem.IsUnLimited then return end
	if GameRules:GetCustomGameDifficulty() >= FUNNY_MODE then return end
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target

	if target:GetPlayerOwnerID() ~= caster:GetPlayerOwnerID() then return end

	caster:EmitSound("Hero_Enchantress.EnchantCreep")

	if target:THTD_IsTower() and target~=caster and target:THTD_GetLevel()<THTD_MAX_LEVEL then
		target:THTD_LevelUp(keys.level_up)		
	end
	
	local count = keys.level_up - 1
	local targets = THTD_FindFriendlyUnitsInRadius(caster,target:GetOrigin(),1200)

	for k,v in pairs(targets) do
		if count > 0 and v:THTD_IsTower() and v~=caster and v:THTD_GetLevel()<THTD_MAX_LEVEL and v~=target then			
			v:THTD_LevelUp(count)			
			count = count - 1
		end
	end
end

function OnDaiyousei02SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)

	caster:EmitSound("Sound_THTD.thtd_daiyousei_02")

	local targets = 
		FindUnitsInRadius(
			caster:GetTeamNumber(), 
			caster:GetOrigin(), 
			nil, 
			1000, 
			keys.ability:GetAbilityTargetTeam(), 
			keys.ability:GetAbilityTargetType(), 
			keys.ability:GetAbilityTargetFlags(), 
			FIND_ANY_ORDER, 
			false
		)

	local effectIndex = ParticleManager:CreateParticle("particles/thd2/items/item_yatagarasu.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex , 0 , caster:GetOrigin())
	ParticleManager:DestroyParticleSystem(effectIndex,false)	

	for k,v in pairs(targets) do
		if IsInDaiyousei02BlackList(v) == false then
			v:GiveMana(keys.mana_reg)
			local effectIndex = ParticleManager:CreateParticle("particles/heroes/daiyousei/ability_daiyousei_02.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex , 0 , v:GetOrigin())
			ParticleManager:DestroyParticleSystem(effectIndex,false)
		end
	end
end

local daiyousei_02_black_list =
{
	"lily",
	"daiyousei",
	"youmu",
}

function IsInDaiyousei02BlackList(unit)
	for k,v in pairs(daiyousei_02_black_list) do
		if unit:GetUnitName() == v then
			return true
		end
	end
	return false
end

function OnDaiyousei03SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	if caster == target then return end
	if not target:THTD_IsTower() then return end
	if target:HasModifier("modifier_daiyousei_03") then return end
	if target:GetPlayerOwnerID() ~= caster:GetPlayerOwnerID() then return end

	caster:EmitSound("Hero_Wisp.Tether.Target")
  
	keys.ability:ApplyDataDrivenModifier(caster, target, "modifier_daiyousei_03", nil)
	local effectIndex = Daiyousei03CreateLine(caster,target)

	local hasCombo = caster:HasModifier("modifier_thtd_daiyousei_03_combo_buff")
	if hasCombo then
		if THTD_IsValid(caster.ability_daiyousei_03_target1) == false then
			caster.ability_daiyousei_03_target1 = target
		elseif THTD_IsValid(caster.ability_daiyousei_03_target2) == false then
			caster.ability_daiyousei_03_target2 = target
		else
			caster.ability_daiyousei_03_target1 = caster.ability_daiyousei_03_target2
			caster.ability_daiyousei_03_target2 = target
		end
	else
		caster.ability_daiyousei_03_target1 = target
	end
	
	target:SetContextThink(DoUniqueString("modifier_daiyousei_03"), 
		function()
			if GameRules:IsGamePaused() then return 0.03 end

			if target == nil or target:IsNull() or target:IsAlive() == false then
				ParticleManager:DestroyParticleSystem(effectIndex,true)
				if caster ~= nil and caster:IsNull() == false then
					if caster.ability_daiyousei_03_target1 == target then 
						caster.ability_daiyousei_03_target1 = nil 
					elseif caster.ability_daiyousei_03_target2 == target then 
						caster.ability_daiyousei_03_target2 = nil 
					end
				end
				return nil
			end
			
			local isCancel = false

			if THTD_IsValid(caster) == false then
				isCancel = true
				if caster ~= nil and caster:IsNull() == false then					
					caster.ability_daiyousei_03_target1 = nil 					
					caster.ability_daiyousei_03_target2 = nil 					
				end
			else		
				if caster:HasModifier("modifier_thtd_daiyousei_03_combo_buff") == false then
					if caster.ability_daiyousei_03_target2 ~= nil then
						caster.ability_daiyousei_03_target1 = caster.ability_daiyousei_03_target2
						caster.ability_daiyousei_03_target2 = nil
					end
				end				
				isCancel = (caster.ability_daiyousei_03_target1 ~= target and caster.ability_daiyousei_03_target2 ~= target)				
			end

			if isCancel then
				if target:HasModifier("modifier_daiyousei_03") then
					target:RemoveModifierByName("modifier_daiyousei_03")
				end
				ParticleManager:DestroyParticleSystem(effectIndex,true)
				if caster ~= nil and caster:IsNull() == false then
					if caster.ability_daiyousei_03_target1 == target then 
						caster.ability_daiyousei_03_target1 = nil 
					elseif caster.ability_daiyousei_03_target2 == target then 
						caster.ability_daiyousei_03_target2 = nil 
					end
				end
				return nil
			end

			return 0.2
		end,
	0)
end

function Daiyousei03CreateLine(caster,target)
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/daiyousei/ability_daiyousei_03.vpcf", PATTACH_CUSTOMORIGIN, target)
	ParticleManager:SetParticleControlEnt(effectIndex , 0, caster, 5, "attach_hitloc", Vector(0,0,0), true)
	ParticleManager:SetParticleControlEnt(effectIndex , 1, target, 5, "attach_hitloc", Vector(0,0,0), true)
	return effectIndex
end

function OnCreatedDaiyousei03Buff(keys)	
	local target = keys.target	
	local bonus = 0
	if target:GetUnitName() == "cirno" then 	
		bonus = keys.crino_up
	else 
		bonus = keys.damage_up
	end
	target:AddDamageOutgoingMagical(bonus, "thtd_daiyousei_03_damage_up")
end

function OnUpgradeDaiyousei03Buff(keys)	
	local caster = keys.caster
	local targets = {}
	if caster.ability_daiyousei_03_target1 ~= nil then 
		table.insert(targets, caster.ability_daiyousei_03_target1)
	end
	if caster.ability_daiyousei_03_target2 ~= nil then 
		table.insert(targets, caster.ability_daiyousei_03_target2)
	end

	for _,target in pairs(targets) do
		local bonus = 0
		if target:GetUnitName() == "cirno" then 	
			bonus = keys.crino_up
		else 
			bonus = keys.damage_up
		end
		target:AddDamageOutgoingMagical(bonus, "thtd_daiyousei_03_damage_up")
	end	

	targets = {}
end

function OnDestroyDaiyousei03Buff(keys)		
	keys.target:AddDamageOutgoingMagical("thtd_daiyousei_03_damage_up")
end

function OnDaiyousei04SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target

	if caster.thtd_ability_daiyousei_04_lock == nil then caster.thtd_ability_daiyousei_04_lock = false end

	if target:GetUnitName() == "cirno" and target:THTD_GetStar() == 5 and caster.thtd_ability_daiyousei_04_lock == false then
		target:EmitSound("Hero_Wisp.Tether")
		caster.thtd_ability_daiyousei_04_lock = true
		target:THTD_UpgradeEx()		
		target:SetAttackCapability(DOTA_UNIT_CAP_MELEE_ATTACK)
		target:SetModel("models/new_touhou_model/cirno/ex/ex_cirno.vmdl")
		target:SetOriginalModel("models/new_touhou_model/cirno/ex/ex_cirno.vmdl")
		local modifier = target:AddNewModifier(target, nil, "modifier_attack_time", {})
		modifier:SetStackCount(6) -- 8对应0.8，5对应0.5，6对应0.6
		local mana_regen_ability =target:FindAbilityByName("ability_common_mana_regen_buff")
		if mana_regen_ability ~= nil then
			if mana_regen_ability:GetLevel() < mana_regen_ability:GetMaxLevel() then
				mana_regen_ability:SetLevel(5)
			end
		end

		local ability = target:FindAbilityByName("thtd_cirno_01")
		if ability ~= nil and target:HasModifier("modifier_cirno_suwako_aura") == false then
			ability:ApplyDataDrivenModifier(target, target, "modifier_cirno_suwako_aura", nil)
		end
	end
end