local thtd_satori_02 = 
{
	[4] = 0.4,
	[5] = 1.0,
}

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

	local powerDamage = 0
	local pv = caster:GetAbilityPowerValue("thtd_satori_02")
	if pv ~= nil then 					
		powerDamage = pv[1]	
	end	
	if caster:IsPower666() then 
		powerDamage = powerDamage + 400
	end
	local limit = 100 * (1 + powerDamage/100)

	local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.radius)

	caster:EmitSound("Sound_THTD.thtd_satori_01")

	for k,v in pairs(targets) do
		local modifier = v:FindModifierByName("modifier_satori_01_debuff")
		if modifier == nil then
			keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_satori_01_debuff", {Duration=keys.duration_time})
		else
			modifier:SetDuration(keys.duration_time,false)
		end

		local ability_02 = caster:FindAbilityByName("thtd_satori_02") 
		if ability_02 ~= nil and ability_02:GetLevel() > 0 and v.thtd_satori_02_debuff ~= true then
			v.thtd_satori_02_debuff = true			
			local health = v:GetHealth()	
			local time = 3.0	
			v:SetContextThink(DoUniqueString("thtd_satori02_debuff"), 
				function()
					if GameRules:IsGamePaused() then return 0.1 end
					if not THTD_IsValid(v) then return nil end
					if time <= 0 then
						local damage = math.min(limit * caster:THTD_GetStarDamage(), (health - v:GetHealth()) * ability_02:GetSpecialValueFor("damage_percent") / 100)
						local DamageTable = {
							ability = ability_02,
							victim = v, 
							attacker = caster, 
							damage = damage, 
							damage_type = ability_02:GetAbilityDamageType(), 
							damage_flags = DOTA_DAMAGE_FLAG_NONE
						}						
						UnitDamageTarget(DamageTable)
						v.thtd_satori_02_debuff = false
						return nil
					end
					time = time - 0.1
					return 0.1
				end, 
			0)
		end
	end
end

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

	if caster:HasModifier("modifier_thtd_satori_02_combo_buff") and target.thtd_satori_01_debuff ~= true then
		target.thtd_satori_01_debuff = true
		local bonus = caster:GetAbilityValue("thtd_satori_02", "damage_up")	
		target:AddDamageIncomingPhysical(bonus, "thtd_satori_01_damage_up")		
	end
end

function OnSatori01ModifierDestroy(keys)
	keys.target:AddDamageIncomingPhysical("thtd_satori_01_damage_up")
end
