function OnFlandre01SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)

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

	caster:EmitSound("Hero_PhantomLancer.Doppelganger.Appear")

	for i=1,3 do
		local illusion = CreateUnitByName(
				"flandre_illusion", 
				caster:GetOrigin() + Vector(math.cos(math.pi/2*(i+1)),math.sin(math.pi/2*(i+1)),0)*100, 
				false, 
				caster, 
				caster, 
				caster:GetTeam() 
			)
		illusion:SetControllableByPlayer(caster:GetPlayerOwnerID(), true) 
		illusion:MoveToPosition(illusion:GetOrigin()+Vector(0,-100,0))
		illusion:SetForwardVector(caster:GetForwardVector())
		keys.ability:ApplyDataDrivenModifier(caster, illusion, "modifier_flandre_01_illusion", nil)

		local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_flandre/ability_flandre_02_fire.vpcf", PATTACH_CUSTOMORIGIN, illusion) 
		ParticleManager:SetParticleControlEnt(effectIndex , 0, illusion, 5, "attach_attack1", Vector(0,0,0), true)

		caster.thtd_flandre_illusion_table[i] = illusion

		local count = 0
		illusion:SetContextThink(DoUniqueString("thtd_flandre_01_illusion"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end
				if count > 20 or THTD_IsValid(caster) == false then 
					illusion:AddNoDraw()
					illusion:ForceKill(true)
					return nil
				elseif caster:IsAttacking()==false then
					illusion:RemoveGesture(ACT_DOTA_ATTACK)
					illusion:MoveToPosition(illusion:GetOrigin()+Vector(0,-100,0))
				end
				count = count + 1
				return 0.5
			end, 
		0)
	end
end

function OnFlandre02Created(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_flandre/ability_flandre_02_fire.vpcf", PATTACH_CUSTOMORIGIN, caster) 
	ParticleManager:SetParticleControlEnt(effectIndex , 0, caster, 5, "attach_attack1", Vector(0,0,0), true)

	caster.thtd_flandre_02_effectIndex = effectIndex
end

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

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

	for k,v in pairs(caster.thtd_flandre_illusion_table) do
		if v~=nil and v:IsNull()==false and v:IsAlive() then
			local forward = (target:GetOrigin() - v:GetOrigin()):Normalized()
			v:StartGesture(ACT_DOTA_ATTACK)
			v:MoveToTargetToAttack(target)
		end
	end
end

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

	local ability4 = caster:FindAbilityByName("thtd_flandre_04")
	if ability4:GetLevel() >= 1 then 
		if caster.lock_target ~= target then 
			if THTD_IsValid(caster.lock_target) then 
				caster.lock_target:RemoveModifierByName("modifier_flandre_damage_lock")
			end
			keys.ability:ApplyDataDrivenModifier(caster, target, "modifier_flandre_damage_lock", {duration = 2.0})
			caster.lock_target = target
		else
			local modifier = target:FindModifierByName("modifier_flandre_damage_lock")
			if modifier ~= nil then 
				modifier:SetDuration(2.0, false)
			else
				keys.ability:ApplyDataDrivenModifier(caster, target, "modifier_flandre_damage_lock", {duration = 2.0})
			end
		end
		if ability4:IsCooldownReady() then 
			target.flandre_04_lock = true
		else
			target.flandre_04_lock = false
		end
	end

	local damage = caster:THTD_GetAbilityPowerDamage(keys.ability) * 4
	local crit = 1
	local ability3 = caster:FindAbilityByName("thtd_flandre_03")
	if ability3:GetLevel() > 0 then		
		crit = 1 + (100 - target:GetHealthPercent()) * ability3:GetSpecialValueFor("damage_up") / 100
	end

	local DamageTable = {
		ability = keys.ability,
        victim = target, 
        attacker = caster, 
        damage = damage * crit, 
        damage_type = keys.ability:GetAbilityDamageType(), 
        damage_flags = DOTA_DAMAGE_FLAG_NONE
	}
	UnitDamageTarget(DamageTable)
	OnFlandre02SpellStart(caster)
end

function OnFlandre02SpellStart(caster)	
	local step = caster:GetAbilityValue("thtd_flandre_02", "damage_up") * 4
	local maxUp = caster:GetAbilityValue("thtd_flandre_02", "max_up")
	local duration = caster:GetAbilityValue("thtd_flandre_02", "duration_time")	
	
	if caster.thtd_flandre_02_outgoing == nil then
		caster.thtd_flandre_02_outgoing = 0
	end
	
	local count = math.min(step, maxUp - caster.thtd_flandre_02_outgoing)
	if count > 0 then
		caster:AddDamageOutgoingPhysical(count)		
		caster.thtd_flandre_02_outgoing = caster.thtd_flandre_02_outgoing + count		
		caster:SetContextThink(DoUniqueString("modifier_flandre_02_buff_count"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end
				caster:AddDamageOutgoingPhysical(-count)				
				caster.thtd_flandre_02_outgoing = caster.thtd_flandre_02_outgoing - count
				return nil
			end, 
		duration)
	end
end

function OnFlandre02Destroy(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	ParticleManager:DestroyParticleSystem(caster.thtd_flandre_02_effectIndex,false)
end

function OnCreatedFlandreLock(keys)	
	local target = keys.target

	local effectIndex = ParticleManager:CreateParticle("particles/units/heroes/hero_bounty_hunter/bounty_hunter_track_trail.vpcf", PATTACH_POINT_FOLLOW, target) 
	ParticleManager:SetParticleControlEnt(effectIndex , 0, target, PATTACH_ABSORIGIN_FOLLOW, "attach_hitloc", target:GetAbsOrigin(), true)
	ParticleManager:SetParticleControlEnt(effectIndex , 1, target, PATTACH_POINT_FOLLOW, "attach_hitloc", target:GetAbsOrigin(), true)
	target.flandre_damage_lock_effect1 = effectIndex

	local effectIndex = ParticleManager:CreateParticle("particles/units/heroes/hero_bounty_hunter/bounty_hunter_track_shield.vpcf", PATTACH_OVERHEAD_FOLLOW, target) 
	target.flandre_damage_lock_effect2 = effectIndex

	target.flandre_damage_lock = true
end

function OnDestroyFlandreLock(keys)
	local target = keys.target
	ParticleManager:DestroyParticleSystem(target.flandre_damage_lock_effect1,true)
	ParticleManager:DestroyParticleSystem(target.flandre_damage_lock_effect2,true)
	target.flandre_damage_lock = false
end

function OnFlandre04SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local ability = keys.ability

	if caster:IsPower999() then		
		local bonusCastCount = 4	
		caster:SetContextThink(DoUniqueString("thtd_flandre_04_power999"), 
			function()
				if GameRules:IsGamePaused() then return 0.03 end				
				if bonusCastCount <= 0 then return nil end								
				if THTD_IsValid(caster) and THTD_IsValid(target) then								
					Flandre04SpellCast(caster, target, ability)	
					bonusCastCount = bonusCastCount - 1
					return 0.25					
				else					
					return nil 
				end				
			end, 
		0)
	else 
		Flandre04SpellCast(caster, target, ability)
	end
end

function Flandre04SpellCast(caster, target, ability)	
	-- target:StartGesture(ACT_DOTA_DIE)
	caster:EmitSound("Hero_DoomBringer.LvlDeath")	
	
	local damage = caster:THTD_GetAbilityPowerDamage(ability) * 4
	local crit = 1
	local ability3 = caster:FindAbilityByName("thtd_flandre_03")
	if ability3:GetLevel() > 0 then				
		crit = 1 + (100 - target:GetHealthPercent()) * ability3:GetSpecialValueFor("damage_up") / 100
	end

	local DamageTable = {
		ability = ability,
        victim = target, 
        attacker = caster, 
        damage = damage * crit, 
        damage_type = ability:GetAbilityDamageType(), 
        damage_flags = DOTA_DAMAGE_FLAG_NONE
   	}	
	UnitDamageTarget(DamageTable)
	OnFlandre02SpellStart(caster)	
end

