function OnJunko01SpellStart(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_junko_01") then return end
	if target:GetPlayerOwnerID() ~= caster:GetPlayerOwnerID() then return end

	keys.ability:ApplyDataDrivenModifier(caster, target, "modifier_junko_01", nil)
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_junko/ability_junko_01_buff.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControlEnt(effectIndex , 0, target, 5, "follow_origin", Vector(0,0,0), true)

	local hasCombo = caster:HasModifier("modifier_thtd_junko_01_combo_buff")
	if hasCombo then
		if THTD_IsValid(caster.ability_junko_01_target1) == false then
			caster.ability_junko_01_target1 = target
		elseif THTD_IsValid(caster.ability_junko_01_target2) == false then
			caster.ability_junko_01_target2 = target
		else
			caster.ability_junko_01_target1 = caster.ability_junko_01_target2
			caster.ability_junko_01_target2 = target
		end
	else
		caster.ability_junko_01_target1 = target
	end
	
	target:SetContextThink(DoUniqueString("modifier_junko_01"), 
		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_junko_01_target1 == target then 
						caster.ability_junko_01_target1 = nil 
					elseif caster.ability_junko_01_target2 == target then 
						caster.ability_junko_01_target2 = nil 
					end
					if caster.ability_junko_01_target1 == nil and caster.ability_junko_01_target2 == nil then 
						ParticleManager:DestroyParticleSystem(caster.ability_junko_01_effectIndex,true)
						caster.ability_junko_01_effectIndex = 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_junko_01_target1 = nil 					
					caster.ability_junko_01_target2 = nil					
				end
			else		
				if caster:HasModifier("modifier_thtd_junko_01_combo_buff") == false then
					if caster.ability_junko_01_target2 ~= nil then
						caster.ability_junko_01_target1 = caster.ability_junko_01_target2
						caster.ability_junko_01_target2 = nil
					end
				end				
				isCancel = (caster.ability_junko_01_target1 ~= target and caster.ability_junko_01_target2 ~= target)				
			end

			if isCancel then
				if target:HasModifier("modifier_junko_01") then
					target:RemoveModifierByName("modifier_junko_01")
				end
				ParticleManager:DestroyParticleSystem(effectIndex,true)
				if caster ~= nil and caster:IsNull() == false then
					if caster.ability_junko_01_target1 == target then 
						caster.ability_junko_01_target1 = nil 
					elseif caster.ability_junko_01_target2 == target then 
						caster.ability_junko_01_target2 = nil 
					end
				end
				if caster.ability_junko_01_target1 == nil and caster.ability_junko_01_target2 == nil then 
					ParticleManager:DestroyParticleSystem(caster.ability_junko_01_effectIndex,true)
					caster.ability_junko_01_effectIndex = nil
				end					
				return nil
			end

			return 0.2	
		end,
	0.2)	
end

function OnCreatedJunko01Buff(keys)	
	local target = keys.target	
	
	local bonus = 0
	if target:GetUnitName() == "hecatia" then
		bonus = keys.hecatia_up
	else 
		bonus = keys.damage_up
	end
	target:AddDamageOutgoingPure(bonus, "thtd_junko_01_damage_up")	
end

function OnUpgradeJunko01Buff(keys)	
	local caster = keys.caster
	local targets = {}
	if caster.ability_junko_01_target1 ~= nil then 
		table.insert(targets, caster.ability_junko_01_target1)
	end
	if caster.ability_junko_01_target2 ~= nil then 
		table.insert(targets, caster.ability_junko_01_target2)
	end

	for _,target in pairs(targets) do
		local bonus = 0
		if target:GetUnitName() == "hecatia" then
			bonus = keys.hecatia_up
		else 
			bonus = keys.damage_up
		end
		target:AddDamageOutgoingPure(bonus, "thtd_junko_01_damage_up")	
	end	

	targets = {}
end

function OnDestroyJunko01Buff(keys)		
	keys.target:AddDamageOutgoingPure("thtd_junko_01_damage_up")
end

function OnJunko02SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)		
	local casterForward = caster:GetForwardVector()	
	local casterPoint = caster:GetOrigin()

	local point = THTDSystem:FindRadiusOnePointPerfectAOE(caster, caster:Script_GetAttackRange(), 390 * 2)
	if point ~= nil then
		casterForward = (Vector(point.x, point.y,casterPoint.z)  - casterPoint):Normalized()		
	end

	local duration_time = math.floor(keys.duration_time * 100 + 0.5) / 100
	local tick = math.floor(keys.tick * 100 + 0.5) / 100
	
	local time = duration_time

	caster:SetContextThink("modifier_junko_02_think", 
		function()
			if GameRules:IsGamePaused() then return 0.03 end
			if not THTD_IsValid(caster) then return nil end
			if time <= 0 then return nil end
			
			local forwardCos = casterForward.x
			local forwardSin = casterForward.y
			local angle = (39 - 6.5 * RandomInt(0,12)) / 180 * math.pi
			local forward = Vector(	math.cos(angle)*forwardCos - math.sin(angle)*forwardSin,
									forwardSin*math.cos(angle) + forwardCos*math.sin(angle),0)
			local info = 
			{
					Ability = keys.ability,
		        	EffectName = "particles/heroes/thtd_junko/ability_junko_02.vpcf",
		        	vSpawnOrigin = casterPoint + forward * 500 - casterForward * 500 + Vector(0,0,128),
		        	fDistance = 1200,
		        	fStartRadius = 150,
		        	fEndRadius = 150,
		        	Source = caster,
		        	bHasFrontalCone = false,
		        	bReplaceExisting = false,
					iUnitTargetTeam = DOTA_UNIT_TARGET_TEAM_ENEMY,				
		        	iUnitTargetFlags = DOTA_UNIT_TARGET_FLAG_NONE,
		        	iUnitTargetType = DOTA_UNIT_TARGET_HERO, -- 解决撞击后消失
		        	fExpireTime = GameRules:GetGameTime() + 10.0,
					bDeleteOnHit = false,
					vVelocity = casterForward * 1800,
					bProvidesVision = true,
					iVisionRadius = 1000,
					iVisionTeamNumber = caster:GetTeamNumber()
			}
			local projectile = ProjectileManager:CreateLinearProjectile(info)
			ParticleManager:DestroyLinearProjectileSystem(projectile,false)

			local targets = 
				FindUnitsInLine(
					caster:GetTeamNumber(), 
					casterPoint, 
					casterPoint + casterForward * 1500,
					nil, 
					390,
					keys.ability:GetAbilityTargetTeam(), 
					keys.ability:GetAbilityTargetType(), 
					keys.ability:GetAbilityTargetFlags()
				)
		
			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	

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

function OnJunko03SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local friends = THTD_FindFriendlyUnitsAll(caster)

	local duration_time = math.floor(keys.duration_time * 100 + 0.5) / 100
	local damage_up = keys.damage_up
	local power_suck = keys.power_suck

	local modifier = caster:FindModifierByName("modifier_junko_03_buff")
	if modifier == nil then 
		keys.ability:ApplyDataDrivenModifier(caster, caster, "modifier_junko_03_buff", nil)
	else
		modifier:SetDuration(duration_time, false)
	end

	local total = 0
	for k,v in pairs(friends) do
		if v ~= caster then
			local power = v:THTD_GetBasePower()
			local bonus_power = math.min(power, power_suck)
			if bonus_power > 0 then 
				total = total + bonus_power
				v:THTD_AddBasePower(-bonus_power)				
				v:SetContextThink(DoUniqueString("dota_timer"), 
					function() 
						if GameRules:IsGamePaused() then return 0.1 end
						v:THTD_AddBasePower(bonus_power)	
						return nil
					end, 
				duration_time)
			end
		end
	end
	if total > 0 then 
		caster:THTD_AddBasePower(total)	
		caster:SetContextThink(DoUniqueString("dota_timer"), 
			function() 
				if GameRules:IsGamePaused() then return 0.1 end
				caster:THTD_AddBasePower(-total)						
				return nil
			end, 
		duration_time)
	end	

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_junko/ability_junko_03.vpcf", PATTACH_CUSTOMORIGIN, nil)
	ParticleManager:SetParticleControl(effectIndex, 0, caster:GetAbsOrigin())			
	ParticleManager:DestroyParticleSystem(effectIndex,false)
end

function OnCreatedJunko03Buff(keys)		
	keys.target:AddDamageOutgoingAll(keys.damage_up, "thtd_junko_03_damage_up")	
end

function OnDestroyJunko03Buff(keys)		
	keys.target:AddDamageOutgoingAll("thtd_junko_03_damage_up")
end

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

	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability, 1)
	local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.range)
	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)
		if THTD_IsValid(v) and v:HasModifier("modifier_junko_04_debuff") == false then		
			keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_junko_04_debuff", nil)				
		end	
	end

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_junko/ability_junko_04.vpcf", PATTACH_CUSTOMORIGIN, nil)
	ParticleManager:SetParticleControl(effectIndex, 0, targetPoint+Vector(0,0,64))		
	ParticleManager:SetParticleControl(effectIndex, 1, Vector(1,0,0))			
	ParticleManager:SetParticleControl(effectIndex, 2, Vector(255,255,255))				
	ParticleManager:SetParticleControl(effectIndex, 3, targetPoint)	
	ParticleManager:DestroyParticleSystem(effectIndex,false)
end

function OnThinkJunko04Debuff(keys)	
	local caster = keys.caster
	local target = keys.target
	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability, 2)
	local DamageTable = {
		ability = keys.ability,
		victim = target, 
		attacker = caster, 
		damage = damage, 
		damage_type = keys.ability:GetAbilityDamageType(), 
		damage_flags = DOTA_DAMAGE_FLAG_NONE
	}
	UnitDamageTarget(DamageTable)
end
