-- 回头、方向旋转角度实现可参考
function OnMedicine01AttackLandedOld(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	
	local modifier = target:FindModifierByName("modifier_medicine_01_slow")
	if modifier == nil then
		keys.ability:ApplyDataDrivenModifier(caster, target, "modifier_medicine_01_slow", {Duration = keys.duration_time})
		
		local time =keys.duration_time		
		local tick = keys.tick_time
		target:AddPoison(1, caster)		
		target:SetContextThink(DoUniqueString("thtd_medicine01_debuff"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end
				if THTD_IsValid(target) == false then return nil end
				if time <= 0 then 
					target:AddPoison(-1)
					return nil 
				end
				
				local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * tick
				local DamageTable = {
					ability = keys.ability,
					victim = target, 
					attacker = caster, 
					damage = damage, 
					damage_type = keys.ability:GetAbilityDamageType(), 
					damage_flags = DOTA_DAMAGE_FLAG_NONE
			   	}
				UnitDamageTarget(DamageTable)
				time = time - tick
				return tick
			end, 
		0)

		if target.thtd_is_fearing ~= true then			
			target.thtd_is_fearing = true
			local current_next_move_point = target.next_move_point					
			target.next_move_point = RotatePosition(target:GetOrigin(), QAngle(0,RandomInt(-70, 70),0), target:GetOrigin() - target:GetForwardVector() * 500)
			local time = keys.fear_time
			target:SetContextThink(DoUniqueString("modifier_medicine_01_debuff"), 
				function()
					if GameRules:IsGamePaused() then return 0.03 end
					if not IsValidAlive(target) then 
						return nil 
					end
					if time <= 0 or THTD_IsValid(caster) == false then
						target.next_move_point = current_next_move_point																
						target.thtd_is_fearing = false
						return nil
					end

					time = time - 0.1
					return 0.1					
				end, 
			0)
		end
	else
		modifier:SetDuration(keys.duration_time,false)
	end
end

function OnMedicine01AttackLanded(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	
	local modifier = target:FindModifierByName("modifier_medicine_01_slow")
	if modifier == nil then
		keys.ability:ApplyDataDrivenModifier(caster, target, "modifier_medicine_01_slow", {Duration = keys.duration_time})
	else
		modifier:SetDuration(keys.duration_time,false)
	end
		
	local time = keys.duration_time		
	local tick = keys.tick_time
	target:AddPoison(1, caster)		
	target:SetContextThink(DoUniqueString("thtd_medicine01_debuff"), 
		function()
			if GameRules:IsGamePaused() then return 0.03 end
			if THTD_IsValid(target) == false then return nil end
			if time <= 0 then 
				target:AddPoison(-1)
				return nil 
			end
			
			local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * tick * target:GetPoisonCount()
			local DamageTable = {
				ability = keys.ability,
				victim = target, 
				attacker = caster, 
				damage = damage, 
				damage_type = keys.ability:GetAbilityDamageType(), 
				damage_flags = DOTA_DAMAGE_FLAG_NONE
			}
			UnitDamageTarget(DamageTable)
			time = time - tick
			return tick
		end, 
	0)
end

function OnMedicine02SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local targetPoint = keys.target_points[1]
	
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_medicine/ability_medicine_02.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, targetPoint)
	ParticleManager:DestroyParticleSystemTime(effectIndex, keys.duration_time)

	local time = math.floor(keys.duration_time * 100 + 0.5) / 100
	local tick = math.floor(keys.tick_time * 100 + 0.5) / 100
	-- print(keys.duration_time, time)
	-- print(keys.tick_time, tick)
	-- local startTime = GameRules:GetGameTime()
	caster:SetContextThink(DoUniqueString("modifier_medicine_02_think"), 
		function()
			if GameRules:IsGamePaused() then return 0.03 end
			if time <= 0 then 
				-- print("----- ability end")
				-- print(GameRules:GetGameTime() - startTime)
				return nil 
			end
			if THTD_IsValid(caster) == false then
				ParticleManager:DestroyParticleSystem(effectIndex,true)
				return nil 
			end

			local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * tick		
			local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.range)			
			for k,v in pairs(targets) do
				if v.thtd_is_fearing ~= true then
					v.thtd_is_fearing = true

					v:AddPoison(1, caster)
					local current_next_move_point = v.next_move_point
					v.next_move_point = targetPoint	

					v:SetContextThink(DoUniqueString("modifier_medicine_02_debuff"), 
						function()
							if GameRules:IsGamePaused() then return 0.03 end
							
							if not IsValidAlive(v) then 
								return nil 
							end
							if time <= 0 or THTD_IsValid(caster) == false then
								v.next_move_point = current_next_move_point																
								v.thtd_is_fearing = false
								return nil
							end
							return 0.1							
						end, 
					0)
				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

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