enum EComputerMenuType
{
	MAIN = 0,
	SYSTEM = 1,
	FILES = 2,
	MAILS = 3,
	NEWSFEED = 4,
	INTERNET = 5,
	INVALID = 6,
}

class RequestComputerMenuWidgetsUpdateEvent extends RequestWidgetUpdateEvent
{
}

class RequestComputerMainMenuWidgetsUpdateEvent extends RequestWidgetUpdateEvent
{
}

class RequestDocumentThumbnailWidgetsUpdateEvent extends RequestWidgetUpdateEvent
{
	var documentType : EDocumentType;
}

class RequestDocumentWidgetUpdateEvent extends RequestWidgetUpdateEvent
{
	var documentType : EDocumentType;
	var documentAdress : SDocumentAdress;
}

class RequestBannerWidgetUpdateEvent extends RequestWidgetUpdateEvent
{
}

enum EDocumentType
{
	FILE = 0,
	MAIL = 1,
	Invalid = 2,
}

struct SDocumentAdress
{
	editable var folderID : Int32;
	default folderID = -1;
	editable var documentID : Int32;
	default documentID = -1;
}

class DocumentCustomData extends IScriptable
{
	var id : Int32;
	var type : EDocumentType;
}

struct SBannerWidgetPackage extends SWidgetPackage
{
	var title : String;
	var description : String;
	var content : ResRef;
}

struct SComputerMenuButtonWidgetPackage extends SWidgetPackage
{
	var counter : Int32;
}

struct SDocumentWidgetPackage extends SWidgetPackage
{
	var owner : String;
	var date : String;
	var title : String;
	var content : String;
	var image : TweakDBID;
	var videoPath : ResRef;
	var isEncrypted : Bool;
	var questInfo : QuestInfo;
	var wasRead : Bool;
	var documentType : EDocumentType;
}

struct SDocumentThumbnailWidgetPackage extends SWidgetPackage
{
	var folderName : String;
	var documentAdress : SDocumentAdress;
	var documentType : EDocumentType;
	var questInfo : QuestInfo;
	var wasRead : Bool;
	var isOpened : Bool;
}

class ComputerInkGameController extends DeviceInkGameControllerBase
{
	[ customEditor = "TweakDBGroupInheritance;WidgetDefinition" ]
	protected editable var m_layoutID : TweakDBID;
	protected var m_currentLayoutLibraryID : CName;
	protected var m_mainLayout : weak< inkWidget >;
	protected var m_devicesMenuInitialized : Bool;
	protected var m_devicesMenuSpawned : Bool;
	protected var m_devicesMenuSpawnRequested : Bool;
	protected var m_menuInitialized : Bool;
	private var m_mainDisplayWidget : weak< inkVideo >;
	private var m_forceOpenDocumentType : EDocumentType;
	default m_forceOpenDocumentType = EDocumentType.Invalid;
	private var m_forceOpenDocumentAdress : SDocumentAdress;
	private var m_onMailThumbnailWidgetsUpdateListener : CallbackHandle;
	private var m_onFileThumbnailWidgetsUpdateListener : CallbackHandle;
	private var m_onMailWidgetsUpdateListener : CallbackHandle;
	private var m_onFileWidgetsUpdateListener : CallbackHandle;
	private var m_onMenuButtonWidgetsUpdateListener : CallbackHandle;
	private var m_onMainMenuButtonWidgetsUpdateListener : CallbackHandle;
	private var m_onBannerWidgetsUpdateListener : CallbackHandle;
	private var m_onGlitchingStateChangedListener : CallbackHandle;

	protected event OnUninitialize()
	{
		super.OnUninitialize();
		if( m_mainDisplayWidget )
		{
			m_mainDisplayWidget.Stop();
		}
	}

	protected override function SetupWidgets()
	{
		if( !( m_isInitialized ) )
		{
			ResolveInitialMenuType();
			m_mainDisplayWidget = ( ( inkVideo )( GetWidget( 'main_display' ) ) );
			m_mainDisplayWidget.SetVisible( false );
			InitializeMainLayout();
		}
	}

