thtd_sanae_01 = class({})

LinkLuaModifier("modifier_sanae_01", "abilities/abilitySanae", LUA_MODIFIER_MOTION_NONE)

function thtd_sanae_01:OnSpellStart()
	local caster = self:GetCaster()
	local target = self:GetCursorTarget()
	
	-- AI需要改动的地方
	caster.thtd_last_cast_unit = target

	local ExtraData = { 
			count=0
		}
		
	self:Sanae01PassToNextUnit(caster,caster:GetOrigin(),target,ExtraData)
end


function thtd_sanae_01:Sanae01PassToNextUnit(source,loction,target,data)
	local caster = self:GetCaster()

	local info = 
	{
		Target = target,
		Source = source,
		Ability = self,	
		EffectName = "particles/heroes/sanae/ability_sanae_01.vpcf",
    	iMoveSpeed = 1400,
		vSourceLoc= loction,                -- Optional (HOW)
		bDrawsOnMinimap = false,                          -- Optional
		bDodgeable = true,                                -- Optional
		bIsAttack = false,                                -- Optional
		bVisibleToEnemies = true,                         -- Optional
		bReplaceExisting = false,                         -- Optional
		flExpireTime = GameRules:GetGameTime() + 10,      -- Optional but recommended
		bProvidesVision = true,
		iVisionRadius = 400,
		iVisionTeamNumber = caster:GetTeamNumber(),
		ExtraData = { 
			count = data.count + 1
		}
	}
	projectile = ProjectileManager:CreateTrackingProjectile(info)
end


function thtd_sanae_01:OnProjectileHit_ExtraData(hTarget, vLocation, data)
	local caster = self:GetCaster()
	local target = hTarget

	if target:THTD_IsTower() and target.thtd_sanae_01_power_bonus == nil then
		local duration_time = self:GetSpecialValueFor("duration_time")
		target.thtd_sanae_01_power_bonus = math.max(1, math.floor(self:GetSpecialValueFor("power_bonus") * (1 - self:GetSpecialValueFor("down_percent")/100)^(data.count - 1)))	
		target:AddNewModifier(caster, self, "modifier_sanae_01", {duration = duration_time})

		local effectIndex = ParticleManager:CreateParticle("particles/heroes/sanae/ability_sanae_01_effect.vpcf", PATTACH_CUSTOMORIGIN, target)
		ParticleManager:SetParticleControlEnt(effectIndex , 0, target, 5, "follow_origin", Vector(0,0,0), true)
		ParticleManager:DestroyParticleSystemTime(effectIndex, duration_time)
	end

	if data.count < self:GetSpecialValueFor("max_count") then	
		local targets = 
			FindUnitsInRadius(
				caster:GetTeamNumber(), 
				caster:GetOrigin(), 
				nil, 
				self:GetSpecialValueFor("range"), 
				self:GetAbilityTargetTeam(), 
				self:GetAbilityTargetType(), 
				self:GetAbilityTargetFlags(), 
				FIND_ANY_ORDER, 
				false
			)
		for k,v in pairs(targets) do
			if v~=nil and v~=target and v:THTD_IsTower() and v:HasModifier("modifier_sanae_01") == false then
				self:Sanae01PassToNextUnit(target,vLocation,v,data)
				break
			end
		end
	end

	return true
end

--------------------------------------------------------------------------------

modifier_sanae_01 = class({})

function modifier_sanae_01:OnCreated( kv )
	if IsServer() then
		local target = self:GetParent()	
		target:THTD_AddBasePower(target.thtd_sanae_01_power_bonus)
	end
end

function modifier_sanae_01:OnDestroy()
	if IsServer() then
		local target = self:GetParent()
		target:THTD_AddBasePower(-target.thtd_sanae_01_power_bonus)	
		target.thtd_sanae_01_power_bonus = nil	
	end
end

--------------------------------------------------------------------------------

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

	effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_sanae/ability_sanae_02_p.vpcf", PATTACH_CUSTOMORIGIN, nil)
	ParticleManager:SetParticleControl(effectIndex, 0, targetPoint)
	ParticleManager:DestroyParticleSystemTime(effectIndex, keys.duration_time)

	local time = keys.duration_time

	caster:SetContextThink(DoUniqueString("thtd_patchouli04_spell_start"), 
		function()
			if GameRules:IsGamePaused() then return 0.03 end
			if time <= 0 then return nil end
			
			local targets = THTD_FindUnitsInRadius(caster, targetPoint, keys.range)
			local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * 0.2
			for k,v in pairs(targets) do
				local modifier = v:FindModifierByName("modifier_sanae_debuff")
			  if modifier == nil then
					keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_sanae_debuff", {duration=0.2})
				else
					modifier:SetDuration(0.2,false)
			  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 - 0.2
			return 0.2
		end,
	0)
