class ComputerMenuWidgetController extends inkLogicController
{
	[ category = "Widget Refs" ]
	protected editable var m_thumbnailsListWidget : inkWidgetRef;
	[ category = "Widget Refs" ]
	protected editable var m_contentWidget : inkWidgetRef;
	protected var m_isInitialized : Bool;
	private var m_fileWidgetsData : array< SDocumentWidgetPackage >;
	private var m_fileThumbnailWidgetsData : array< SDocumentThumbnailWidgetPackage >;

	public virtual function InitializeFiles( gameController : ComputerInkGameController, const widgetsData : ref< array< SDocumentWidgetPackage > > )
	{
		var i : Int32;
		var widget : inkWidget;
		HideFileWidgets();
		for( i = 0; i < widgetsData.Size(); i += 1 )
		{
			widget = GetFileWidget( widgetsData[ i ], gameController );
			if( widget == NULL )
			{
				if( widgetsData[ i ].placement == EWidgetPlacementType.FLOATING )
				{
					CreateDocumentWidgetAsync( gameController, gameController.GetMainLayoutController().GetMenuContainer(), widgetsData[ i ] );
				}
				else
				{
					CreateDocumentWidgetAsync( gameController, inkWidgetRef.Get( m_contentWidget ), widgetsData[ i ] );
				}
			}
			else
			{
				InitializeDocumentWidget( gameController, widget, widgetsData[ i ] );
			}
		}
		m_isInitialized = true;
	}

	public virtual function InitializeFilesThumbnails( gameController : ComputerInkGameController, const widgetsData : ref< array< SDocumentThumbnailWidgetPackage > > )
	{
		var i : Int32;
		var widget : inkWidget;
		HideFileThumbnailWidgets();
		for( i = 0; i < widgetsData.Size(); i += 1 )
		{
			widget = GetFileThumbnailWidget( widgetsData[ i ], gameController );
			if( widget == NULL )
			{
				CreateDocumentThumbnailWidgetAsync( gameController, inkWidgetRef.Get( m_thumbnailsListWidget ), widgetsData[ i ] );
			}
			else
			{
				InitializeDocumentThumbnailWidget( gameController, widget, widgetsData[ i ] );
			}
		}
		m_isInitialized = true;
	}

	public function CreateDocumentWidget( gameController : ComputerInkGameController, parentWidget : weak< inkWidget >, const widgetData : ref< SDocumentWidgetPackage > ) : weak< inkWidget >
	{
		var widget : inkWidget;
		var screenDef : ScreenDefinitionPackage;
		screenDef = gameController.GetScreenDefinition();
		widget = gameController.FindWidgetInLibrary( parentWidget, TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.ComputerScreenType(), screenDef.style, widgetData.libraryID, widgetData.libraryPath );
		if( widget != NULL )
		{
			widget.SetAnchor( inkEAnchor.Fill );
			widget.SetSizeRule( inkESizeRule.Stretch );
		}
		return widget;
	}

	protected function CreateDocumentWidgetAsync( gameController : ComputerInkGameController, parentWidget : weak< inkWidget >, widgetData : SDocumentWidgetPackage )
	{
		var screenDef : ScreenDefinitionPackage;
		var spawnData : AsyncSpawnData;
		if( HasFileWidgetData( widgetData, gameController ) )
		{
			return;
		}
		screenDef = gameController.GetScreenDefinition();
		spawnData = new AsyncSpawnData;
		spawnData.Initialize( this, 'OnDocumentWidgetSpawned', widgetData, gameController );
		widgetData.libraryID = gameController.RequestWidgetFromLibrary( parentWidget, TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.ComputerScreenType(), screenDef.style, widgetData.libraryID, widgetData.libraryPath, spawnData );
		AddFileWidgetData( widgetData, gameController );
	}

	protected event OnDocumentWidgetSpawned( widget : inkWidget, userData : IScriptable )
	{
		var spawnData : AsyncSpawnData;
		var widgetData : SDocumentWidgetPackage;
		var deviceGameController : ComputerInkGameController;
		if( widget != NULL )
		{
			widget.SetAnchor( inkEAnchor.Fill );
			widget.SetSizeRule( inkESizeRule.Stretch );
		}
		spawnData = ( ( AsyncSpawnData )( userData ) );
		if( spawnData != NULL )
		{
			deviceGameController = ( ( ComputerInkGameController )( spawnData.m_controller ) );
			widgetData = ( ( SDocumentWidgetPackage )spawnData.m_widgetData );
			if( deviceGameController != NULL )
			{
				widgetData.widget = widget;
				widgetData.libraryID = spawnData.m_libraryID;
				UpdateFileWidgetData( widgetData, GetFileWidgetDataIndex( widgetData, deviceGameController ) );
				InitializeDocumentWidget( deviceGameController, widget, widgetData );
			}
		}
	}

