class ComputerMainMenuWidgetController extends inkLogicController
{
	[ category = "Widget Refs" ]
	protected editable var m_menuButtonsListWidget : inkWidgetRef;
	protected var m_isInitialized : Bool;
	private var m_computerMenuButtonWidgetsData : array< SComputerMenuButtonWidgetPackage >;

	public virtual function InitializeMenuButtons( gameController : ComputerInkGameController, const widgetsData : ref< array< SComputerMenuButtonWidgetPackage > > )
	{
		var i : Int32;
		var widget : inkWidget;
		HideMenuButtonWidgets();
		for( i = 0; i < widgetsData.Size(); i += 1 )
		{
			widget = GetMenuButtonWidget( widgetsData[ i ], gameController );
			if( widget == NULL )
			{
				widget = CreateMenuButtonWidget( gameController, inkWidgetRef.Get( m_menuButtonsListWidget ), widgetsData[ i ] );
				AddMenuButtonWidget( widget, widgetsData[ i ], gameController );
			}
			InitializeMenuButtonWidget( gameController, widget, widgetsData[ i ] );
		}
		m_isInitialized = true;
	}

	public function CreateMenuButtonWidget( gameController : ComputerInkGameController, parentWidget : weak< inkWidget >, widgetData : SComputerMenuButtonWidgetPackage ) : 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 InitializeMenuButtonWidget( gameController : ComputerInkGameController, widget : inkWidget, widgetData : SComputerMenuButtonWidgetPackage )
	{
		var controller : ComputerMenuButtonController;
		controller = ( ( ComputerMenuButtonController )( widget.GetController() ) );
		if( controller != NULL )
		{
			controller.Initialize( gameController, widgetData );
		}
		widget.SetVisible( true );
	}

	protected function GetMenuButtonWidget( widgetData : SComputerMenuButtonWidgetPackage, gameController : ComputerInkGameController ) : weak< inkWidget >
	{
		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_computerMenuButtonWidgetsData.Size(); i += 1 )
		{
			if( ( ( ( m_computerMenuButtonWidgetsData[ i ].ownerID == widgetData.ownerID && m_computerMenuButtonWidgetsData[ i ].widgetName == widgetData.widgetName ) && ( m_computerMenuButtonWidgetsData[ i ].widgetTweakDBID == widgetData.widgetTweakDBID ) ) && m_computerMenuButtonWidgetsData[ i ].libraryPath == widgetData.libraryPath ) && m_computerMenuButtonWidgetsData[ i ].libraryID == widgetData.libraryID )
			{
				return m_computerMenuButtonWidgetsData[ i ].widget;
			}
		}
		return NULL;
	}

	protected function AddMenuButtonWidget( widget : inkWidget, widgetData : SComputerMenuButtonWidgetPackage, 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_computerMenuButtonWidgetsData.PushBack( widgetData );
		return widgetData.widget;
	}

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

}

