function OnAttackKomachi01(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/komachi/ability_komachi_02.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex , 0, caster:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex , 1, caster:GetOrigin())
	ParticleManager:SetParticleControlForward(effectIndex, 1, caster:GetForwardVector())
	ParticleManager:DestroyParticleSystem(effectIndex, false)
end

function OnAttackLandedKomachi01(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local range = caster:Script_GetAttackRange()
	local targets = THTD_FindUnitsInRadius(caster,caster:GetAbsOrigin(),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

function OnCreatedKomachi01Buff(keys)
	-- 被动需要加延时确保在初始化之后
	keys.target:SetContextThink(DoUniqueString("dota_timer"), 
		function() 
			if GameRules:IsGamePaused() then return 0.1 end
			keys.target:THTD_AddCritChance(keys.chance, "thtd_komachi_01_chance")
			return nil 
		end, 
	0.3)	
end

function OnThinkKomachi01(keys)
	local caster = keys.caster
	local range = caster:Script_GetAttackRange()
	local targets = THTD_FindUnitsInRadius(caster,caster:GetAbsOrigin(),range) 
	local crit = 0
	local chance = 0
	for k,v in pairs(targets) do
		local lostHp = 100 - v:GetHealthPercent()
		crit = crit + math.floor(lostHp/4) * keys.crit_bonus		
	end
	caster:THTD_AddCritDamage(crit, "thtd_komachi_01_bonus")	
end

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

	local effectIndex = ParticleManager:CreateParticle("particles/units/heroes/hero_dark_seer/dark_seer_vacuum.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, targetPoint)
	ParticleManager:SetParticleControl(effectIndex, 1, Vector(keys.radius,0, 0))

	caster:EmitSound("Hero_Dark_Seer.Vacuum")	

	local time = math.floor(keys.duration_time * 100 + 0.5) / 100
	local tick = 0.03	
	caster:SetContextThink(DoUniqueString("thtd_komachi_02"), 
		function()		
			if THTD_IsValid(caster) == false then 
				ParticleManager:DestroyParticleSystem(effectIndex, true)				
				return nil 
			end
			if time < 0 then 
				ParticleManager:DestroyParticleSystem(effectIndex, true)
				caster:StopSound("Hero_Dark_Seer.Vacuum")

				targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.radius)
				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	

				return nil 
			end

			local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.radius)
			for k, v in pairs(targets) do
				local distance = (v:GetAbsOrigin() - targetPoint):Length2D()
				local in_pull = 700
				local new_pos = GetGroundPosition(v:GetAbsOrigin(), v)
				if distance > 20 then
					local direction = (targetPoint - new_pos):Normalized()
					direction.z = 0.0
					new_pos = new_pos + direction * in_pull * tick					
				end
				v:SetOrigin(new_pos)	
			end
			time = time - tick
			return tick
		end,
	0)
end

function OnSpellStartKomachi03(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local targetUnit = keys.target

	local targets = {}
	local count = 0
	local modifierName = "modifier_komachi_03_debuff"

	if not targetUnit:HasModifier(modifierName) then 
		table.insert(targets, targetUnit)
		count = count + 1
	end
	if count < keys.max_count then
		local rangeTargets = THTD_FindUnitsInRadius(caster,targetUnit:GetAbsOrigin(),1000) 
		for k,v in pairs(rangeTargets) do
			if not v:HasModifier(modifierName) then 
				table.insert(targets, v)
				count = count + 1
				if count >= keys.max_count then
					break
				end
			end
		end
	end
	if #targets == 0 then 
		keys.ability:EndCooldown()
		return 
	end

	for _,target in pairs(targets) do
		keys.ability:ApplyDataDrivenModifier(caster, target, modifierName, {})		
	end
end
					
function OnCreatedKomachi03Debuff(keys)
	keys.target:AddDamageIncomingAll(keys.damage_up, "thtd_komachi_02_damage_up")
end

function OnDestroyKomachi03Debuff(keys)
	local caster = keys.caster
	local target = keys.target	
	
	target:AddDamageIncomingAll("thtd_komachi_02_damage_up")

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/komachi/ability_komachi_03_explosion_2.vpcf", PATTACH_CUSTOMORIGIN, target)
	ParticleManager:SetParticleControl(effectIndex , 0, target:GetOrigin() + Vector(0,0,128))
	ParticleManager:DestroyParticleSystem(effectIndex, false)
end	

function OnSpellStartKomachi04(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local vecCaster = caster:GetOrigin()	

	if target:HasModifier("modifier_komachi_04") then 
		keys.ability:EndCooldown()
		return 
	end

	keys.ability:ApplyDataDrivenModifier(caster,caster,"modifier_komachi_04",{Duration = 2.0})
	keys.ability:ApplyDataDrivenModifier(caster,target,"modifier_komachi_04",{Duration = 2.0})	
	target.komachi_04_damage_lock = true

	local rad = GetRadBetweenTwoVec2D(caster:GetOrigin(),target:GetOrigin())
	local tarForward = Vector(math.cos(rad),math.sin(rad),0)

	target:SetForwardVector(tarForward)	
	
	-- 渐隐效果
	-- local effectIndex_start = ParticleManager:CreateParticle("particles/heroes/komachi/ability_komachi_04_start.vpcf", PATTACH_CUSTOMORIGIN, nil)
	-- ParticleManager:SetParticleControl(effectIndex_start, 0, vecCaster)
	-- ParticleManager:SetParticleControlForward(effectIndex_start, 0, tarForward)

	-- caster:SetContextThink(DoUniqueString("OnKomachi04SpellStart"), 
	-- 	function ()
	-- 		if GameRules:IsGamePaused() then return 0.03 end

			local effectIndex = ParticleManager:CreateParticle("particles/heroes/komachi/ability_komachi_04_blink.vpcf", PATTACH_CUSTOMORIGIN, target)
			ParticleManager:SetParticleControl(effectIndex, 0, target:GetOrigin()-tarForward*250)
			ParticleManager:SetParticleControlForward(effectIndex, 0, tarForward)

			caster:SetOrigin(target:GetOrigin()-tarForward*150)
			caster:SetForwardVector(tarForward)
			caster:StartGesture(ACT_DOTA_CAST_ABILITY_4)
						
	-- 		return nil
	-- 	end,
	-- 2.0)
	
	local time = 2.0
	caster:SetContextThink(DoUniqueString("OnKomachi04SpellStart_stage_2"), 
		function ()			
			if GameRules:IsGamePaused() then return 0.1 end			
			if not THTD_IsValid(caster) then
				target.komachi_04_damage_lock = false				
				target:RemoveModifierByName("modifier_komachi_04")	
				ParticleManager:DestroyParticleSystem(effectIndex, true)
				if caster ~= nil and caster:IsNull() == false then
					caster:RemoveModifierByName("modifier_komachi_04")
				end				
				return nil
			end
			if not THTD_IsValid(target) then
				caster:SetOrigin(vecCaster)
				caster:RemoveModifierByName("modifier_komachi_04")
				if target ~= nil and target:IsNull() == false then
					target:RemoveModifierByName("modifier_komachi_04")
				end
				caster:EmitSound("Hero_Axe.Culling_Blade_Success")
				
				local bonusTime = math.floor(keys.duration_time * 100 + 0.5) / 100				
				local modifier = caster:FindModifierByName("modifier_komachi_04_buff")
				if modifier == nil then 
					keys.ability:ApplyDataDrivenModifier(caster,caster,"modifier_komachi_04_buff",{Duration=bonusTime})					
				else					
					modifier:SetDuration(bonusTime, false)
				end
				
				if keys.bonus_power > 0 then 					
					local skipedCount = 0
					if SpawnSystem.ReachToWave ~= nil then 
						if caster.skiped_wave == nil then
							caster.skiped_wave = SpawnSystem.ReachToWave + 50 - SpawnSystem.CurWave - 1
						end
					elseif caster.skiped_wave ~= nil then 
						skipedCount = caster.skiped_wave * 2
						caster.skiped_wave = nil
					end
					modifier = caster:FindModifierByName("modifier_komachi_04_kill_power_bonus")
					if modifier == nil then						
						keys.ability:ApplyDataDrivenModifier(caster,caster,"modifier_komachi_04_kill_power_bonus",{}):SetStackCount(1)
						caster:THTD_AddBasePower(keys.bonus_power)
					elseif modifier:GetStackCount() < 999 then
						local addCount = math.min(999 - modifier:GetStackCount(), 1 + skipedCount)
						modifier:SetStackCount(modifier:GetStackCount() + addCount)
						caster:THTD_AddBasePower(keys.bonus_power * addCount)
					end
				end

				return nil
			end		

			if time <= 0 then				
				target.komachi_04_damage_lock = false
				caster:RemoveModifierByName("modifier_komachi_04")
				target:RemoveModifierByName("modifier_komachi_04")				

				local effectIndex_end = ParticleManager:CreateParticle("particles/heroes/komachi/ability_komachi_04_scythe.vpcf", PATTACH_CUSTOMORIGIN, target)
				ParticleManager:SetParticleControl(effectIndex_end, 0, target:GetOrigin())
				ParticleManager:SetParticleControl(effectIndex_end, 1, target:GetOrigin())			
				
				caster:SetOrigin(vecCaster)

				if target:GetHealthPercent() <= keys.hp_kill then 
					caster:NpcKill(target)
				else
					caster:AbilityKill(target, keys.ability)					
				end
				time = -1
				return 0.1
			end

			if time <= -1 then 
				return nil 
			end

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

function OnCreatedKomachi04Buff(keys)
	local target = keys.target
	target:THTD_AddAttackSpeed(keys.attack_speed, "thtd_komachi_04_bonus")
	target:AddDamageOutgoingAll(keys.damage_up, "thtd_komachi_04_bonus")
end

function OnDestroyKomachi04Buff(keys)
	local target = keys.target
	target:THTD_AddAttackSpeed("thtd_komachi_04_bonus")
	target:AddDamageOutgoingAll("thtd_komachi_04_bonus")
end