	protected function AddFileWidgetData( widgetData : SDocumentWidgetPackage, gameController : DeviceInkGameControllerBase )
	{
		var screenDef : ScreenDefinitionPackage;
		screenDef = gameController.GetScreenDefinition();
		widgetData.libraryID = gameController.GetCurrentFullLibraryID( TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.ComputerScreenType(), screenDef.style );
		m_fileWidgetsData.PushBack( widgetData );
	}

	protected function UpdateFileWidgetData( const widgetData : ref< SDocumentWidgetPackage >, index : Int32 )
	{
		if( ( index >= 0 ) && ( index < m_fileWidgetsData.Size() ) )
		{
			m_fileWidgetsData[ index ] = widgetData;
		}
	}

	protected function InitializeDocumentWidget( gameController : ComputerInkGameController, widget : inkWidget, const widgetData : ref< SDocumentWidgetPackage > )
	{
		var controller : ComputerDocumentWidgetController;
		controller = ( ( ComputerDocumentWidgetController )( widget.GetController() ) );
		if( controller != NULL )
		{
			controller.Initialize( gameController, widgetData );
		}
		widget.SetVisible( true );
	}

	protected function GetFileWidgetDataIndex( widgetData : SDocumentWidgetPackage, gameController : DeviceInkGameControllerBase ) : Int32
	{
		var i : Int32;
		var screenDef : ScreenDefinitionPackage;
		screenDef = gameController.GetScreenDefinition();
		widgetData.libraryID = gameController.GetCurrentFullLibraryID( TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.ComputerScreenType(), screenDef.style );
		for( i = 0; i < m_fileWidgetsData.Size(); i += 1 )
		{
			if( ( ( ( m_fileWidgetsData[ i ].ownerID == widgetData.ownerID && m_fileWidgetsData[ i ].widgetName == widgetData.widgetName ) && ( m_fileWidgetsData[ i ].widgetTweakDBID == widgetData.widgetTweakDBID ) ) && m_fileWidgetsData[ i ].libraryPath == widgetData.libraryPath ) && m_fileWidgetsData[ i ].libraryID == widgetData.libraryID )
			{
				return i;
			}
		}
		return -1;
	}

	protected function HasFileWidgetData( const widgetData : ref< SDocumentWidgetPackage >, gameController : DeviceInkGameControllerBase ) : Bool
	{
		return GetFileWidgetDataIndex( widgetData, gameController ) >= 0;
	}

	protected function GetFileWidget( const widgetData : ref< SDocumentWidgetPackage >, gameController : ComputerInkGameController ) : weak< inkWidget >
	{
		var index : Int32;
		index = GetFileWidgetDataIndex( widgetData, gameController );
		if( ( index >= 0 ) && ( index < m_fileWidgetsData.Size() ) )
		{
			return m_fileWidgetsData[ index ].widget;
		}
		return NULL;
	}

	protected function AddFileWidget( widget : inkWidget, widgetData : SDocumentWidgetPackage, gameController : ComputerInkGameController ) : weak< inkWidget >
	{
		var screenDef : ScreenDefinitionPackage;
		screenDef = gameController.GetScreenDefinition();
		widgetData.libraryID = gameController.GetCurrentFullLibraryID( TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.ComputerScreenType(), screenDef.style );
		widgetData.widget = widget;
		m_fileWidgetsData.PushBack( widgetData );
		return widgetData.widget;
	}

	public function HideFileWidgets()
	{
		var i : Int32;
		for( i = 0; i < m_fileWidgetsData.Size(); i += 1 )
		{
			if( m_fileWidgetsData[ i ].widget != NULL )
			{
				m_fileWidgetsData[ i ].widget.SetVisible( false );
			}
		}
	}

	public function CreateDocumentThumbnailWidget( gameController : ComputerInkGameController, parentWidget : weak< inkWidget >, const widgetData : ref< SDocumentThumbnailWidgetPackage > ) : weak< inkWidget >
	{
		var widget : inkWidget;
		var screenDef : ScreenDefinitionPackage;
		screenDef = gameController.GetScreenDefinition();
		widget = gameController.FindWidgetInLibrary( parentWidget, TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.ComputerScreenType(), screenDef.style, widgetData.libraryID, widgetData.libraryPath );
		return widget;
	}

