class EvaluateMinigame extends Event
{
	var minigameBB : weak< IBlackboard >;
	var reward : TweakDBID;
	var journalEntry : String;
	var fact : CName;
	var factValue : Int32;
	var item : ItemID;
	var showPopup : Bool;
	var returnToJournal : Bool;
}

class TriggerHackingMinigameEffector extends Effector
{
	var m_owner : weak< GameObject >;
	var m_listener : CallbackHandle;
	var m_item : ItemID;
	var m_reward : TweakDBID;
	var m_journalEntry : String;
	var m_fact : CName;
	var m_factValue : Int32;
	var m_showPopup : Bool;
	var m_returnToJournal : Bool;

	protected override function Initialize( record : TweakDBID, game : GameInstance, parentRecord : TweakDBID )
	{
		m_item = ItemID.FromTDBID( parentRecord );
		m_reward = TweakDBInterface.GetTriggerHackingMinigameEffectorRecord( record ).Reward().GetID();
		m_journalEntry = TweakDBInterface.GetString( record + T".journalEntry", "" );
		m_fact = TweakDBInterface.GetCName( record + T".factName", '' );
		m_factValue = TweakDBInterface.GetInt( record + T".factValue", 0 );
		m_showPopup = TweakDBInterface.GetBool( record + T".showPopup", false );
	}

	protected override function Uninitialize( game : GameInstance )
	{
		var bb : IBlackboard;
		bb = GameInstance.GetBlackboardSystem( game ).Get( GetAllBlackboardDefs().NetworkBlackboard );
		bb.SetString( GetAllBlackboardDefs().NetworkBlackboard.NetworkName, "" );
		bb.SetBool( GetAllBlackboardDefs().NetworkBlackboard.ItemBreach, false );
	}

	protected override function ActionOn( owner : GameObject )
	{
		var bb : IBlackboard;
		m_owner = owner;
		bb = GameInstance.GetBlackboardSystem( owner.GetGame() ).Get( GetAllBlackboardDefs().NetworkBlackboard );
		bb.SetString( GetAllBlackboardDefs().NetworkBlackboard.NetworkName, ( ( String )( TDBID.ToNumber( ItemID.GetTDBID( m_item ) ) ) ) );
		bb.SetVariant( GetAllBlackboardDefs().NetworkBlackboard.NetworkTDBID, ItemID.GetTDBID( m_item ) );
		bb.SetBool( GetAllBlackboardDefs().NetworkBlackboard.ItemBreach, true );
		m_returnToJournal = GameInstance.GetBlackboardSystem( owner.GetGame() ).Get( GetAllBlackboardDefs().HackingMinigame ).GetBool( GetAllBlackboardDefs().HackingMinigame.IsJournalTarget );
		m_listener = GameInstance.GetBlackboardSystem( owner.GetGame() ).Get( GetAllBlackboardDefs().HackingMinigame ).RegisterListenerInt( GetAllBlackboardDefs().HackingMinigame.State, this, 'OnItemCracked' );
	}

	protected event OnItemCracked( value : Int32 )
	{
		var evalMinigame : EvaluateMinigame;
		if( HackingMinigameState.InProgress != ( ( HackingMinigameState )( value ) ) )
		{
			if( HackingMinigameState.Succeeded == ( ( HackingMinigameState )( value ) ) )
			{
				evalMinigame = new EvaluateMinigame;
				evalMinigame.minigameBB = GameInstance.GetBlackboardSystem( m_owner.GetGame() ).Get( GetAllBlackboardDefs().HackingMinigame );
				evalMinigame.reward = m_reward;
				evalMinigame.journalEntry = m_journalEntry;
				evalMinigame.fact = m_fact;
				evalMinigame.factValue = m_factValue;
				evalMinigame.item = m_item;
				evalMinigame.showPopup = m_showPopup;
				evalMinigame.returnToJournal = m_returnToJournal;
				m_owner.QueueEvent( evalMinigame );
			}
			else if( HackingMinigameState.Failed == ( ( HackingMinigameState )( value ) ) )
			{
				( ( ScriptedPuppet )( m_owner ) ).SetItemMinigameAttempted( m_item );
			}
			GameInstance.GetEffectorSystem( m_owner.GetGame() ).RemoveEffector( m_owner.GetEntityID(), GetRecord() );
		}
	}

}