	protected virtual function InitializeMainLayout()
	{
		var layoutRecord : WidgetDefinition_Record;
		var screenDef : ScreenDefinitionPackage;
		var newLibraryID : CName;
		var spawnData : AsyncSpawnData;
		if( !( TDBID.IsValid( m_layoutID ) ) )
		{
			m_layoutID = T"DevicesUIDefinitions.ComputerLayoutWidget";
		}
		screenDef = GetScreenDefinition();
		layoutRecord = TweakDBInterface.GetWidgetDefinitionRecord( m_layoutID );
		if( screenDef.screenDefinition )
		{
			newLibraryID = GetCurrentFullLibraryID( layoutRecord, screenDef.screenDefinition.ComputerScreenType(), screenDef.style );
		}
		if( m_currentLayoutLibraryID == newLibraryID )
		{
			return;
		}
		if( m_mainLayout != NULL )
		{
			( ( inkCompoundWidget )( GetRootWidget() ) ).RemoveChild( m_mainLayout );
		}
		spawnData = new AsyncSpawnData;
		spawnData.Initialize( this, 'OnMainLayoutSpawned', NULL, this );
		if( screenDef.screenDefinition )
		{
			m_currentLayoutLibraryID = RequestWidgetFromLibrary( GetRootWidget(), layoutRecord, screenDef.screenDefinition.ComputerScreenType(), screenDef.style, , , spawnData );
		}
	}

	protected function IsMainLayoutInitialized() : Bool
	{
		return m_mainLayout != NULL;
	}

	protected event OnMainLayoutSpawned( widget : inkWidget, userData : IScriptable )
	{
		var controller : ComputerMainLayoutWidgetController;
		m_mainLayout = widget;
		if( m_mainLayout )
		{
			m_mainLayout.SetAnchor( inkEAnchor.Fill );
			controller = GetMainLayoutController();
			if( controller )
			{
				controller.Initialize( this );
			}
			SetDevicesMenu( this, GetMainLayoutController().GetDevicesMenuContainer() );
			RegisterCloseWindowButtonCallback();
			if( GetOwner().GetHideTopNavigationBar() )
			{
				SetTopNavigationBarHidden( true );
			}
		}
	}

	public virtual function SetDevicesMenu( gameController : ComputerInkGameController, parentWidget : weak< inkWidget > )
	{
		var path : ResRef;
		var spawnData : AsyncSpawnData;
		if( IsDevicesManuSpawnRequested() )
		{
			return;
		}
		path = gameController.GetTerminalInkLibraryPath( gameController.GetScreenDefinition() );
		spawnData = new AsyncSpawnData;
		spawnData.Initialize( this, 'OnDevicesMenuSpawned', NULL, this );
		AsyncSpawnFromExternal( parentWidget, path, 'Root', this, 'OnDevicesMenuSpawned', spawnData );
		m_devicesMenuSpawnRequested = true;
	}

	protected event OnDevicesMenuSpawned( widget : inkWidget, userData : IScriptable )
	{
		var devicesMenu : inkWidget;
		devicesMenu = widget;
		if( devicesMenu )
		{
			devicesMenu.SetAnchor( inkEAnchor.Fill );
			devicesMenu.SetVisible( false );
			GetMainLayoutController().SetDevicesMenu( devicesMenu );
			m_devicesMenuSpawned = true;
			if( GetOwner().IsReadyForUI() )
			{
				Refresh( GetOwner().GetDeviceState() );
			}
		}
	}

	protected function IsDevicesManuSpawned() : Bool
	{
		return m_devicesMenuSpawned;
	}

	protected function IsDevicesManuSpawnRequested() : Bool
	{
		return m_devicesMenuSpawnRequested;
	}

	public virtual function GetComputerInkLibraryPath( screenDefinition : ScreenDefinitionPackage ) : String
	{
		var path : String;
		screenDefinition.screenDefinition.ComputerScreenType().LibraryPath();
		return path;
	}

	public virtual function GetTerminalInkLibraryPath( screenDefinition : ScreenDefinitionPackage ) : ResRef
	{
		return screenDefinition.screenDefinition.TerminalScreenType().LibraryPath();
	}