	protected function CreateDocumentThumbnailWidgetAsync( gameController : ComputerInkGameController, parentWidget : weak< inkWidget >, widgetData : SDocumentThumbnailWidgetPackage )
	{
		var screenDef : ScreenDefinitionPackage;
		var spawnData : AsyncSpawnData;
		if( HasFileThumbnailWidgetData( widgetData, gameController ) )
		{
			return;
		}
		screenDef = gameController.GetScreenDefinition();
		spawnData = new AsyncSpawnData;
		spawnData.Initialize( this, 'OnDocumentThumbnailWidgetSpawned', widgetData, gameController );
		widgetData.libraryID = gameController.RequestWidgetFromLibrary( parentWidget, TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.ComputerScreenType(), screenDef.style, widgetData.libraryID, widgetData.libraryPath, spawnData );
		AddFileThumbnailWidgetData( widgetData, gameController );
	}

	protected event OnDocumentThumbnailWidgetSpawned( widget : inkWidget, userData : IScriptable )
	{
		var spawnData : AsyncSpawnData;
		var widgetData : SDocumentThumbnailWidgetPackage;
		var deviceGameController : ComputerInkGameController;
		if( widget != NULL )
		{
			widget.SetHAlign( inkEHorizontalAlign.Fill );
		}
		spawnData = ( ( AsyncSpawnData )( userData ) );
		if( spawnData != NULL )
		{
			deviceGameController = ( ( ComputerInkGameController )( spawnData.m_controller ) );
			widgetData = ( ( SDocumentThumbnailWidgetPackage )spawnData.m_widgetData );
			if( deviceGameController != NULL )
			{
				widgetData.widget = widget;
				widgetData.libraryID = spawnData.m_libraryID;
				UpdateFileThumbnailWidgetData( widgetData, GetFileThumbnailWidgetDataIndex( widgetData, deviceGameController ) );
				InitializeDocumentThumbnailWidget( deviceGameController, widget, widgetData );
			}
		}
	}

	protected function AddFileThumbnailWidgetData( widgetData : SDocumentThumbnailWidgetPackage, gameController : DeviceInkGameControllerBase )
	{
		var screenDef : ScreenDefinitionPackage;
		screenDef = gameController.GetScreenDefinition();
		widgetData.libraryID = gameController.GetCurrentFullLibraryID( TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.ComputerScreenType(), screenDef.style );
		m_fileThumbnailWidgetsData.PushBack( widgetData );
	}

	protected function UpdateFileThumbnailWidgetData( const widgetData : ref< SDocumentThumbnailWidgetPackage >, index : Int32 )
	{
		if( ( index >= 0 ) && ( index < m_fileThumbnailWidgetsData.Size() ) )
		{
			m_fileThumbnailWidgetsData[ index ] = widgetData;
		}
	}

	protected function InitializeDocumentThumbnailWidget( gameController : ComputerInkGameController, widget : inkWidget, const widgetData : ref< SDocumentThumbnailWidgetPackage > )
	{
		var controller : ComputerDocumentThumbnailWidgetController;
		controller = ( ( ComputerDocumentThumbnailWidgetController )( widget.GetController() ) );
		if( controller != NULL )
		{
			controller.Initialize( gameController, widgetData );
			controller.ToggleSelection( widgetData.isOpened );
			if( widgetData.isOpened )
			{
				if( widgetData.documentType == EDocumentType.MAIL )
				{
					gameController.RequestMailWidgetUpdate( widgetData.documentAdress );
				}
				else if( widgetData.documentType == EDocumentType.FILE )
				{
					gameController.RequestFileWidgetUpdate( widgetData.documentAdress );
				}
			}
		}
		widget.SetVisible( true );
	}

	protected function HasFileThumbnailWidgetData( const widgetData : ref< SDocumentThumbnailWidgetPackage >, gameController : DeviceInkGameControllerBase ) : Bool
	{
		return GetFileThumbnailWidgetDataIndex( widgetData, gameController ) >= 0;
	}

	protected function GetFileThumbnailWidgetDataIndex( widgetData : SDocumentThumbnailWidgetPackage, gameController : DeviceInkGameControllerBase ) : Int32
	{
		var i : Int32;
		var screenDef : ScreenDefinitionPackage;
		screenDef = gameController.GetScreenDefinition();
		widgetData.libraryID = gameController.GetCurrentFullLibraryID( TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.ComputerScreenType(), screenDef.style );
		for( i = 0; i < m_fileThumbnailWidgetsData.Size(); i += 1 )
		{
			if( ( ( ( m_fileThumbnailWidgetsData[ i ].ownerID == widgetData.ownerID && m_fileThumbnailWidgetsData[ i ].widgetName == widgetData.widgetName ) && ( m_fileThumbnailWidgetsData[ i ].widgetTweakDBID == widgetData.widgetTweakDBID ) ) && m_fileThumbnailWidgetsData[ i ].libraryPath == widgetData.libraryPath ) && m_fileThumbnailWidgetsData[ i ].libraryID == widgetData.libraryID )
			{
				return i;
			}
		}
		return -1;
	}