end

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

	local time = keys.duration_time

	local vec = targetPoint + RandomVector(keys.range)
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_sanae_03/ability_sanae_03.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, vec)
	ParticleManager:SetParticleControl(effectIndex, 1, vec)
	ParticleManager:SetParticleControl(effectIndex, 3, vec)
	ParticleManager:SetParticleControl(effectIndex, 4, vec)
	ParticleManager:SetParticleControl(effectIndex, 5, vec)
	ParticleManager:SetParticleControl(effectIndex, 6, vec)
	ParticleManager:DestroyParticleSystem(effectIndex,false)

	caster:SetContextThink(DoUniqueString("thtd_patchouli04_spell_start"), 
		function()
			if GameRules:IsGamePaused() then return 0.03 end
			if time <= 0 then return nil end
			
			local targets = THTD_FindUnitsInRadius(caster,vec,keys.range)
			local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * (1 + caster:THTD_GetFaith() * keys.damage_up/100)		
			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)
		   		UnitStunTarget(caster,v, keys.stun_time * (1 + caster:THTD_GetFaith() * keys.stun_up/100))
			end
			vec = targetPoint + RandomVector(keys.range)
			local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_sanae_03/ability_sanae_03.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex, 0, vec)
			ParticleManager:SetParticleControl(effectIndex, 1, vec)
			ParticleManager:SetParticleControl(effectIndex, 3, vec)
			ParticleManager:SetParticleControl(effectIndex, 4, vec)
			ParticleManager:SetParticleControl(effectIndex, 5, vec)
			ParticleManager:SetParticleControl(effectIndex, 6, vec)
			ParticleManager:DestroyParticleSystem(effectIndex,false)
			time = time - 0.2
			return 0.2
		end,
	0)
end

function OnSanae04SpellStart(keys)
	local caster = keys.caster

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/sanae/ability_sanea_04_effect_b.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, caster:GetOrigin())
	ParticleManager:DestroyParticleSystemTime(effectIndex, keys.duration_time)
	
	if keys.ability:GetLevel() < 2 then return end

	local hero = GameRules.HeroList[caster:GetPlayerOwnerID()]
	if hero == nil then return end
	for k,v in pairs(hero.thtd_hero_tower_list) do
		local unitName = v:GetUnitName()
		if unitName == "sanae" or unitName == "kanako" or unitName == "suwako" then 			
			keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_sanae_04_buff", nil)					
		end
	end
end

function OnCreatedSanae04Buff(keys)
	keys.target:THTD_AddBasePower(keys.power_bonus * keys.target:THTD_GetFaith()/3, "thtd_sanae_04_buff")
end

function OnDestroySanae04Buff(keys)
	keys.target:THTD_AddBasePower("thtd_sanae_04_buff")
end

function OnSanae04Created(keys)
	-- local caster = EntIndexToHScript(keys.caster_entindex)   与下面一样
	local caster = keys.caster
	local target = keys.target  

	local unitName = target:GetUnitName()
	if unitName == "sanae" or unitName == "kanako" or unitName == "suwako" then 
		if not target:HasModifier("modifier_sanae_04_combo_buff") then						
			local modifier = keys.ability:ApplyDataDrivenModifier(caster, target, "modifier_sanae_04_combo_buff", nil)
			local hero = GameRules.HeroList[caster:GetPlayerOwnerID()]
			if hero ~= nil and hero.thtd_ss_faith ~= nil and hero.thtd_ss_faith > 0 then
				modifier:SetStackCount(hero.thtd_ss_faith) 
			end			
		end
	end    
end

function OnSanae04Destroy(keys)	
	local caster = keys.caster
	local target = keys.target  
	
	local unitName = target:GetUnitName()
	if unitName == "sanae" or unitName == "kanako" or unitName == "suwako" then 
		if target:HasModifier("modifier_sanae_04_combo_buff") then						
			target:RemoveModifierByName("modifier_sanae_04_combo_buff")		
		end
		if target:HasModifier("modifier_sanae_04_buff") then			
			target:RemoveModifierByName("modifier_sanae_04_buff")		
		end
	end    
end

function OnCreatedSanae04ComboBuff(keys)
	keys.target:THTD_AddBasePower(math.floor(keys.target:THTD_GetFaith()/3), "thtd_sanae_04_combo_buff", "thtd_sanae_04_combo_buff")
end

function OnDestroySanae04ComboBuff(keys)
	keys.target:THTD_AddBasePower("thtd_sanae_04_combo_buff")
end

function OnSanaeKill(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)	
	caster:THTD_AddFaith()	
end