local inaba_01_black_list =
{
	"lily",
	"daiyousei",	
}

function IsInInaba01BlackList(unit)
	for k,v in pairs(inaba_01_black_list) do
		if unit:GetUnitName() == v then
			return true
		end
	end
	return false
end

function OnSpellStartInaba01(keys)
	local caster = keys.caster
	local target = keys.target

	if target:THTD_IsTower() == false or target:GetPlayerOwnerID() ~= caster:GetPlayerOwnerID() then 	
		keys.ability:EndCooldown()
		caster:GiveMana(caster:GetRealManaCost(keys.ability))	
		return
	end

	caster.thtd_last_cast_unit = target
	caster:EmitSound("Hero_OgreMagi.Bloodlust.Cast")
	
	local modifier = target:FindModifierByName("modifier_inaba_01_buff")
	if modifier == nil then 
		modifier = keys.ability:ApplyDataDrivenModifier(caster, target, "modifier_inaba_01_buff", nil)
	else
		modifier:SetDuration(keys.duration, false)
	end
	
	local effectName = "particles/econ/items/ogre_magi/ogre_ti8_immortal_weapon/ogre_ti8_immortal_bloodlust_buff.vpcf"
	local effectIndex = ParticleManager:CreateParticle(effectName, PATTACH_CUSTOMORIGIN, caster)
    ParticleManager:SetParticleControlEnt(effectIndex, 0, caster, PATTACH_POINT_FOLLOW, "attach_hitloc", caster:GetAbsOrigin(), true)
    ParticleManager:SetParticleControlEnt(effectIndex, 2, target, PATTACH_CUSTOMORIGIN_FOLLOW, nil, target:GetAbsOrigin(), true)
    ParticleManager:SetParticleControlEnt(effectIndex, 3, target, PATTACH_CUSTOMORIGIN_FOLLOW, nil, target:GetAbsOrigin(), true)
	ParticleManager:DestroyParticleSystem(effectIndex,false)
end


function OnCreatedInaba01Buff(keys)
	local target = keys.target
	target:THTD_AddCritChance(keys.chance, "thtd_inaba_01_buff_bonus")	
	target:THTD_AddAttackSpeed(keys.attack_speed, "thtd_inaba_01_buff_bonus")
	if not IsInInaba01BlackList(target) then 
		target:AddManaCostReducePercent(keys.mana_cost, "thtd_inaba_01_buff_bonus")	
	end
end

function OnDestroyInaba01Buff(keys)
	local target = keys.target	
	target:THTD_AddCritChance("thtd_inaba_01_buff_bonus")	
	target:THTD_AddAttackSpeed("thtd_inaba_01_buff_bonus")	
	if not IsInInaba01BlackList(target) then 
		target:AddManaCostReducePercent("thtd_inaba_01_buff_bonus")	
	end
end


