
THTD_KEINE_02_HUMEN = 1
THTD_KEINE_02_SHIRASAWA = 2

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

	if caster.thtd_keine_change == nil then
		caster.thtd_keine_change = THTD_KEINE_02_HUMEN
	end

	if caster.thtd_keine_change == THTD_KEINE_02_HUMEN then
		if target:THTD_IsTower() then
			caster.thtd_last_cast_unit = target			

			if target:HasModifier("modifier_keine_01_buff") then
				target:FindModifierByName("modifier_keine_01_buff"):SetDuration(keys.bonus_time, false)
			else
				local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_keine/ability_keine_01_buff.vpcf", PATTACH_CUSTOMORIGIN, target) 
				ParticleManager:SetParticleControlEnt(effectIndex , 0, target, 5, "follow_origin", Vector(0,0,0), true)
				keys.ability:ApplyDataDrivenModifier(caster,target, "modifier_keine_01_buff", {duration = keys.bonus_time})				
			end
		end
	end
end

function OnCreatedKeine01Buff(keys)
	keys.target:THTD_AddBaseAttack(keys.attack_bonus, "thtd_keine_01_bonus")
end

function OnDestroyKeine01Buff(keys)
	keys.target:THTD_AddBaseAttack("thtd_keine_01_bonus")
end

function OnKeine01AttackLanded(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local targetPoint = target:GetOrigin()
	
	if caster.thtd_keine_change == THTD_KEINE_02_SHIRASAWA then	
		if RollPercentage(keys.chance) then
			local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_keine/ability_keine_01_stun.vpcf", PATTACH_CUSTOMORIGIN, caster)
			ParticleManager:SetParticleControl(effectIndex, 0, targetPoint)
			ParticleManager:SetParticleControl(effectIndex, 1, Vector(keys.range,keys.range,keys.range))
			ParticleManager:DestroyParticleSystem(effectIndex,false)

			local damage = caster:THTD_GetAbilityPowerDamage(keys.ability)			
			local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.range)
			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)
				   
				if not v:HasModifier("modifier_keine_01_lock") then 
					keys.ability:ApplyDataDrivenModifier(caster, v, "modifier_keine_01_lock", {Duration = keys.lock_time})	
					UnitStunTarget(caster,v,keys.stun_time)		   			
		   		end
			end
		end
	end
end

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

	if caster.thtd_keine_change == nil then
		caster.thtd_keine_change = THTD_KEINE_02_HUMEN
	end

	if caster.thtd_keine_change == THTD_KEINE_02_HUMEN then
		caster.thtd_keine_change = THTD_KEINE_02_SHIRASAWA
		caster:SetModel("models/thd_hero/keine/keine2/keine2.vmdl")
		caster:SetOriginalModel("models/thd_hero/keine/keine2/keine2.vmdl")
		if caster.thtd_close_ai == true then
			CustomGameEventManager:Send_ServerToPlayer( caster:GetPlayerOwner() , "show_message", {msg="change_to_keine_shirasawa", duration=5, params={}, color="#0ff"} )
		end
	elseif caster.thtd_keine_change == THTD_KEINE_02_SHIRASAWA then
		caster.thtd_keine_change = THTD_KEINE_02_HUMEN
		caster:SetModel("models/thd_hero/keine/keine.vmdl")
		caster:SetOriginalModel("models/thd_hero/keine/keine.vmdl")
		if caster.thtd_close_ai == true then
			CustomGameEventManager:Send_ServerToPlayer( caster:GetPlayerOwner() , "show_message", {msg="change_to_keine_humen", duration=5, params={}, color="#0ff"} )
		end
	end
end

function OnKeine03SpellStart(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local targetPoint = keys.target_points[1]
	local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.range1)

	if caster.thtd_keine_change == nil then
		caster.thtd_keine_change = THTD_KEINE_02_HUMEN
	end

	if caster.thtd_keine_change == THTD_KEINE_02_HUMEN then
		for k,v in pairs(targets) do
			Keine03MarkUnit(v)
			local modifier = v:FindModifierByName("thtd_keine_03_debuff")
			if modifier==nil and v.thtd_keine_mark_count < keys.max_count then
   				keys.ability:ApplyDataDrivenModifier(caster, v, "thtd_keine_03_debuff", nil)
   			end
		end
	elseif caster.thtd_keine_change == THTD_KEINE_02_SHIRASAWA then	
		for k,v in pairs(targets) do
			if v:HasModifier("thtd_keine_03_debuff") then
				v.thtd_keine_mark_count = v.thtd_keine_mark_count + 1
				v:RemoveModifierByName("thtd_keine_03_debuff")
			end
		end
	end
end

function Keine03MarkUnit(unit)
	unit.thtd_keine_mark_count = 0
	unit.thtd_keine_03_origin = unit:GetOrigin()
	unit.thtd_keine_03_mark_health = unit:GetHealth()
	unit.thtd_keine_03_next_move_point = unit.next_move_point
	unit.thtd_keine_03_next_move_forward = unit.next_move_forward	
	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_keine/ability_keine_03.vpcf", PATTACH_CUSTOMORIGIN, unit)
	ParticleManager:SetParticleControl(effectIndex, 0, unit:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 1, unit:GetOrigin())
	ParticleManager:SetParticleControl(effectIndex, 2, unit:GetOrigin())
	ParticleManager:DestroyParticleSystem(effectIndex,false)	
