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

	local count = keys.max_count
	caster:SetContextThink(DoUniqueString("thtd_kaguya01_spell_think"), 
		function()
			if GameRules:IsGamePaused() then return 0.03 end
			if count <= 0 then 
				return nil
			end
			OnKaguya01SpellThink(keys,count)
			count = count - 1
			return 0.7
		end, 
	0)
end

function OnKaguya01SpellThink(keys,count)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local targetPoint = keys.target_points[1]

	for i=1,count*5 do
		local forwardVector = caster:GetForwardVector()
		local rollRad = i*math.pi*2/(count*5)
		local forwardCos = forwardVector.x
		local forwardSin = forwardVector.y
		local damageVector =  Vector(math.cos(rollRad)*forwardCos - math.sin(rollRad)*forwardSin,
								 forwardSin*math.cos(rollRad) + forwardCos*math.sin(rollRad),
								 0) * count*100 + targetPoint

		local effectIndex
		if((i*count*5)%3==0)then
			effectIndex = ParticleManager:CreateParticle("particles/thd2/heroes/kaguya/ability_kaguya01_light.vpcf", PATTACH_CUSTOMORIGIN, nil)
		elseif((i*count*5)%3==1)then
			effectIndex = ParticleManager:CreateParticle("particles/thd2/heroes/kaguya/ability_kaguya01_light_green.vpcf", PATTACH_CUSTOMORIGIN, nil)
		elseif((i*count*5)%3==2)then
			effectIndex = ParticleManager:CreateParticle("particles/thd2/heroes/kaguya/ability_kaguya01_light_red.vpcf", PATTACH_CUSTOMORIGIN, nil)
		end		
		
		ParticleManager:SetParticleControl(effectIndex, 0, damageVector)
		ParticleManager:SetParticleControl(effectIndex, 1, damageVector)
		ParticleManager:DestroyParticleSystem(effectIndex,false)
		
		local targets = THTD_FindUnitsInRadius(caster,damageVector,keys.range)
		local damage = caster:THTD_GetAbilityPowerDamage(keys.ability)
		for k,v in pairs(targets) do			
			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
	end
end

function OnKaguya02SpellThink(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)	
	if keys.ability:GetLevel() < 1 then return end

	if caster.thtd_kaguya_02_count == nil then 
		caster.thtd_kaguya_02_count = 0
	end

	if caster.thtd_kaguya_02_count == 0 then		
		local effectName = "particles/econ/items/windrunner/wr_ti8_immortal_shoulder/wr_ti8_shackleshot_pair_rope_target_glow.vpcf"
		local effectIndex = ParticleManager:CreateParticle(effectName, PATTACH_ABSORIGIN_FOLLOW, caster)
		ParticleManager:SetParticleControl(effectIndex, 0, caster:GetOrigin() + Vector(0, 0, 100))
		ParticleManager:ReleaseParticleIndex(effectIndex)
	end

	caster.thtd_kaguya_02_count = caster.thtd_kaguya_02_count + 1
	if caster.thtd_kaguya_02_count >= 20 then 
		caster.thtd_kaguya_02_count = 0
	end

	local targets = THTD_FindFriendlyUnitsInRadius(caster,caster:GetAbsOrigin(),1000)	
	for k,v in pairs(targets) do
		local modifier = v:FindModifierByName("modifier_kaguya_02_buff")
		-- 持续时间比think大一点
		if modifier == nil then			
			keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_kaguya_02_buff", {Duration = 0.3})
		else
			modifier:SetDuration(0.3, false)
		end
	end	
end

function OnCreatedKaguya02_buff(keys)
	local target = keys.target	
	local factor = 0.5
	if target:GetUnitName() == "kaguya" then 
		factor = 1
	end	
	target:THTD_AddCritChance(keys.bonus_chance * factor, "thtd_kaguya_02_bonus")
	target:THTD_AddCritDamage(keys.bonus_crit * factor, "thtd_kaguya_02_bonus")	
end

