class CodexEntryViewController extends inkLogicController
{
	private editable var m_titleText : inkTextRef;
	private editable var m_descriptionText : inkTextRef;
	private editable var m_imageWidget : inkImageRef;
	private editable var m_imageWidgetFallback : inkWidgetRef;
	private editable var m_imageWidgetWrapper : inkWidgetRef;
	private editable var m_expansionWidget : inkWidgetRef;
	private editable var m_scrollWidget : inkWidgetRef;
	private editable var m_contentWrapper : inkWidgetRef;
	private editable var m_noEntrySelectedWidget : inkWidgetRef;
	private var m_data : GenericCodexEntryData;
	private var m_scroll : weak< inkScrollController >;

	protected event OnInitialize()
	{
		m_scroll = ( ( inkScrollController )( inkWidgetRef.GetControllerByType( m_scrollWidget, 'inkScrollController' ) ) );
		inkWidgetRef.SetVisible( m_noEntrySelectedWidget, true );
		inkWidgetRef.SetVisible( m_contentWrapper, false );
		inkWidgetRef.SetVisible( m_imageWidgetFallback, false );
	}

	protected event OnIconCallback( evt : iconAtlasCallbackData )
	{
		if( evt.loadResult != inkIconResult.Success )
		{
			inkImageRef.SetVisible( m_imageWidget, false );
			inkWidgetRef.SetVisible( m_imageWidgetWrapper, false );
		}
		else
		{
			inkImageRef.SetVisible( m_imageWidget, true );
			inkWidgetRef.SetVisible( m_imageWidgetWrapper, true );
		}
	}

	public function ShowEntry( data : GenericCodexEntryData, inputDevice : InputDevice, inputScheme : InputScheme )
	{
		var iconRecord : UIIcon_Record;
		m_data = data;
		m_scroll.SetScrollPosition( 0.0 );
		if( inkTextRef.IsValid( m_titleText ) )
		{
			inkTextRef.SetText( m_titleText, data.m_title );
		}
		UpdateDescription( inputDevice, inputScheme );
		if( inkImageRef.IsValid( m_imageWidget ) )
		{
			if( TDBID.IsValid( m_data.m_imageId ) )
			{
				iconRecord = TweakDBInterface.GetUIIconRecord( m_data.m_imageId );
				inkImageRef.SetVisible( m_imageWidget, true );
				inkImageRef.SetAtlasResource( m_imageWidget, iconRecord.AtlasResourcePath() );
				inkImageRef.SetTexturePart( m_imageWidget, iconRecord.AtlasPartName() );
				inkWidgetRef.SetVisible( m_imageWidgetWrapper, true );
			}
			else
			{
				inkImageRef.SetVisible( m_imageWidget, false );
				inkWidgetRef.SetVisible( m_imageWidgetWrapper, false );
			}
		}
		inkWidgetRef.SetVisible( m_noEntrySelectedWidget, false );
		inkWidgetRef.SetVisible( m_contentWrapper, true );
		inkWidgetRef.SetVisible( m_expansionWidget, data.m_isEp1 );
	}

	public function Refresh( inputDevice : InputDevice, inputScheme : InputScheme )
	{
		UpdateDescription( inputDevice, inputScheme );
	}

	private function UpdateDescription( inputDevice : InputDevice, inputScheme : InputScheme )
	{
		var isEntryDescriptionOverridden : Bool;
		var i : Int32;
		if( inkTextRef.IsValid( m_descriptionText ) )
		{
			for( i = 0; i < m_data.m_journalEntryOverrideDataList.Size(); i += 1 )
			{
				if( m_data.m_journalEntryOverrideDataList[ i ].inputDevice == inputDevice && ( m_data.m_journalEntryOverrideDataList[ i ].inputDevice == InputDevice.KBM || m_data.m_journalEntryOverrideDataList[ i ].inputScheme == inputScheme ) )
				{
					inkTextRef.SetText( m_descriptionText, m_data.m_journalEntryOverrideDataList[ i ].GetOverriddenLocalizedText() );
					isEntryDescriptionOverridden = true;
					break;
				}
			}
			if( !( isEntryDescriptionOverridden ) )
			{
				inkTextRef.SetText( m_descriptionText, m_data.m_description );
			}
		}
	}

}

