class ShardCollectedInventoryCallback extends InventoryScriptCallback
{
	var m_notificationQueue : weak< JournalNotificationQueue >;
	var m_journalManager : weak< JournalManager >;

	public export override function OnItemQuantityChanged( item : ItemID, diff : Int32, total : Uint32, flaggedAsSilent : Bool )
	{
		var entryString : String;
		var journalEntry : weak< JournalOnscreen >;
		var journalHash : Int32;
		var effect : weak< TriggerHackingMinigameEffector_Record >;
		if( diff < 1 )
		{
			return;
		}
		else if( RPGManager.GetItemType( item ) == gamedataItemType.Gen_Readable )
		{
			entryString = ReadAction.GetJournalEntryFromAction( ItemActionsHelper.GetReadAction( item ).GetID() );
			journalEntry = ( ( JournalOnscreen )( m_journalManager.GetEntryByString( entryString, "gameJournalOnscreen" ) ) );
			journalHash = m_journalManager.GetEntryHash( journalEntry );
			if( m_journalManager.IsAttachedToAnyActiveQuest( journalHash ) )
			{
				OpenShardPopup( journalEntry, item, false );
			}
			else
			{
				m_notificationQueue.PushNotification( journalEntry );
			}
		}
		else if( ItemActionsHelper.GetCrackAction( item ) )
		{
			effect = ( ( TriggerHackingMinigameEffector_Record )( ( ( CrackAction_Record )( ItemActionsHelper.GetCrackAction( item ) ) ).Effector() ) );
			if( effect )
			{
				entryString = effect.JournalEntry();
				if( IsStringValid( entryString ) )
				{
					journalEntry = ( ( JournalOnscreen )( m_journalManager.GetEntryByString( entryString, "gameJournalOnscreen" ) ) );
					journalHash = m_journalManager.GetEntryHash( journalEntry );
					if( m_journalManager.IsAttachedToAnyActiveQuest( journalHash ) )
					{
						OpenShardPopup( journalEntry, item, true );
					}
					else
					{
						m_notificationQueue.PushCrackableNotification( item, journalEntry );
					}
				}
			}
		}
	}

	private function OpenShardPopup( entry : JournalOnscreen, item : ItemID, isCrypted : Bool )
	{
		var evt : NotifyShardRead;
		evt = new NotifyShardRead;
		evt.title = GetLocalizedText( entry.GetTitle() );
		evt.text = entry.GetDescription();
		evt.entry = entry;
		evt.isCrypted = isCrypted;
		evt.itemID = item;
		evt.m_imageId = entry.GetIconID();
		m_notificationQueue.QueueBroadcastEvent( evt );
	}

}

class ShardCollectedNotificationViewData extends GenericNotificationViewData
{
	var entry : JournalOnscreen;
	var isCrypted : Bool;
	var itemID : ItemID;
	var shardTitle : String;
	var m_imageId : TweakDBID;

	public override function CanMerge( data : GenericNotificationViewData ) : Bool
	{
		var compareTo : ShardCollectedNotificationViewData;
		compareTo = ( ( ShardCollectedNotificationViewData )( data ) );
		return compareTo.shardTitle == shardTitle;
	}

}

class ShardCollectedNotification extends GenericNotificationController
{
	private editable var m_shardTitle : inkTextRef;
	var m_bbListenerId : CallbackHandle;
	private var m_animProxy : inkAnimProxy;

	protected event OnInitialize()
	{
		var playerStateMachineBlackboard : IBlackboard;
		var playerPuppet : weak< PlayerPuppet >;
		super.OnInitialize();
		playerPuppet = ( ( PlayerPuppet )( GameInstance.GetPlayerSystem( GetPlayerControlledObject().GetGame() ).GetLocalPlayerControlledGameObject() ) );
		playerStateMachineBlackboard = GameInstance.GetBlackboardSystem( GetPlayerControlledObject().GetGame() ).GetLocalInstanced( playerPuppet.GetEntityID(), GetAllBlackboardDefs().PlayerStateMachine );
		m_bbListenerId = playerStateMachineBlackboard.RegisterDelayedListenerBool( GetAllBlackboardDefs().PlayerStateMachine.IsInteractingViaPersonalLink, this, 'OnInteractionUpdate', true );
		RegisterToCallback( 'OnNotificationPaused', this, 'OnNotificationPaused' );
		RegisterToCallback( 'OnNotificationResumed', this, 'OnNotificationResumed' );
	}

	protected event OnUninitialize()
	{
		var playerStateMachineBlackboard : IBlackboard;
		var playerPuppet : weak< PlayerPuppet >;
		playerPuppet = ( ( PlayerPuppet )( GameInstance.GetPlayerSystem( GetPlayerControlledObject().GetGame() ).GetLocalPlayerControlledGameObject() ) );
		playerStateMachineBlackboard = GameInstance.GetBlackboardSystem( GetPlayerControlledObject().GetGame() ).GetLocalInstanced( playerPuppet.GetEntityID(), GetAllBlackboardDefs().PlayerStateMachine );
		playerStateMachineBlackboard.UnregisterListenerBool( GetAllBlackboardDefs().PlayerStateMachine.IsInteractingViaPersonalLink, m_bbListenerId );
		UnregisterFromCallback( 'OnNotificationPaused', this, 'OnNotificationPaused' );
		UnregisterFromCallback( 'OnNotificationResumed', this, 'OnNotificationResumed' );
		super.OnUninitialize();
	}

	protected event OnInteractionUpdate( value : Bool )
	{
		m_blockAction = value;
		inkWidgetRef.SetVisible( m_actionRef, !( m_blockAction ) );
	}

	public override function SetNotificationData( notificationData : GenericNotificationViewData )
	{
		var data : ShardCollectedNotificationViewData;
		data = ( ( ShardCollectedNotificationViewData )( notificationData ) );
		inkTextRef.SetText( m_shardTitle, WrapText( data.shardTitle ) );
		if( m_animProxy )
		{
			m_animProxy.Stop();
			m_animProxy = NULL;
		}
		m_animProxy = PlayLibraryAnimation( 'notification_shard' );
		super.SetNotificationData( notificationData );
	}

	protected event OnNotificationPaused()
	{
		if( m_animProxy )
		{
			m_animProxy.Pause();
		}
		super.OnNotificationPaused();
	}

	protected event OnNotificationResumed()
	{
		if( m_animProxy )
		{
			m_animProxy.Resume();
		}
		super.OnNotificationResumed();
	}

	private function WrapText( title : String ) : String
	{
		var maxTextSize : Int32;
		maxTextSize = 85;
		if( StrLen( title ) > maxTextSize )
		{
			title = StrLeft( title, maxTextSize );
			title = StrLeft( title, StrFindLast( title, " " ) );
			title += "(...)";
		}
		return title;
	}

}