function OnRemoveKaguya02_buff(keys)
	local target = keys.target	
	target:THTD_AddCritChance("thtd_kaguya_02_bonus")
	target:THTD_AddCritDamage("thtd_kaguya_02_bonus")	
end

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

	if caster.thtd_kaguya_03_roll == false then
		caster.thtd_kaguya_03_roll = true
	else
		caster.thtd_kaguya_03_roll = false
	end	
end

function OnUpgradeKaguya03(keys)
	local caster = keys.caster
	local friends = THTD_FindFriendlyUnitsAll(caster)
	local enemies = THTD_FindUnitsAll(caster)
	for i=1,4 do
		local buff = "modifier_kaguya_03_"..i.."_buff"
		local debuff = "modifier_kaguya_03_"..i.."_debuff"
		for k,v in pairs(friends) do
			if v:FindModifierByNameAndCaster(buff,caster)~=nil then
				v:RemoveModifierByName(buff)
			end
		end
		for k,v in pairs(enemies) do
			if v:FindModifierByNameAndCaster(debuff,caster)~=nil then
				v:RemoveModifierByName(debuff)
			end
		end
	end
end

function OnKaguya03SpellThink(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)	
	if keys.ability:GetLevel() < 1 then return end
	if GameRules:IsGamePaused() then return end
	if caster:THTD_IsHidden() then 
		OnKaguya03ReleaseBall(caster)
		return 
	end

	if caster.thtd_kaguya_03_treasure_table == nil then
		caster.thtd_kaguya_03_treasure_table = {}
	end

	if caster.thtd_kaguya_03_think_count == nil then
		caster.thtd_kaguya_03_think_count = 0
	end

	if caster.thtd_kaguya_03_roll ~= false then 
		if caster.thtd_kaguya_03_think_count < 360 then
			caster.thtd_kaguya_03_think_count = caster.thtd_kaguya_03_think_count + 1
		else
			caster.thtd_kaguya_03_think_count = 0
		end
	end

	if caster.thtd_effect_count == nil then 
		caster.thtd_effect_count = 1
	end
	
	-- 旋转宝具
	for i=1,4 do
		if caster.thtd_kaguya_03_treasure_table[i] == nil then
			caster.thtd_kaguya_03_treasure_table[i] = {}
		end
		if caster.thtd_kaguya_03_treasure_table[i]["effectIndex"] == nil then
			local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_kaguya/thtd_kaguya_03_"..i..".vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex, 1, Vector(1/12,0,0))
			ParticleManager:SetParticleControl(effectIndex, 2, Vector(500/12,0,0))
			caster.thtd_kaguya_03_treasure_table[i]["effectIndex"] = effectIndex
		end

		if caster.thtd_kaguya_03_roll ~= false then 
			caster.thtd_kaguya_03_treasure_table[i]["origin"] = caster:GetOrigin() + 
				Vector(
					math.cos(i*2*math.pi/4 + caster.thtd_kaguya_03_think_count * math.pi/180)*400,
					math.sin(i*2*math.pi/4 + caster.thtd_kaguya_03_think_count * math.pi/180)*400,
					150)
		end
	end	

	local friends = {}
	local enemies = {}
	if caster.thtd_effect_count >= 10 then 
		friends = THTD_FindFriendlyUnitsAll(caster)
		enemies = THTD_FindUnitsAll(caster)
	end	
	for i=1,4 do
		if caster.thtd_kaguya_03_treasure_table[i]["effectIndex"] ~= nil then
			ParticleManager:SetParticleControl(caster.thtd_kaguya_03_treasure_table[i]["effectIndex"], 0, caster.thtd_kaguya_03_treasure_table[i]["origin"] )
		end

		if caster.thtd_effect_count >= 10 then 			
			local buff = "modifier_kaguya_03_"..i.."_buff"
			local debuff = "modifier_kaguya_03_"..i.."_debuff"
			for k,v in pairs(friends) do
				if GetDistanceBetweenTwoVec2D(caster.thtd_kaguya_03_treasure_table[i]["origin"], v:GetOrigin()) > keys.range then
					if v:FindModifierByNameAndCaster(buff,caster)~=nil then
						v:RemoveModifierByName(buff)
					end
				else
					if v:HasModifier(buff) == false then
						keys.ability:ApplyDataDrivenModifier(caster, v, buff, {})
					end
				end
			end
			for k,v in pairs(enemies) do
				if GetDistanceBetweenTwoVec2D(caster.thtd_kaguya_03_treasure_table[i]["origin"], v:GetOrigin()) > keys.range then
					if v:FindModifierByNameAndCaster(debuff,caster)~=nil then
						v:RemoveModifierByName(debuff)
					end
				else				
					if i ~= 2 and v:HasModifier(debuff) == false then
						keys.ability:ApplyDataDrivenModifier(caster, v, debuff, {})
					end
					local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * 0.2
					if i == 2 then
						damage = damage * (1 + keys.damage_up/100)
					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
			end
			if i == 4 then caster.thtd_effect_count = 1 end
		else
			if i == 4 then caster.thtd_effect_count = caster.thtd_effect_count + 1 end
		end
	end	
