class DeviceWidgetControllerBase extends DeviceInkLogicControllerBase
{
	[ category = "Widget Refs" ]
	protected editable var m_backgroundTextureRef : inkImageRef;
	[ category = "Widget Refs" ]
	protected editable var m_statusNameWidget : inkTextRef;
	[ category = "Widget Refs" ]
	protected editable var m_actionsListWidget : inkWidgetRef;
	protected var m_actionWidgetsData : array< SActionWidgetPackage >;
	protected var m_actionData : ResolveActionData;

	public virtual function Initialize( gameController : DeviceInkGameControllerBase, widgetData : SDeviceWidgetPackage )
	{
		var i : Int32;
		var actionWidget : inkWidget;
		HideActionWidgets();
		for( i = 0; i < widgetData.actionWidgets.Size(); i += 1 )
		{
			actionWidget = GetActionWidget( widgetData.actionWidgets[ i ], gameController );
			if( actionWidget == NULL )
			{
				CreateActionWidgetAsync( gameController, GetParentForActionWidgets(), widgetData.actionWidgets[ i ] );
			}
			else
			{
				InitializeActionWidget( gameController, actionWidget, widgetData.actionWidgets[ i ] );
			}
		}
		if( TDBID.IsValid( widgetData.bckgroundTextureID ) )
		{
			SetTexture( m_backgroundTextureRef, widgetData.bckgroundTextureID );
		}
		else
		{
			inkImageRef.SetVisible( m_backgroundTextureRef, false );
		}
		inkTextRef.SetLocalizedTextScript( m_statusNameWidget, widgetData.deviceStatus, widgetData.textData );
		inkTextRef.SetLocalizedTextScript( m_displayNameWidget, widgetData.displayName );
		if( widgetData.widgetState == EWidgetState.ALLOWED )
		{
			inkTextRef.SetState( m_statusNameWidget, 'Allowed' );
			inkTextRef.SetState( m_displayNameWidget, 'Allowed' );
		}
		else if( widgetData.widgetState == EWidgetState.LOCKED )
		{
			inkTextRef.SetState( m_statusNameWidget, 'Locked' );
			inkTextRef.SetState( m_displayNameWidget, 'Locked' );
		}
		else if( widgetData.widgetState == EWidgetState.SEALED )
		{
			inkTextRef.SetState( m_statusNameWidget, 'Sealed' );
			inkTextRef.SetState( m_displayNameWidget, 'Sealed' );
		}
		m_isInitialized = true;
		if( gameController != NULL )
		{
			gameController.SetUICameraZoomState( false );
		}
	}

	public virtual function GetParentForActionWidgets() : weak< inkWidget >
	{
		return inkWidgetRef.Get( m_actionsListWidget );
	}

	protected function CreateActionWidgetAsync( gameController : DeviceInkGameControllerBase, parentWidget : weak< inkWidget >, widgetData : SActionWidgetPackage )
	{
		var screenDef : ScreenDefinitionPackage;
		var spawnData : AsyncSpawnData;
		if( HasActionWidgetData( widgetData, gameController ) )
		{
			return;
		}
		screenDef = gameController.GetScreenDefinition();
		spawnData = new AsyncSpawnData;
		spawnData.Initialize( this, 'OnActionWidgetSpawned', widgetData, gameController );
		widgetData.libraryID = gameController.RequestWidgetFromLibrary( parentWidget, TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.TerminalScreenType(), screenDef.style, widgetData.libraryID, widgetData.libraryPath, spawnData );
		AddActionWidgetData( widgetData, gameController );
	}

	protected event OnActionWidgetSpawned( widget : inkWidget, userData : IScriptable )
	{
		var spawnData : AsyncSpawnData;
		var widgetData : SActionWidgetPackage;
		var deviceGameController : DeviceInkGameControllerBase;
		if( widget != NULL )
		{
			widget.SetSizeRule( inkESizeRule.Stretch );
		}
		spawnData = ( ( AsyncSpawnData )( userData ) );
		if( spawnData != NULL )
		{
			deviceGameController = ( ( DeviceInkGameControllerBase )( spawnData.m_controller ) );
			widgetData = ( ( SActionWidgetPackage )spawnData.m_widgetData );
			if( deviceGameController != NULL )
			{
				widgetData.widget = widget;
				widgetData.libraryID = spawnData.m_libraryID;
				UpdateActionWidgetData( widgetData, GetActionWidgetDataIndex( widgetData, deviceGameController ) );
				InitializeActionWidget( deviceGameController, widget, widgetData );
			}
		}
	}