end

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

	if RollPercentage(keys.chance) then
		local targetPoint = target:GetOrigin()
		local targets = THTD_FindUnitsInRadius(caster,targetPoint,keys.range2)
		if caster.thtd_keine_change == THTD_KEINE_02_HUMEN then	
			for k,v in pairs(targets) do
				Keine03MarkUnit(v)
				local modifier = v:FindModifierByName("thtd_keine_03_debuff")
				if modifier==nil and v.thtd_keine_mark_count < keys.max_count then
					keys.ability:ApplyDataDrivenModifier(caster, v, "thtd_keine_03_debuff", nil)
				end
			end			
		else			
			for k,v in pairs(targets) do
				if v:HasModifier("thtd_keine_03_debuff") then
					v.thtd_keine_mark_count = v.thtd_keine_mark_count + 1
					v:RemoveModifierByName("thtd_keine_03_debuff")
				end
			end
		end
	end
end

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

	if target~=nil and target:IsNull()==false and target:IsAlive() and target.thtd_keine_03_origin ~= nil then	
		local distance = GetDistanceBetweenTwoVec2D(target:GetOrigin(), target.thtd_keine_03_origin)
		
		local effectIndex = ParticleManager:CreateParticle("particles/bosses/thtd_keine/ability_bosses_keine.vpcf", PATTACH_CUSTOMORIGIN, caster)
		ParticleManager:SetParticleControl(effectIndex, 0, target:GetOrigin())

		FindClearSpaceForUnit(target,target.thtd_keine_03_origin,false)

		ParticleManager:SetParticleControl(effectIndex, 2, target:GetOrigin())
		ParticleManager:DestroyParticleSystem(effectIndex,false)

   		target.next_move_point = target.thtd_keine_03_next_move_point
		target.next_move_forward = target.thtd_keine_03_next_move_forward
		target.thtd_keine_03_origin = nil

		local currentHealth = target:GetHealth()
		local decrease_health = target.thtd_keine_03_mark_health - currentHealth

		target:SetHealth(target.thtd_keine_03_mark_health)

		local ability = caster:FindAbilityByName("thtd_keine_04")
		if ability:GetLevel() >= 1 then 
			if caster.thtd_keine_04_change == THTD_KEINE_04_SWORD then
				OnKeine04Sword(keys)
		   elseif caster.thtd_keine_04_change == THTD_KEINE_04_JADE then		
			   OnKeine04Jade(keys,distance)
		   elseif caster.thtd_keine_04_change == THTD_KEINE_04_MIRROR then
			   OnKeine04Mirror(keys,decrease_health,currentHealth)		
		   end
		end		
	end
end

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

	local info = 
	{
		Target = keys.target,
		Source = caster,
		Ability = keys.ability,	
		EffectName = "particles/heroes/thtd_keine/ability_keine_04_sword.vpcf",
        iMoveSpeed = 1400,
		vSourceLoc= caster:GetAbsOrigin(),                -- 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,                           -- Optional
		iVisionRadius = 400,                              -- Optional
		iVisionTeamNumber = caster:GetTeamNumber(),        -- Optional
	}
	local projectile = ProjectileManager:CreateTrackingProjectile(info)
	ParticleManager:DestroyLinearProjectileSystem(projectile,false)
end

function OnKeine04SwordHit(keys)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local ability = caster:FindAbilityByName("thtd_keine_04")
	local damage = caster:THTD_GetAbilityPowerDamage(ability)

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

function OnKeine04Jade(keys,distance)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	local damage = caster:THTD_GetStarDamage() * (distance/100) * caster:GetAbilityValue("thtd_keine_04", "distance_damage")

	local DamageTable = {
			ability = caster:FindAbilityByName("thtd_keine_04"),
	        victim = target, 
	        attacker = caster, 
	        damage = damage, 
	        damage_type = keys.ability:GetAbilityDamageType(), 
	        damage_flags = DOTA_DAMAGE_FLAG_NONE
   	}
   	UnitDamageTarget(DamageTable)
end

function OnKeine04Mirror(keys,decrease_health,currentHealth)
	local caster = EntIndexToHScript(keys.caster_entindex)
	local target = keys.target
	target:SetHealth(currentHealth + decrease_health * math.max(0, (1 - caster:GetAbilityValue("thtd_keine_04", "hp_percent"))))

	local effectIndex = ParticleManager:CreateParticle("particles/heroes/thtd_keine/ability_keine_04_mirror.vpcf", PATTACH_CUSTOMORIGIN, caster)
	ParticleManager:SetParticleControl(effectIndex, 0, target:GetOrigin())
	ParticleManager:DestroyParticleSystem(effectIndex,false)
end

THTD_KEINE_04_SWORD = 1
THTD_KEINE_04_JADE = 2
THTD_KEINE_04_MIRROR = 3

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

	if caster.thtd_keine_04_change == nil then
		caster.thtd_keine_04_change = THTD_KEINE_04_SWORD
	end

	if caster.thtd_keine_04_change == THTD_KEINE_04_SWORD then
		CustomGameEventManager:Send_ServerToPlayer( caster:GetPlayerOwner() , "show_message", {msg="change_to_keine_04_jade", duration=5, params={count=1}, color="#0ff"} )
		caster.thtd_keine_04_change = THTD_KEINE_04_JADE
	elseif caster.thtd_keine_04_change == THTD_KEINE_04_JADE then
		CustomGameEventManager:Send_ServerToPlayer( caster:GetPlayerOwner() , "show_message", {msg="change_to_keine_04_mirror", duration=5, params={count=1}, color="#0ff"} )
		caster.thtd_keine_04_change = THTD_KEINE_04_MIRROR
	elseif caster.thtd_keine_04_change == THTD_KEINE_04_MIRROR then		
		CustomGameEventManager:Send_ServerToPlayer( caster:GetPlayerOwner() , "show_message", {msg="change_to_keine_04_sword", duration=5, params={count=1}, color="#0ff"} )
		caster.thtd_keine_04_change = THTD_KEINE_04_SWORD
	end
end