function OnSuwako01SpellHit(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local damage_table = {
		ability = keys.ability,
		victim = target,
		attacker = caster,
		damage = caster:THTD_GetAbilityPowerDamage(keys.ability),
		damage_type = keys.ability:GetAbilityDamageType(), 
		damage_flags = 0
	}
	UnitDamageTarget(damage_table)
end

function OnSuwako01AttackLanded(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target

	local info = 
	{
			Ability = keys.ability,
        	EffectName = "particles/heroes/thtd_suwako/ability_suwako_01_ring.vpcf",
        	vSpawnOrigin = caster:GetOrigin() + Vector(0,0,128),
        	fDistance = caster:Script_GetAttackRange(),
        	fStartRadius = 200,
        	fEndRadius = 200,
        	Source = caster,
        	bHasFrontalCone = false,
        	bReplaceExisting = false,
        	iUnitTargetTeam = DOTA_UNIT_TARGET_TEAM_ENEMY,
        	iUnitTargetFlags = DOTA_UNIT_TARGET_FLAG_NONE,
        	iUnitTargetType = DOTA_UNIT_TARGET_HERO + DOTA_UNIT_TARGET_BASIC,
        	fExpireTime = GameRules:GetGameTime() + 10.0,
			bDeleteOnHit = false,
			vVelocity = caster:GetForwardVector() * 1500,
			bProvidesVision = true,
			iVisionRadius = 1000,
			iVisionTeamNumber = caster:GetTeamNumber()
	}
	projectile = ProjectileManager:CreateLinearProjectile(info)
	ParticleManager:DestroyLinearProjectileSystem(projectile,false)
end


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

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

	caster.thtd_suwako_02_rect["point"]  = targetPoint
	if caster.thtd_suwako_02_rect["effectIndex"] == nil then
		caster.thtd_suwako_02_rect["effectIndex"] = ParticleManager:CreateParticle("particles/heroes/thtd_suwako/ability_suwako_02.vpcf", PATTACH_CUSTOMORIGIN, caster)
		ParticleManager:SetParticleControl(caster.thtd_suwako_02_rect["effectIndex"], 0, targetPoint)
		ParticleManager:SetParticleControl(caster.thtd_suwako_02_rect["effectIndex"], 1, Vector(keys.range,1,1))
	else
		ParticleManager:DestroyParticleSystem(caster.thtd_suwako_02_rect["effectIndex"],true)
		caster.thtd_suwako_02_rect["effectIndex"] = ParticleManager:CreateParticle("particles/heroes/thtd_suwako/ability_suwako_02.vpcf", PATTACH_CUSTOMORIGIN, caster)
		ParticleManager:SetParticleControl(caster.thtd_suwako_02_rect["effectIndex"], 0, targetPoint)
		ParticleManager:SetParticleControl(caster.thtd_suwako_02_rect["effectIndex"], 1, Vector(keys.range,1,1))
	end
end

function OnSuwako02Think(keys)
	if GameRules:IsGamePaused() then return end
	local caster = EntIndexToHScript(keys.caster_entindex)
	if caster.thtd_suwako_02_rect == nil then
		caster.thtd_suwako_02_rect = {}
	end
	if keys.ability:GetLevel() < 1 or caster:THTD_IsHidden() then 
		if caster.thtd_suwako_02_rect["effectIndex"]~=nil then
			ParticleManager:DestroyParticleSystem(caster.thtd_suwako_02_rect["effectIndex"],true)
		end
		return
	end

	if caster.thtd_suwako_02_rect["effectIndex"] ~= nil then
		local point = caster.thtd_suwako_02_rect["point"]
		local targets = THTD_FindUnitsInRadius(caster,point,keys.range)
		local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * 0.2 * (1 + caster:THTD_GetFaith()*keys.up_percent/100)	
		for k,v in pairs(targets) do
			if v.thtd_suwako_02_count == nil then
				v.thtd_suwako_02_count = 0
			end
			if v.thtd_suwako_02_lock == nil then
				v.thtd_suwako_02_lock = false
			end
			v.thtd_suwako_02_count = v.thtd_suwako_02_count + 1
			if v.thtd_suwako_02_count > keys.stay_time/0.2 and v.thtd_suwako_02_lock == false then
				keys.ability:ApplyDataDrivenModifier(caster,v,"modifier_suwako_rooted",{Duration = keys.root_time})
				v.thtd_suwako_02_lock = true

				v:SetContextThink(DoUniqueString("thtd_suwako_02_leave_rect"), 
					function()
						if GameRules:IsGamePaused() then return 0.03 end
						if GetDistanceBetweenTwoVec2D(point,v:GetOrigin()) > keys.range then
							v.thtd_suwako_02_count = 0
							v.thtd_suwako_02_lock = false
							return nil
						else
							local damage_table = {
								ability = keys.ability,
								victim = v,
								attacker = caster,
								damage = damage,
								damage_type = keys.ability:GetAbilityDamageType(), 
								damage_flags = 0
							}
							UnitDamageTarget(damage_table)
						end
						return 0.2
					end, 
				0)
			end
		end
	end
end

function OnSuwako03SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local inners = THTD_FindUnitsInner(caster)

	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * (1 + caster:THTD_GetFaith()*keys.up_percent/100)
	for k,v in pairs(inners) do
		if v:HasModifier("modifier_suwako_03_rooted") == false then
			local g = -10
			local vh = 120
			local t = math.abs(2*vh/g)
			local targetPoint = v:GetOrigin() -- + RandomVector(200)
			local rad = GetRadBetweenTwoVec2D(v:GetOrigin(),targetPoint)
			local dis = GetDistanceBetweenTwoVec2D(v:GetOrigin(),targetPoint)
			local speed = dis/t
			local curOrigin = v:GetOrigin()
			local originz = curOrigin.z
			
			keys.ability:ApplyDataDrivenModifier(caster,v,"modifier_suwako_03_rooted",{})

			local effectIndex = ParticleManager:CreateParticle("particles/econ/items/kunkka/kunkka_torrent_base/kunkka_spell_torrent_splash_econ.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex, 0, v:GetOrigin())
			ParticleManager:DestroyParticleSystem(effectIndex,false)

			v:SetContextThink(DoUniqueString("thtd_suwako_03_unit_up"), 
				function()
					if GameRules:IsGamePaused() then return 0.03 end
					vh = vh + g
					curOrigin = Vector(curOrigin.x + math.cos(rad) * speed,curOrigin.y + math.sin(rad) * speed,curOrigin.z + vh)
					v:SetAbsOrigin(curOrigin)

					if curOrigin.z >= originz then
						return 0.03
					else
						-- FindClearSpaceForUnit(v, v:GetOrigin(), false)
						v:SetAbsOrigin(targetPoint)

						local damage_table = {
							ability = keys.ability,
							victim = v,
							attacker = caster,
							damage = damage,
							damage_type = keys.ability:GetAbilityDamageType(), 
							damage_flags = 0
						}
						UnitDamageTarget(damage_table)
						v:RemoveModifierByName("modifier_suwako_03_rooted")
						return nil
					end
				end,
			0)
		end
	end
end

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

function OnSuwako04SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target

	caster:EmitSound("Sound_THTD.thtd_suwako_04")
	caster.thtd_last_cast_unit = target

	local modifier = target:FindModifierByName("modifier_suwako_04_buff")
	if modifier == nil then 
		keys.ability:ApplyDataDrivenModifier(caster, target, "modifier_suwako_04_buff", {duration = keys.duration_time})
	else
		modifier:SetDuration(keys.duration_time, false)
	end	
end

function OnCreatedSuwako04Buff(keys)
	local unitName = keys.target:GetUnitName()
	if unitName == "suwako" or unitName == "kanako" or unitName == "sanae" then 
		keys.target:THTD_AddCritDamage(keys.self_crit, "thtd_suwako_04_bonus")	
	else
		keys.target:THTD_AddCritDamage(keys.crit, "thtd_suwako_04_bonus")	
	end
end

function OnDestroySuwako04Buff(keys)
	keys.target:THTD_AddCritDamage("thtd_suwako_04_bonus")
end

