class BloodswellCallback extends DelayCallback
{
	private var bloodswellEffector : BloodswellEffector;

	public static function Construct( eff : BloodswellEffector ) : BloodswellCallback
	{
		var callback : BloodswellCallback;
		callback = new BloodswellCallback;
		callback.bloodswellEffector = eff;
		return callback;
	}

}

class BloodswellEffectorHealthListener extends ScriptStatPoolsListener
{
	var m_effector : BloodswellEffector;

	protected event OnStatPoolCustomLimitReached( value : Float )
	{
		m_effector.OnDeath();
	}

}

class BloodswellEffectorColdBloodListener extends ScriptStatsListener
{
	var m_effector : BloodswellEffector;
	var m_gameInstance : GameInstance;

	public export override function OnStatChanged( ownerID : StatsObjectID, statType : gamedataStatType, diff : Float, total : Float )
	{
		if( statType == gamedataStatType.ColdBloodBuffBonus )
		{
			if( total > 0.0 )
			{
				m_effector.ColdbloodAcquired();
			}
			else if( total == 0.0 )
			{
				m_effector.ColdBloodSpend();
			}
		}
	}

}

class BloodswellEffector extends Effector
{
	private var m_deathListener : BloodswellEffectorHealthListener;
	private var m_coldBloodListener : BloodswellEffectorColdBloodListener;
	private var m_gameInstance : GameInstance;
	private var m_owner : weak< GameObject >;
	private var m_isImmortal : Bool;

	protected override function Initialize( record : TweakDBID, game : GameInstance, parentRecord : TweakDBID )
	{
		m_gameInstance = game;
		m_deathListener = new BloodswellEffectorHealthListener;
		m_deathListener.m_effector = this;
		m_coldBloodListener = new BloodswellEffectorColdBloodListener;
		m_coldBloodListener.m_effector = this;
	}

	protected override function ActionOn( owner : GameObject )
	{
		var statPoolsSystem : StatPoolsSystem;
		var statsSystem : StatsSystem;
		statPoolsSystem = GameInstance.GetStatPoolsSystem( m_gameInstance );
		statsSystem = GameInstance.GetStatsSystem( m_gameInstance );
		m_owner = owner;
		m_coldBloodListener.m_gameInstance = m_gameInstance;
		statPoolsSystem.RequestRegisteringListener( m_owner.GetEntityID(), gamedataStatPoolType.Health, m_deathListener );
		statsSystem.RegisterListener( m_owner.GetEntityID(), m_coldBloodListener );
	}

	protected override function Uninitialize( game : GameInstance )
	{
		var statPoolsSystem : StatPoolsSystem;
		var statsSystem : StatsSystem;
		statPoolsSystem = GameInstance.GetStatPoolsSystem( m_gameInstance );
		statsSystem = GameInstance.GetStatsSystem( m_gameInstance );
		if( m_isImmortal )
		{
			GameInstance.GetStatPoolsSystem( m_gameInstance ).RequestSettingStatPoolValueCustomLimit( m_owner.GetEntityID(), gamedataStatPoolType.Health, 0.0, NULL );
			m_isImmortal = false;
		}
		statPoolsSystem.RequestUnregisteringListener( m_owner.GetEntityID(), gamedataStatPoolType.Health, m_deathListener );
		statsSystem.UnregisterListener( m_owner.GetEntityID(), m_coldBloodListener );
	}

	public function ColdbloodAcquired()
	{
		if( !( m_isImmortal ) )
		{
			GameInstance.GetStatPoolsSystem( m_gameInstance ).RequestSettingStatPoolValueCustomLimit( m_owner.GetEntityID(), gamedataStatPoolType.Health, 0.1, NULL );
			m_isImmortal = true;
		}
	}

	public function ColdBloodSpend()
	{
		if( m_isImmortal )
		{
			GameInstance.GetStatPoolsSystem( m_gameInstance ).RequestSettingStatPoolValueCustomLimit( m_owner.GetEntityID(), gamedataStatPoolType.Health, 0.0, NULL );
			m_isImmortal = false;
		}
	}

	public function OnDeath()
	{
		var statSys : StatsSystem;
		var maxHealth : Float;
		var healthToHeal : Float;
		var coldbloodStacks : Float;
		statSys = GameInstance.GetStatsSystem( m_gameInstance );
		coldbloodStacks = statSys.GetStatValue( m_owner.GetEntityID(), gamedataStatType.ColdBloodBuffBonus );
		if( coldbloodStacks == 0.0 )
		{
			return;
		}
		maxHealth = statSys.GetStatValue( m_owner.GetEntityID(), gamedataStatType.Health );
		healthToHeal = ( maxHealth * 0.1 ) * coldbloodStacks;
		GameInstance.GetStatPoolsSystem( m_gameInstance ).RequestChangingStatPoolValue( m_owner.GetEntityID(), gamedataStatPoolType.Health, healthToHeal, m_owner, true, false );
		GameInstance.GetStatusEffectSystem( m_gameInstance ).RemoveStatusEffect( m_owner.GetEntityID(), T"BaseStatusEffect.ColdBlood" );
		GameInstance.GetStatusEffectSystem( m_gameInstance ).ApplyStatusEffect( m_owner.GetEntityID(), T"BaseStatusEffect.PlayerInvulnerable" );
		GameInstance.GetStatusEffectSystem( m_gameInstance ).ApplyStatusEffect( m_owner.GetEntityID(), T"BaseStatusEffect.BloodswellPerkCooldown" );
	}

}

