class PerkPrereqState extends PrereqState
{
}

class PerkPrereq extends IScriptablePrereq
{
	var m_invert : Bool;
	var m_perk : gamedataPerkType;

	protected override function Initialize( recordID : TweakDBID )
	{
		var str : String;
		str = TDB.GetString( recordID + T".perk", "" );
		m_invert = TDB.GetBool( recordID + T".invert" );
		m_perk = ( ( gamedataPerkType )( ( ( Int32 )( EnumValueFromString( "gamedataPerkType", str ) ) ) ) );
	}

	public const override function IsFulfilled( game : GameInstance, context : IScriptable ) : Bool
	{
		var owner : GameObject;
		var result : Bool;
		owner = ( ( GameObject )( context ) );
		result = PlayerDevelopmentSystem.GetData( owner ).HasPerk( m_perk );
		if( m_invert )
		{
			return !( result );
		}
		return result;
	}

}

class IsNewPerkBoughtPrereqState extends PrereqState
{
}

class IsNewPerkBoughtPrereq extends IScriptablePrereq
{
	var m_invert : Bool;
	var m_perkType : gamedataNewPerkType;
	var m_level : Int32;

	protected override function Initialize( recordID : TweakDBID )
	{
		var str : String;
		str = TweakDBInterface.GetString( recordID + T".perkType", "" );
		m_perkType = ( ( gamedataNewPerkType )( ( ( Int32 )( EnumValueFromString( "gamedataNewPerkType", str ) ) ) ) );
		m_invert = TDB.GetBool( recordID + T".invert" );
		m_level = TweakDBInterface.GetInt( recordID + T".level", 0 );
	}

	public const override function IsFulfilled( game : GameInstance, context : IScriptable ) : Bool
	{
		var owner : GameObject;
		var result : Bool;
		owner = ( ( GameObject )( context ) );
		result = PlayerDevelopmentSystem.GetData( owner ).IsNewPerkBought( m_perkType ) == m_level;
		if( m_invert )
		{
			return !( result );
		}
		return result;
	}

}

class PlayerIsNewPerkBoughtPrereqState extends PrereqState
{
	var m_listenerPerksVariant : CallbackHandle;
	var m_owner : weak< GameObject >;

	protected event OnPerkStateUpdate( value : Variant )
	{
		var checkPassed : Bool;
		const var prereq : PlayerIsNewPerkBoughtPrereq;
		prereq = ( ( PlayerIsNewPerkBoughtPrereq )( GetPrereq() ) );
		checkPassed = prereq.IsFulfilled( m_owner.GetGame(), m_owner );
		OnChanged( checkPassed );
	}

}

class PlayerIsNewPerkBoughtPrereq extends IScriptablePrereq
{
	var m_invert : Bool;
	var m_perkType : gamedataNewPerkType;
	var m_level : Int32;

	protected const override function OnRegister( state : PrereqState, game : GameInstance, context : IScriptable ) : Bool
	{
		var bb : IBlackboard;
		var castedState : PlayerIsNewPerkBoughtPrereqState;
		var player : PlayerPuppet;
		castedState = ( ( PlayerIsNewPerkBoughtPrereqState )( state ) );
		castedState.m_owner = ( ( GameObject )( context ) );
		player = ( ( PlayerPuppet )( context ) );
		if( player )
		{
			bb = GameInstance.GetBlackboardSystem( game ).Get( GetAllBlackboardDefs().UI_PlayerStats );
			castedState.m_listenerPerksVariant = bb.RegisterListenerVariant( GetAllBlackboardDefs().UI_PlayerStats.Perks, castedState, 'OnPerkStateUpdate' );
		}
		return false;
	}

	protected const override function OnUnregister( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var bb : IBlackboard;
		var castedState : PlayerIsNewPerkBoughtPrereqState;
		var player : PlayerPuppet;
		player = ( ( PlayerPuppet )( context ) );
		if( player )
		{
			castedState = ( ( PlayerIsNewPerkBoughtPrereqState )( state ) );
			if( castedState.m_listenerPerksVariant )
			{
				bb = GameInstance.GetBlackboardSystem( game ).Get( GetAllBlackboardDefs().UI_PlayerStats );
				bb.UnregisterListenerVariant( GetAllBlackboardDefs().UI_PlayerStats.Perks, castedState.m_listenerPerksVariant );
			}
		}
	}

	protected override function Initialize( recordID : TweakDBID )
	{
		var str : String;
		str = TweakDBInterface.GetString( recordID + T".perkType", "" );
		m_perkType = ( ( gamedataNewPerkType )( ( ( Int32 )( EnumValueFromString( "gamedataNewPerkType", str ) ) ) ) );
		m_invert = TDB.GetBool( recordID + T".invert" );
		m_level = TweakDBInterface.GetInt( recordID + T".level", 0 );
	}

	protected const override function OnApplied( state : PrereqState, game : GameInstance, context : IScriptable )
	{
		var castedState : PlayerIsNewPerkBoughtPrereqState;
		castedState = ( ( PlayerIsNewPerkBoughtPrereqState )( state ) );
		castedState.OnChanged( IsFulfilled( game, context ) );
	}

	public const override function IsFulfilled( game : GameInstance, context : IScriptable ) : Bool
	{
		var player : GameObject;
		var result : Bool;
		player = ( ( GameObject )( GameInstance.GetPlayerSystem( game ).GetLocalPlayerControlledGameObject() ) );
		result = PlayerDevelopmentSystem.GetData( player ).IsNewPerkBought( m_perkType ) >= m_level;
		if( m_invert )
		{
			return !( result );
		}
		return result;
	}

}