	public override function Refresh( state : EDeviceStatus )
	{
		SetupWidgets();
		InitializeMainLayout();
		if( !( IsMainLayoutInitialized() ) || !( IsDevicesManuSpawned() ) )
		{
			return;
		}
		switch( state )
		{
			case EDeviceStatus.ON:
				TurnOn();
			break;
			case EDeviceStatus.OFF:
				TurnOff();
			break;
			case EDeviceStatus.UNPOWERED:
				TurnOff();
			break;
			case EDeviceStatus.DISABLED:
				TurnOff();
			break;
			default:
				break;
		}
		super.Refresh( state );
	}

	protected override function RegisterBlackboardCallbacks( blackboard : IBlackboard )
	{
		super.RegisterBlackboardCallbacks( blackboard );
		if( blackboard )
		{
			m_onMailThumbnailWidgetsUpdateListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().MailThumbnailWidgetsData, this, 'OnMailThumbnailWidgetsUpdate' );
			m_onFileThumbnailWidgetsUpdateListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().FileThumbnailWidgetsData, this, 'OnFileThumbnailWidgetsUpdate' );
			m_onMailWidgetsUpdateListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().MailWidgetsData, this, 'OnMailWidgetsUpdate' );
			m_onFileWidgetsUpdateListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().FileWidgetsData, this, 'OnFileWidgetsUpdate' );
			m_onMenuButtonWidgetsUpdateListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().MenuButtonWidgetsData, this, 'OnMenuButtonWidgetsUpdate' );
			m_onMainMenuButtonWidgetsUpdateListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().MainMenuButtonWidgetsData, this, 'OnMainMenuButtonWidgetsUpdate' );
			m_onBannerWidgetsUpdateListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().BannerWidgetsData, this, 'OnBannerWidgetsUpdate' );
			m_onGlitchingStateChangedListener = blackboard.RegisterListenerVariant( GetOwner().GetBlackboardDef().GlitchData, this, 'OnGlitchingStateChanged' );
		}
	}

	protected override function UnRegisterBlackboardCallbacks( blackboard : IBlackboard )
	{
		super.UnRegisterBlackboardCallbacks( blackboard );
		if( blackboard )
		{
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().MailThumbnailWidgetsData, m_onMailThumbnailWidgetsUpdateListener );
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().FileThumbnailWidgetsData, m_onFileThumbnailWidgetsUpdateListener );
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().MailWidgetsData, m_onMailWidgetsUpdateListener );
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().FileWidgetsData, m_onFileWidgetsUpdateListener );
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().MenuButtonWidgetsData, m_onMenuButtonWidgetsUpdateListener );
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().MainMenuButtonWidgetsData, m_onMainMenuButtonWidgetsUpdateListener );
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().BannerWidgetsData, m_onBannerWidgetsUpdateListener );
			blackboard.UnregisterListenerVariant( GetOwner().GetBlackboardDef().GlitchData, m_onGlitchingStateChangedListener );
		}
	}

	private function ResolveInitialMenuType()
	{
		var menuType : EComputerMenuType;
		var element : SBreadcrumbElementData;
		menuType = GetOwner().GetInitialMenuType();
		switch( menuType )
		{
			case EComputerMenuType.MAILS:
				element.elementName = "mails";
			break;
			case EComputerMenuType.FILES:
				element.elementName = "files";
			break;
			case EComputerMenuType.SYSTEM:
				element.elementName = "devices";
			break;
			case EComputerMenuType.NEWSFEED:
				element.elementName = "newsFeed";
			break;
			case EComputerMenuType.MAIN:
				element.elementName = "mainMenu";
			break;
			case EComputerMenuType.INTERNET:
				element.elementName = "internet";
			break;
			default:
				break;
		}
		GoDown( element );
	}

	protected event OnBannerWidgetsUpdate( value : Variant )
	{
		var widgetsData : array< SBannerWidgetPackage >;
		widgetsData = ( ( array< SBannerWidgetPackage > )value );
		UpdateBannersWidgets( widgetsData );
	}

	protected event OnMailThumbnailWidgetsUpdate( value : Variant )
	{
		var widgetsData : array< SDocumentThumbnailWidgetPackage >;
		widgetsData = ( ( array< SDocumentThumbnailWidgetPackage > )value );
		UpdateMailsThumbnailsWidgets( widgetsData );
	}

	protected event OnFileThumbnailWidgetsUpdate( value : Variant )
	{
		var widgetsData : array< SDocumentThumbnailWidgetPackage >;
		widgetsData = ( ( array< SDocumentThumbnailWidgetPackage > )value );
		UpdateFilesThumbnailsWidgets( widgetsData );
	}

	protected event OnMailWidgetsUpdate( value : Variant )
	{
		var widgetsData : array< SDocumentWidgetPackage >;
		widgetsData = ( ( array< SDocumentWidgetPackage > )value );
		UpdateMailsWidgets( widgetsData );
	}

	protected event OnFileWidgetsUpdate( value : Variant )
	{
		var widgetsData : array< SDocumentWidgetPackage >;
		widgetsData = ( ( array< SDocumentWidgetPackage > )value );
		UpdateFilesWidgets( widgetsData );
	}

	protected event OnMenuButtonWidgetsUpdate( value : Variant )
	{
		var widgetsData : array< SComputerMenuButtonWidgetPackage >;
		widgetsData = ( ( array< SComputerMenuButtonWidgetPackage > )value );
		UpdateMenuButtonsWidgets( widgetsData );
	}

	protected event OnMainMenuButtonWidgetsUpdate( value : Variant )
	{
		var widgetsData : array< SComputerMenuButtonWidgetPackage >;
		widgetsData = ( ( array< SComputerMenuButtonWidgetPackage > )value );
		UpdateMainMenuButtonsWidgets( widgetsData );
	}

	public override function UpdateActionWidgets( const widgetsData : ref< array< SActionWidgetPackage > > )
	{
		var i : Int32;
		var widget : inkWidget;
		for( i = 0; i < widgetsData.Size(); i += 1 )
		{
			if( widgetsData[ i ].wasInitalized == true )
			{
				widget = GetActionWidget( widgetsData[ i ] );
				if( widget == NULL )
				{
					widget = AddActionWidget( GetMainLayoutController().GetOffButton(), widgetsData[ i ] );
				}
				InitializeActionWidget( widget, widgetsData[ i ] );
			}
		}
	}

	public virtual function UpdateMenuButtonsWidgets( const widgetsData : ref< array< SComputerMenuButtonWidgetPackage > > )
	{
		InitializeMenuButtons( widgetsData );
	}

	public virtual function UpdateMainMenuButtonsWidgets( const widgetsData : ref< array< SComputerMenuButtonWidgetPackage > > )
	{
		InitializeMainMenuButtons( widgetsData );
	}

	public virtual function UpdateBannersWidgets( const widgetsData : ref< array< SBannerWidgetPackage > > )
	{
		InitializeBanners( widgetsData );
	}

	public virtual function UpdateMailsWidgets( widgetsData : array< SDocumentWidgetPackage > )
	{
		InitializeMails( widgetsData );
	}

	public virtual function UpdateFilesWidgets( widgetsData : array< SDocumentWidgetPackage > )
	{
		InitializeFiles( widgetsData );
	}

	public virtual function UpdateMailsThumbnailsWidgets( const widgetsData : ref< array< SDocumentThumbnailWidgetPackage > > )
	{
		InitializeMailsThumbnails( widgetsData );
	}

	public virtual function UpdateFilesThumbnailsWidgets( const widgetsData : ref< array< SDocumentThumbnailWidgetPackage > > )
	{
		InitializeFilesThumbnails( widgetsData );
	}

	protected override function GetOwner() : Computer
	{
		return ( ( Computer )( GetOwnerEntity() ) );
	}

	public function ShowNewsfeed()
	{
		GetMainLayoutController().ShowNewsfeed();
		RequestBannerWidgetsUpdate();
	}

	public function ShowMails()
	{
		GetMainLayoutController().ShowMails();
		RequestMailThumbnailWidgetsUpdate();
	}

	public function ShowFiles()
	{
		GetMainLayoutController().ShowFiles();
		RequestFileThumbnailWidgetsUpdate();
	}

	public function ShowDevices()
	{
		if( !( IsDevicesManuSpawned() ) )
		{
			return;
		}
		GetMainLayoutController().ShowDevices();
		if( !( m_devicesMenuInitialized ) )
		{
			m_devicesMenuInitialized = true;
			RequestUIRefresh();
		}
	}

	public function ShowMainMenu()
	{
		GetMainLayoutController().ShowMainMenu();
		GetMainLayoutController().HideWindow();
		RequestMenuButtonWidgetsUpdate();
	}

	public function ShowInternet()
	{
		var internetData : SInternetData;
		internetData = ( ( ComputerControllerPS )( GetOwner().GetDevicePS() ) ).GetInternetData();
		GetMainLayoutController().ShowInternet( internetData.startingPage );
		RequestMainMenuButtonWidgetsUpdate();
	}

	protected virtual function TurnOn()
	{
		m_rootWidget.SetVisible( true );
		if( GetOwner().IsInSleepMode() )
		{
			GetMainLayoutController().ShowScreenSaver();
			GetMainLayoutController().HideWallpaper();
		}
		else
		{
			GetMainLayoutController().HideScreenSaver();
			GetMainLayoutController().ShowWallpaper();
			RequestActionWidgetsUpdate();
			ResolveBreadcrumbLevel();
		}
	}

	protected virtual function TurnOff()
	{
		m_rootWidget.SetVisible( false );
		ClearBreadcrumbStack();
		m_devicesMenuInitialized = false;
		ResolveInitialMenuType();
	}

	protected override function ResolveBreadcrumbLevel()
	{
		var element : SBreadcrumbElementData;
		var activeElement : SBreadcrumbElementData;
		if( !( GetOwner().GetDevicePS().IsPlayerAuthorized() ) || GetOwner().HasActiveStaticHackingSkillcheck() )
		{
			ShowDevices();
			return;
		}
		activeElement = GetActiveBreadcrumbElement();
		element = GetCurrentBreadcrumbElement();
		if( !( IsStringValid( element.elementName ) ) )
		{
			element.elementName = "mainMenu";
			GoDown( element );
		}
		if( activeElement.elementName == element.elementName )
		{
			HideMenuByName( element.elementName );
		}
		ShowMenuByName( element.elementName );
		if( element.elementName != "mainMenu" )
		{
			RequestMenuButtonWidgetsUpdate();
		}
		SetActiveBreadcrumbElement( element );
	}

	private function ShowMenuByName( elementName : String )
	{
		switch( elementName )
		{
			case "mails":
				ShowMails();
			break;
			case "files":
				ShowFiles();
			break;
			case "devices":
				ShowDevices();
			break;
			case "newsFeed":
				ShowNewsfeed();
			break;
			case "mainMenu":
				ShowMainMenu();
			break;
			case "internet":
				ShowInternet();
			break;
			default:
				break;
		}
		if( IsStringValid( elementName ) && elementName != "mainMenu" )
		{
			GetMainLayoutController().MarkManuButtonAsSelected( elementName );
		}
	}

	private function HideMenuByName( elementName : String )
	{
		switch( elementName )
		{
			case "mails":
				GetMainLayoutController().HideMails();
			break;
			case "files":
				GetMainLayoutController().HideFiles();
			break;
			case "devices":
				GetMainLayoutController().HideDevices();
			break;
			case "newsFeed":
				GetMainLayoutController().HideNewsFeed();
			break;
			case "mainMenu":
				GetMainLayoutController().HideMainMenu();
			break;
			case "internet":
				GetMainLayoutController().HideInternet();
			break;
			default:
				break;
		}
	}

	private function GoToMenu( const menuID : ref< String > )
	{
		var element : SBreadcrumbElementData;
		var elementName : String;
		var breadcrumbUpdateData : SBreadCrumbUpdateData;
		if( !( IsStringValid( menuID ) ) )
		{
			return;
		}
		element = GetCurrentBreadcrumbElement();
		elementName = element.elementName;
		if( elementName == "mainMenu" )
		{
			RequestMenuButtonWidgetsUpdate();
		}
		if( elementName != menuID )
		{
			GoUp();
			element.elementName = menuID;
			GoDown( element );
		}
		if( elementName == "devices" )
		{
			breadcrumbUpdateData.context = 'system_refresh';
			RequestBeadcrumbBarUpdate( breadcrumbUpdateData );
		}
		ResolveBreadcrumbLevel();
	}

	private function GetMenuName( menuType : EComputerMenuType ) : String
	{
		var menuName : String;
		switch( menuType )
		{
			case EComputerMenuType.MAIN:
				menuName = "mainMenu";
			break;
			case EComputerMenuType.MAILS:
				menuName = "mails";
			break;
			case EComputerMenuType.FILES:
				menuName = "files";
			break;
			case EComputerMenuType.INTERNET:
				menuName = "internet";
			break;
			case EComputerMenuType.SYSTEM:
				menuName = "devices";
			break;
			case EComputerMenuType.NEWSFEED:
				menuName = "newsFeed";
			break;
			default:
				break;
		}
		return menuName;
	}

	private function OpenDocument( documentType : EDocumentType, adress : SDocumentAdress )
	{
		var controller : ComputerDocumentThumbnailWidgetController;
		if( documentType == EDocumentType.MAIL )
		{
			controller = GetMainLayoutController().GetMailThumbnailController( adress );
		}
		else if( documentType == EDocumentType.FILE )
		{
			controller = GetMainLayoutController().GetFileThumbnailController( adress );
		}
		OpenDocument( controller );
	}

	public function OpenDocument( controller : ComputerDocumentThumbnailWidgetController )
	{
		var documentType : EDocumentType;
		var docStateEvt : SetDocumentStateEvent;
		if( controller == NULL )
		{
			return;
		}
		docStateEvt = new SetDocumentStateEvent;
		documentType = controller.GetDocumentType();
		docStateEvt.documentType = documentType;
		docStateEvt.isOpened = true;
		docStateEvt.documentAdress = controller.GetDocumentAdress();
		if( documentType == EDocumentType.MAIL )
		{
			RequestMailWidgetUpdate( controller.GetDocumentAdress() );
			GetMainLayoutController().MarkMailThumbnailAsSelected( controller );
		}
		else if( documentType == EDocumentType.FILE )
		{
			RequestFileWidgetUpdate( controller.GetDocumentAdress() );
			GetMainLayoutController().MarkFileThumbnailAsSelected( controller );
		}
		controller.OpenDocument();
		ResolveQuestInfo( controller.GetQuestInfo() );
		GetOwner().QueueEvent( docStateEvt );
	}

	public const function GetForceOpenDocumentType() : EDocumentType
	{
		return m_forceOpenDocumentType;
	}

	public const function GetForceOpenDocumentAdress() : SDocumentAdress
	{
		return m_forceOpenDocumentAdress;
	}

	public function ResetForceOpenDocumentData()
	{
		var invalidAdress : SDocumentAdress;
		m_forceOpenDocumentType = EDocumentType.Invalid;
		m_forceOpenDocumentAdress = invalidAdress;
	}

	protected event OnGoToMenuEvent( evt : GoToMenuEvent )
	{
		if( evt.ownerID == GetOwner().GetEntityID() )
		{
			if( evt.wakeUp )
			{
				GetMainLayoutController().HideScreenSaver();
			}
			GoToMenu( GetMenuName( evt.menuType ) );
		}
	}

	protected event OnOpenDocumentEvent( evt : OpenDocumentEvent )
	{
		var activeMenuID : String;
		var isReady : Bool;
		if( evt.ownerID == GetOwner().GetEntityID() )
		{
			if( evt.wakeUp )
			{
				GetMainLayoutController().HideScreenSaver();
			}
			activeMenuID = GetActiveBreadcrumbElementName();
			if( evt.documentType == EDocumentType.MAIL && activeMenuID != "mails" )
			{
				GoToMenu( "mails" );
			}
			else if( evt.documentType == EDocumentType.FILE && activeMenuID != "files" )
			{
				GoToMenu( "files" );
			}
			else
			{
				isReady = true;
			}
			if( isReady )
			{
				OpenDocument( evt.documentType, evt.documentAdress );
			}
			else
			{
				m_forceOpenDocumentType = evt.documentType;
				m_forceOpenDocumentAdress = evt.documentAdress;
			}
		}
	}

	protected function RegisterCloseWindowButtonCallback()
	{
		GetMainLayoutController().GetWindowCloseButton().RegisterToCallback( 'OnRelease', this, 'OnWindowCloseCallback' );
	}

	protected event OnWindowCloseCallback( e : inkPointerEvent )
	{
		if( IsInteractivityBlocked() )
		{
			return false;
		}
		if( e.IsAction( 'click' ) )
		{
			GetMainLayoutController().HideWindow();
		}
	}

	protected event OnMenuButtonCallback( e : inkPointerEvent )
	{
		var controller : ComputerMenuButtonController;
		if( IsInteractivityBlocked() )
		{
			return false;
		}
		if( e.IsAction( 'click' ) )
		{
			controller = ( ( ComputerMenuButtonController )( e.GetCurrentTarget().GetController() ) );
			if( controller )
			{
				GoToMenu( controller.GetMenuID() );
			}
		}
	}

	protected event OnDocumentThumbnailCallback( e : inkPointerEvent )
	{
		var controller : ComputerDocumentThumbnailWidgetController;
		if( IsInteractivityBlocked() )
		{
			return false;
		}
		if( e.IsAction( 'click' ) )
		{
			controller = ( ( ComputerDocumentThumbnailWidgetController )( e.GetCurrentTarget().GetController() ) );
			OpenDocument( controller );
		}
	}

	protected event OnHideFullBannerCallback( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			GetMainLayoutController().HideFullBanner();
		}
	}

	protected event OnHideMailCallback( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			GetMainLayoutController().HideMails();
		}
	}

	protected event OnHideFileCallback( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			GetMainLayoutController().HideFiles();
		}
	}

	protected event OnShowFullBannerCallback( e : inkPointerEvent )
	{
		var controller : ComputerBannerWidgetController;
		if( e.IsAction( 'click' ) )
		{
			controller = ( ( ComputerBannerWidgetController )( e.GetCurrentTarget().GetController() ) );
			if( controller )
			{
				GetMainLayoutController().ShowFullBanner( this, controller.GetBannerData() );
			}
		}
	}

	private function ResolveQuestInfo( questInfo : QuestInfo )
	{
		if( IsNameValid( questInfo.factName ) )
		{
			AddFact( GetOwner().GetGame(), questInfo.factName, 1 );
		}
	}

	public virtual function GetMainLayoutController() : ComputerMainLayoutWidgetController
	{
		return ( ( ComputerMainLayoutWidgetController )( m_mainLayout.GetController() ) );
	}

	protected function InitializeMails( const widgetsData : ref< array< SDocumentWidgetPackage > > )
	{
		GetMainLayoutController().InitializeMails( this, widgetsData );
	}

	protected function InitializeFiles( const widgetsData : ref< array< SDocumentWidgetPackage > > )
	{
		GetMainLayoutController().InitializeFiles( this, widgetsData );
	}

	protected function InitializeMailsThumbnails( const widgetsData : ref< array< SDocumentThumbnailWidgetPackage > > )
	{
		GetMainLayoutController().InitializeMailsThumbnails( this, widgetsData );
	}

	private function InitializeFilesThumbnails( const widgetsData : ref< array< SDocumentThumbnailWidgetPackage > > )
	{
		GetMainLayoutController().InitializeFilesThumbnails( this, widgetsData );
	}

	protected function InitializeBanners( const widgetsData : ref< array< SBannerWidgetPackage > > )
	{
		GetMainLayoutController().InitializeBanners( this, widgetsData );
	}

	protected function InitializeMainMenuButtons( const widgetsData : ref< array< SComputerMenuButtonWidgetPackage > > )
	{
		GetMainLayoutController().InitializeMainMenuButtons( this, widgetsData );
	}

	protected function InitializeMenuButtons( const widgetsData : ref< array< SComputerMenuButtonWidgetPackage > > )
	{
		GetMainLayoutController().InitializeMenuButtons( this, widgetsData );
		m_menuInitialized = true;
	}

	protected function SetTopNavigationBarHidden( isHidden : Bool )
	{
		GetMainLayoutController().SetTopNavigationBarHidden( isHidden );
	}

	public function RequestMenuButtonWidgetsUpdate()
	{
		var menuWidgetsEvent : RequestComputerMenuWidgetsUpdateEvent;
		menuWidgetsEvent = new RequestComputerMenuWidgetsUpdateEvent;
		menuWidgetsEvent.screenDefinition = GetOwner().GetScreenDefinition();
		GetOwner().QueueEvent( menuWidgetsEvent );
	}

	public function RequestMainMenuButtonWidgetsUpdate()
	{
		var menuWidgetsEvent : RequestComputerMainMenuWidgetsUpdateEvent;
		menuWidgetsEvent = new RequestComputerMainMenuWidgetsUpdateEvent;
		menuWidgetsEvent.screenDefinition = GetOwner().GetScreenDefinition();
		GetOwner().QueueEvent( menuWidgetsEvent );
	}

	public function RequestFileThumbnailWidgetsUpdate()
	{
		var documentThumbnailEvent : RequestDocumentThumbnailWidgetsUpdateEvent;
		documentThumbnailEvent = new RequestDocumentThumbnailWidgetsUpdateEvent;
		documentThumbnailEvent.documentType = EDocumentType.FILE;
		documentThumbnailEvent.screenDefinition = GetOwner().GetScreenDefinition();
		GetOwner().QueueEvent( documentThumbnailEvent );
	}

	public function RequestMailThumbnailWidgetsUpdate()
	{
		var documentThumbnailEvent : RequestDocumentThumbnailWidgetsUpdateEvent;
		documentThumbnailEvent = new RequestDocumentThumbnailWidgetsUpdateEvent;
		documentThumbnailEvent.documentType = EDocumentType.MAIL;
		documentThumbnailEvent.screenDefinition = GetOwner().GetScreenDefinition();
		GetOwner().QueueEvent( documentThumbnailEvent );
	}

	public function RequestMailWidgetUpdate( documentAdress : SDocumentAdress )
	{
		var documentEvent : RequestDocumentWidgetUpdateEvent;
		documentEvent = new RequestDocumentWidgetUpdateEvent;
		documentEvent.documentType = EDocumentType.MAIL;
		documentEvent.documentAdress = documentAdress;
		documentEvent.screenDefinition = GetOwner().GetScreenDefinition();
		GetOwner().QueueEvent( documentEvent );
	}

	public function RequestFileWidgetUpdate( documentAdress : SDocumentAdress )
	{
		var documentEvent : RequestDocumentWidgetUpdateEvent;
		documentEvent = new RequestDocumentWidgetUpdateEvent;
		documentEvent.documentType = EDocumentType.FILE;
		documentEvent.documentAdress = documentAdress;
		documentEvent.screenDefinition = GetOwner().GetScreenDefinition();
		GetOwner().QueueEvent( documentEvent );
	}

	public function RequestBannerWidgetsUpdate()
	{
		var bannerEvent : RequestBannerWidgetUpdateEvent;
		bannerEvent = new RequestBannerWidgetUpdateEvent;
		bannerEvent.screenDefinition = GetOwner().GetScreenDefinition();
		GetOwner().QueueEvent( bannerEvent );
	}

	private override function StartGlitchingScreen( glitchData : GlitchData )
	{
		var glitchVideoPath : ResRef;
		if( glitchData.state == EGlitchState.SUBLIMINAL_MESSAGE )
		{
			glitchVideoPath = GetOwner().GetBroadcastGlitchVideoPath();
		}
		else
		{
			glitchVideoPath = GetOwner().GetDefaultGlitchVideoPath();
		}
		if( ResRef.IsValid( glitchVideoPath ) )
		{
			m_mainDisplayWidget.SetVisible( true );
			m_mainLayout.SetVisible( false );
			StopVideo();
			PlayVideo( glitchVideoPath, true, '' );
		}
	}

	private override function StopGlitchingScreen()
	{
		StopVideo();
		m_mainDisplayWidget.SetVisible( false );
		m_mainLayout.SetVisible( true );
	}

	public function PlayVideo( videoPath : ResRef, looped : Bool, audioEvent : CName )
	{
		m_mainDisplayWidget.SetVideoPath( videoPath );
		m_mainDisplayWidget.SetLoop( looped );
		if( IsNameValid( audioEvent ) )
		{
			m_mainDisplayWidget.SetAudioEvent( audioEvent );
		}
		m_mainDisplayWidget.Play();
	}

	public function StopVideo()
	{
		m_mainDisplayWidget.Stop();
	}

}