function OnAttackLandedInaba02(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local targetPoint = keys.target:GetAbsOrigin()

	caster.thtd_inaba_02_attack_count = (caster.thtd_inaba_02_attack_count or 0) + 1

	local powerDamageUp = 0
	local pv = caster:GetAbilityPowerValue("thtd_inaba_02")
	if pv ~= nil then 	
		powerDamageUp = pv[1]
	end
	
	local damage_up = keys.damage_up + powerDamageUp
	local max_count = keys.max_count
	if caster:IsPower999() then 
		max_count = max_count * 2
	end

	if caster:HasModifier("modifier_inaba_02_buff") then 
		caster:FindModifierByName("modifier_inaba_02_buff"):SetDuration(2.0, false)	
	end

	if caster.thtd_inaba_02_attack_count % keys.count1 == 0 then 
		if caster.thtd_inaba_power666_bonus_power ~= nil then 
			caster:THTD_AddPowerPercentage(-caster.thtd_inaba_power666_bonus_power)
			caster.thtd_inaba_power666_bonus_power = nil
		end
		if caster:IsPower999() then 
			caster.thtd_inaba_power666_bonus_power = 300
			caster:THTD_AddPowerPercentage(caster.thtd_inaba_power666_bonus_power)
		end
		local unit = CreateUnitByName(
			"inaba_rabbit"
			,caster:GetOrigin() + caster:GetForwardVector() * 120
			,false
			,caster:GetOwner()
			,caster:GetOwner()
			,caster:GetTeam()
		)

		local lastVec = target:GetAbsOrigin()
		if unit ~= nil then 
			unit:SetControllableByPlayer(caster:GetPlayerOwnerID(), false) 			
			unit.thtd_spawn_unit_owner = caster
			keys.ability:ApplyDataDrivenModifier(caster, unit, "modifier_inaba_02_rabbit", nil)
			unit:AddNewModifier(unit, nil, "modifier_phased", {})
			local kill_time = 10.0
			unit:SetContextThink(DoUniqueString("dota_timer"), 
				function() 
					if GameRules:IsGamePaused() then return 0.1 end
					kill_time = kill_time - 0.2
					if THTD_IsValid(caster) == false or kill_time < 0 then 
						unit:AddNoDraw()
						unit:ForceKill(false)
						return nil						
					end 
					unit:MoveToPosition(lastVec)
					if THTD_IsValid(target) then 
						lastVec = target:GetAbsOrigin()
					end
					if (lastVec - unit:GetAbsOrigin()):Length2D() < 100 then 
						-- EmitSoundOnLocationWithCaster(lastVec, "Hero_Techies.LandMine.Detonate", caster)						
						local effectIndex = ParticleManager:CreateParticle("particles/units/heroes/hero_techies/techies_blast_off.vpcf", PATTACH_POINT, caster)
						ParticleManager:SetParticleControl(effectIndex , 0, lastVec)
						ParticleManager:DestroyParticleSystem(effectIndex, false)
						unit:AddNoDraw()
						unit:ForceKill(false)

						local boomTargets = THTD_FindUnitsInRadius(caster,lastVec,keys.range) 
						local damage = caster:THTD_GetAbilityPowerDamage(keys.ability, 1)
						local modifier = caster:FindModifierByName("modifier_inaba_02_buff")
						if modifier ~= nil then
							local stack_count = modifier:GetStackCount()
							damage = damage * (1 + stack_count * damage_up/100)
							modifier:SetStackCount(math.min(max_count, stack_count+1))						
						else
							keys.ability:ApplyDataDrivenModifier(caster, caster, "modifier_inaba_02_buff", {duration = 2.0}):SetStackCount(1)
						end
						for k,v in pairs(boomTargets) 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
					
					return 0.2
				end, 
			0.2)			
		end
	end

	if caster.thtd_inaba_02_attack_count >= keys.count2 then 
		caster.thtd_inaba_02_attack_count = 0
		local effectName = "particles/econ/items/sniper/sniper_fall20_immortal/sniper_fall20_immortal_shrapnel.vpcf"
		local effectIndex = ParticleManager:CreateParticle(effectName, PATTACH_CUSTOMORIGIN, caster)
		ParticleManager:SetParticleControl(effectIndex, 0, target:GetAbsOrigin())
		ParticleManager:SetParticleControl(effectIndex, 1, Vector(keys.range, 0, 0))
		local time = math.floor(keys.duration * 100 + 0.5)/100
		local tick = 0.5
		caster:SetContextThink(DoUniqueString("dota_timer"), 
			function() 
				if GameRules:IsGamePaused() then return 0.1 end
				time = time - tick
				if time < 0 or THTD_IsValid(caster) == false then 
					ParticleManager:DestroyParticleSystem(effectIndex,true)
					return nil 
				end 

				local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.range) 
				local damage = caster:THTD_GetAbilityPowerDamage(keys.ability, 2)
				local modifier = caster:FindModifierByName("modifier_inaba_02_buff")
				if modifier ~= nil then				
					damage = damage * (1 + modifier:GetStackCount() * damage_up/100)									
				end
				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 tick
			end, 
		0.1)	

	end
end