	protected function GetFileThumbnailWidget( const widgetData : ref< SDocumentThumbnailWidgetPackage >, gameController : ComputerInkGameController ) : weak< inkWidget >
	{
		var index : Int32;
		index = GetFileThumbnailWidgetDataIndex( widgetData, gameController );
		if( ( index >= 0 ) && ( index < m_fileThumbnailWidgetsData.Size() ) )
		{
			return m_fileThumbnailWidgetsData[ index ].widget;
		}
		return NULL;
	}

	protected function AddFileThumbnailWidget( widget : inkWidget, widgetData : SDocumentThumbnailWidgetPackage, gameController : ComputerInkGameController ) : weak< inkWidget >
	{
		var screenDef : ScreenDefinitionPackage;
		screenDef = gameController.GetScreenDefinition();
		widgetData.libraryID = gameController.GetCurrentFullLibraryID( TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.ComputerScreenType(), screenDef.style );
		widgetData.widget = widget;
		m_fileThumbnailWidgetsData.PushBack( widgetData );
		return widgetData.widget;
	}

	public function HideFileThumbnailWidgets()
	{
		var i : Int32;
		var currentController : ComputerDocumentThumbnailWidgetController;
		HideFileWidgets();
		for( i = 0; i < m_fileThumbnailWidgetsData.Size(); i += 1 )
		{
			if( m_fileThumbnailWidgetsData[ i ].widget != NULL )
			{
				currentController = ( ( ComputerDocumentThumbnailWidgetController )( m_fileThumbnailWidgetsData[ i ].widget.GetController() ) );
				m_fileThumbnailWidgetsData[ i ].widget.SetVisible( false );
				if( currentController )
				{
					currentController.ToggleSelection( false );
				}
			}
		}
	}

	public function GetThumbnailController( adress : SDocumentAdress ) : ComputerDocumentThumbnailWidgetController
	{
		var i : Int32;
		var currentController : ComputerDocumentThumbnailWidgetController;
		var currentAdress : SDocumentAdress;
		for( i = 0; i < m_fileThumbnailWidgetsData.Size(); i += 1 )
		{
			if( m_fileThumbnailWidgetsData[ i ].widget != NULL )
			{
				currentController = ( ( ComputerDocumentThumbnailWidgetController )( m_fileThumbnailWidgetsData[ i ].widget.GetController() ) );
				currentAdress = currentController.GetDocumentAdress();
				if( currentController && ( currentAdress == adress ) )
				{
					break;
				}
				else
				{
					currentController = NULL;
				}
			}
		}
		return currentController;
	}

	public function MarkThumbnailAsSelected( adress : SDocumentAdress )
	{
		var i : Int32;
		var currentController : ComputerDocumentThumbnailWidgetController;
		var currentAdress : SDocumentAdress;
		for( i = 0; i < m_fileThumbnailWidgetsData.Size(); i += 1 )
		{
			if( m_fileThumbnailWidgetsData[ i ].widget != NULL )
			{
				currentController = ( ( ComputerDocumentThumbnailWidgetController )( m_fileThumbnailWidgetsData[ i ].widget.GetController() ) );
				currentAdress = currentController.GetDocumentAdress();
				if( currentController && ( currentAdress == adress ) )
				{
					currentController.ToggleSelection( true );
				}
				else
				{
					currentController.ToggleSelection( false );
				}
			}
		}
	}

	public function MarkThumbnailAsSelected( controller : ComputerDocumentThumbnailWidgetController )
	{
		var i : Int32;
		var currentController : ComputerDocumentThumbnailWidgetController;
		for( i = 0; i < m_fileThumbnailWidgetsData.Size(); i += 1 )
		{
			if( m_fileThumbnailWidgetsData[ i ].widget != NULL )
			{
				currentController = ( ( ComputerDocumentThumbnailWidgetController )( m_fileThumbnailWidgetsData[ i ].widget.GetController() ) );
				if( currentController && ( currentController == controller ) )
				{
					currentController.ToggleSelection( true );
				}
				else
				{
					currentController.ToggleSelection( false );
				}
			}
		}
	}

}

