class ComputerDocumentWidgetController extends DeviceInkLogicControllerBase
{
	[ category = "Widget Refs" ]
	protected editable var m_titleWidget : inkTextRef;
	[ category = "Widget Refs" ]
	protected editable var m_ownerNameWidget : inkTextRef;
	[ category = "Widget Refs" ]
	protected editable var m_dateWidget : inkTextRef;
	[ category = "Widget Refs" ]
	protected editable var m_datePanelWidget : inkTextRef;
	[ category = "Widget Refs" ]
	protected editable var m_ownerPanelWidget : inkTextRef;
	[ category = "Widget Refs" ]
	protected editable var m_textContentWidget : inkTextRef;
	[ category = "Widget Refs" ]
	protected editable var m_textContentHolder : inkWidgetRef;
	[ category = "Widget Refs" ]
	protected editable var m_videoContentWidget : inkVideoRef;
	[ category = "Widget Refs" ]
	protected editable var m_imageContentWidget : inkImageRef;
	[ category = "Widget Refs" ]
	protected editable var m_closeButtonWidget : inkWidgetRef;
	protected var m_documentType : EDocumentType;
	private var m_lastPlayedVideo : ResRef;

	public virtual function Initialize( gameController : ComputerInkGameController, const widgetData : ref< SDocumentWidgetPackage > )
	{
		inkTextRef.SetText( m_titleWidget, widgetData.title );
		m_documentType = widgetData.documentType;
		if( IsStringValid( widgetData.owner ) )
		{
			inkTextRef.SetText( m_ownerNameWidget, widgetData.owner );
			if( m_documentType == EDocumentType.MAIL )
			{
				inkTextRef.SetLocalizedTextScript( m_ownerPanelWidget, "LocKey#13237" );
			}
			else
			{
				inkTextRef.SetLocalizedTextScript( m_ownerPanelWidget, "LocKey#13244" );
			}
		}
		else
		{
			inkTextRef.SetVisible( m_ownerNameWidget, false );
			inkTextRef.SetVisible( m_ownerPanelWidget, false );
		}
		if( IsStringValid( widgetData.date ) )
		{
			inkTextRef.SetText( m_dateWidget, widgetData.date );
			if( m_documentType == EDocumentType.MAIL )
			{
				inkTextRef.SetLocalizedTextScript( m_datePanelWidget, "LocKey#13239" );
			}
			else
			{
				inkTextRef.SetLocalizedTextScript( m_datePanelWidget, "LocKey#13236" );
			}
		}
		else
		{
			inkTextRef.SetVisible( m_dateWidget, false );
			inkTextRef.SetVisible( m_datePanelWidget, false );
		}
		ResolveContent( widgetData );
		RegisterCloseButtonCallback( gameController );
		m_isInitialized = true;
	}

	private function ResolveContent( const widgetData : ref< SDocumentWidgetPackage > )
	{
		var imageRecord : weak< UIIcon_Record >;
		if( m_lastPlayedVideo == widgetData.videoPath && inkVideoRef.IsPlayingVideo( m_videoContentWidget ) )
		{
			inkVideoRef.SetVisible( m_videoContentWidget, true );
			return;
		}
		StopVideo();
		if( ( inkImageRef.Get( m_imageContentWidget ) != NULL ) && TDBID.IsValid( widgetData.image ) )
		{
			imageRecord = TweakDBInterface.GetUIIconRecord( widgetData.image );
			if( imageRecord )
			{
				inkTextRef.SetVisible( m_textContentWidget, false );
				inkWidgetRef.SetVisible( m_textContentHolder, false );
				inkImageRef.SetVisible( m_imageContentWidget, true );
				SetTexture( m_imageContentWidget, widgetData.image );
			}
		}
		else if( ResRef.IsValid( widgetData.videoPath ) )
		{
			inkImageRef.SetVisible( m_imageContentWidget, false );
			inkTextRef.SetVisible( m_textContentWidget, false );
			inkWidgetRef.SetVisible( m_textContentHolder, false );
			inkTextRef.SetText( m_textContentWidget, "" );
			inkVideoRef.SetVideoPath( m_videoContentWidget, widgetData.videoPath );
			inkVideoRef.SetVisible( m_videoContentWidget, true );
			inkVideoRef.Play( m_videoContentWidget );
			inkVideoRef.SetLoop( m_videoContentWidget, true );
			m_lastPlayedVideo = widgetData.videoPath;
		}
		else
		{
			inkImageRef.SetVisible( m_imageContentWidget, false );
			inkWidgetRef.SetVisible( m_textContentHolder, true );
			inkTextRef.SetVisible( m_textContentWidget, true );
			inkTextRef.SetText( m_textContentWidget, widgetData.content );
		}
	}

	public function StopVideo()
	{
		inkVideoRef.Stop( m_videoContentWidget );
		inkVideoRef.SetVisible( m_videoContentWidget, false );
	}

	protected function RegisterCloseButtonCallback( gameController : DeviceInkGameControllerBase )
	{
		var controller : DeviceButtonLogicControllerBase;
		if( !( m_isInitialized ) )
		{
			if( inkWidgetRef.Get( m_closeButtonWidget ) )
			{
				if( m_documentType == EDocumentType.MAIL )
				{
					inkWidgetRef.RegisterToCallback( m_closeButtonWidget, 'OnRelease', gameController, 'OnHideMailCallback' );
				}
				else
				{
					inkWidgetRef.RegisterToCallback( m_closeButtonWidget, 'OnRelease', gameController, 'OnHideFileCallback' );
				}
				controller = ( ( DeviceButtonLogicControllerBase )( inkWidgetRef.GetController( m_closeButtonWidget ) ) );
				if( controller )
				{
					controller.RegisterAudioCallbacks( gameController );
				}
			}
		}
	}

	public function GetDocumentType() : EDocumentType
	{
		return m_documentType;
	}

}