	protected function CreateActionWidget( gameController : DeviceInkGameControllerBase, parentWidget : weak< inkWidget >, const widgetData : ref< SActionWidgetPackage > ) : weak< inkWidget >
	{
		var widget : inkWidget;
		var screenDef : ScreenDefinitionPackage;
		screenDef = gameController.GetScreenDefinition();
		widget = gameController.FindWidgetInLibrary( parentWidget, TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.TerminalScreenType(), screenDef.style, widgetData.libraryID, widgetData.libraryPath );
		if( widget != NULL )
		{
			widget.SetSizeRule( inkESizeRule.Stretch );
		}
		return widget;
	}

	protected function GetActionWidget( const widgetData : ref< SActionWidgetPackage >, gameController : DeviceInkGameControllerBase ) : weak< inkWidget >
	{
		var index : Int32;
		index = GetActionWidgetDataIndex( widgetData, gameController );
		if( ( index >= 0 ) && ( index < m_actionWidgetsData.Size() ) )
		{
			return m_actionWidgetsData[ index ].widget;
		}
		return NULL;
	}

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

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

	protected function HasActionWidgetData( const widgetData : ref< SActionWidgetPackage >, gameController : DeviceInkGameControllerBase ) : Bool
	{
		return GetActionWidgetDataIndex( widgetData, gameController ) >= 0;
	}

	protected function HasActionWidget( const widgetData : ref< SActionWidgetPackage >, gameController : DeviceInkGameControllerBase ) : Bool
	{
		return GetActionWidget( widgetData, gameController ) != NULL;
	}

	protected function AddActionWidget( widget : inkWidget, widgetData : SActionWidgetPackage, gameController : DeviceInkGameControllerBase ) : weak< inkWidget >
	{
		var screenDef : ScreenDefinitionPackage;
		screenDef = gameController.GetScreenDefinition();
		widgetData.libraryID = gameController.GetCurrentFullLibraryID( TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.TerminalScreenType(), screenDef.style );
		widgetData.widget = widget;
		m_actionWidgetsData.PushBack( widgetData );
		return widgetData.widget;
	}

	protected function AddActionWidgetData( widgetData : SActionWidgetPackage, gameController : DeviceInkGameControllerBase )
	{
		var screenDef : ScreenDefinitionPackage;
		screenDef = gameController.GetScreenDefinition();
		widgetData.libraryID = gameController.GetCurrentFullLibraryID( TweakDBInterface.GetWidgetDefinitionRecord( widgetData.widgetTweakDBID ), screenDef.screenDefinition.TerminalScreenType(), screenDef.style );
		m_actionWidgetsData.PushBack( widgetData );
	}

	protected function InitializeActionWidget( gameController : DeviceInkGameControllerBase, widget : inkWidget, const widgetData : ref< SActionWidgetPackage > )
	{
		var controller : DeviceActionWidgetControllerBase;
		controller = ( ( DeviceActionWidgetControllerBase )( widget.GetController() ) );
		if( controller != NULL )
		{
			controller.Initialize( gameController, widgetData );
		}
		widget.SetVisible( true );
	}

	protected function RegisterButtonWidgetToAudioCallbacks( gameController : DeviceInkGameControllerBase, widget : inkWidget )
	{
		var controller : DeviceButtonLogicControllerBase;
		controller = ( ( DeviceButtonLogicControllerBase )( widget.GetController() ) );
		if( controller != NULL )
		{
			controller.RegisterAudioCallbacks( gameController );
		}
	}

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

	protected virtual function ResolveAction( const widgetData : ref< SActionWidgetPackage > ) {}
}