end


function OnKaguya03ReleaseBall(caster)	
	if caster.thtd_kaguya_03_treasure_table ~= nil and caster.thtd_kaguya_03_treasure_table[1] ~= nil and caster.thtd_kaguya_03_treasure_table[1]["effectIndex"] ~= nil then
		local friends = THTD_FindFriendlyUnitsAll(caster)
		local enemies = THTD_FindUnitsAll(caster)

		for i=1,4 do
			local buff = "modifier_kaguya_03_"..i.."_buff"
			local debuff = "modifier_kaguya_03_"..i.."_debuff"
			for k,v in pairs(friends) do
				if v:FindModifierByNameAndCaster(buff,caster)~=nil then
					v:RemoveModifierByName(buff)
				end
			end
			for k,v in pairs(enemies) do
				if v:FindModifierByNameAndCaster(debuff,caster)~=nil then
					v:RemoveModifierByName(debuff)
				end
			end

			if caster.thtd_kaguya_03_treasure_table[i]["effectIndex"] ~= nil then
				ParticleManager:DestroyParticleSystem(caster.thtd_kaguya_03_treasure_table[i]["effectIndex"],true)
				caster.thtd_kaguya_03_treasure_table[i]["effectIndex"] = nil
			end
		end
	end
end

function OnCreatedKaguya03_2_buff(keys)
	keys.target:THTD_AddAttackPercentage(keys.attack_percent, "thtd_kaguya_03_2_buff_bonus")
end

function OnDestroyKaguya03_2_buff(keys)
	keys.target:THTD_AddAttackPercentage("thtd_kaguya_03_2_buff_bonus")
end

function OnCreatedKaguya03_3_debuff(keys)
	keys.target:AddDamageIncomingAll(keys.incoming_percent, "thtd_kaguya_03_3_debuff_damage_up")
end

function OnRemoveKaguya03_3_debuff(keys)
	keys.target:AddDamageIncomingAll("thtd_kaguya_03_3_debuff_damage_up")
end

function OnCreatedKaguya03_3_buff(keys)
	keys.target:AddDamageOutgoingAll(keys.outgoing_percent, "thtd_kaguya_03_3_damage_up")
end

function OnDestroyKaguya03_3_buff(keys)	
	keys.target:AddDamageOutgoingAll("thtd_kaguya_03_3_damage_up")
end

function OnCreatedKaguya03_4_debuff(keys)	
	local target = keys.target	
	
	target:AddPhysicalArmor(-keys.armor)
end

function OnDestroyKaguya03_4_debuff(keys)		
	local target = keys.target
	
	target:AddPhysicalArmor(keys.armor)
end

function OnCreatedKaguya03_4_buff(keys)	
	keys.target:AddDamageOutgoingPhysical(keys.penetration, "thtd_kaguya_03_4_damage_up")	
end

function OnDestroyKaguya03_4_buff(keys)		
	keys.target:AddDamageOutgoingPhysical("thtd_kaguya_03_4_damage_up")
end