class OpenCodexPopupEvent extends Event
{
	var m_entry : weak< JournalEntry >;
}

class CodexPopupData extends inkGameNotificationData
{
	var m_entry : weak< JournalEntry >;
}

class CodexPopupGameController extends inkGameController
{
	private editable var m_entryViewRef : inkCompoundRef;
	private editable var m_characterEntryViewRef : inkCompoundRef;
	private editable var m_imageViewRef : inkImageRef;
	private var m_entryViewController : weak< CodexEntryViewController >;
	private var m_characterEntryViewController : weak< CodexEntryViewController >;
	private var m_player : weak< GameObject >;
	private var m_journalMgr : weak< JournalManager >;
	private var m_data : CodexPopupData;

	protected event OnInitialize()
	{
		m_player = GetPlayerControlledObject();
		m_journalMgr = GameInstance.GetJournalManager( m_player.GetGame() );
		m_entryViewController = ( ( CodexEntryViewController )( inkCompoundRef.GetController( m_entryViewRef ) ) );
		m_characterEntryViewController = ( ( CodexEntryViewController )( inkCompoundRef.GetController( m_characterEntryViewRef ) ) );
		SetupData();
		PlayLibraryAnimation( 'codex_intro' );
		RegisterToGlobalInputCallback( 'OnPostOnRelease', this, 'OnRelease' );
	}

	protected event OnUninitialize()
	{
		UnregisterFromGlobalInputCallback( 'OnPostOnRelease', this, 'OnRelease' );
	}

	protected event OnRelease( evt : inkPointerEvent )
	{
		if( ( evt.IsAction( 'proceed' ) || evt.IsAction( 'cancel' ) ) || evt.IsAction( 'back' ) )
		{
			PlaySound( 'Button', 'OnPress' );
			m_data.token.TriggerCallback( m_data );
		}
	}

	private function SetupData()
	{
		var currentCategory : weak< JournalCodexCategory >;
		var currentGroup : weak< JournalCodexGroup >;
		var codexEntry : weak< JournalCodexEntry >;
		var stateFilter : JournalRequestStateFilter;
		var newEntries : array< Int32 >;
		var codexEntryData : CodexEntryData;
		var currentType : CodexCategoryType;
		var imageEntry : JournalImageEntry;
		var inputDevice : InputDevice;
		var inputScheme : InputScheme;
		inputDevice = ( ( GetPlayerControlledObject().PlayerLastUsedKBM() ) ? ( InputDevice.KBM ) : ( InputDevice.PAD ) );
		inputScheme = ( ( InputScheme )( ( ( Uint8 )( GetBlackboardSystem().Get( GetAllBlackboardDefs().InputSchemes ).GetUint( GetAllBlackboardDefs().InputSchemes.Scheme ) ) ) ) );
		m_data = ( ( CodexPopupData )( GetRootWidget().GetUserData( 'CodexPopupData' ) ) );
		if( m_data )
		{
			codexEntry = ( ( JournalCodexEntry )( m_data.m_entry ) );
			imageEntry = ( ( JournalImageEntry )( m_data.m_entry ) );
			if( imageEntry )
			{
				inkCompoundRef.SetVisible( m_entryViewRef, false );
				inkCompoundRef.SetVisible( m_characterEntryViewRef, false );
				inkImageRef.SetVisible( m_imageViewRef, true );
				InkImageUtils.RequestSetImage( this, m_imageViewRef, imageEntry.GetImageID() );
			}
			else
			{
				currentGroup = ( ( JournalCodexGroup )( m_journalMgr.GetParentEntry( codexEntry ) ) );
				currentCategory = ( ( JournalCodexCategory )( m_journalMgr.GetParentEntry( currentGroup ) ) );
				currentType = CodexUtils.GetCategoryTypeFromId( currentCategory.GetId() );
				stateFilter.inactive = true;
				stateFilter.active = true;
				stateFilter.succeeded = true;
				stateFilter.failed = true;
				codexEntryData = CodexUtils.ConvertToCodexData( m_journalMgr, codexEntry, 0, stateFilter, newEntries );
				if( currentType == CodexCategoryType.Characters )
				{
					m_characterEntryViewController.ShowEntry( codexEntryData, inputDevice, inputScheme );
					inkCompoundRef.SetVisible( m_entryViewRef, false );
					inkCompoundRef.SetVisible( m_characterEntryViewRef, true );
					inkImageRef.SetVisible( m_imageViewRef, false );
				}
				else
				{
					m_entryViewController.ShowEntry( codexEntryData, inputDevice, inputScheme );
					inkCompoundRef.SetVisible( m_entryViewRef, true );
					inkCompoundRef.SetVisible( m_characterEntryViewRef, false );
					inkImageRef.SetVisible( m_imageViewRef, false );
				}
			}
		}
	}

}

