class TerminalSystemCustomData extends WidgetCustomData
{
	var connectedDevices : Int32;
}

class VirtualSystemPS extends MasterControllerPS
{
	default m_deviceName = "SYSTEM";
	private var m_owner : weak< MasterControllerPS >;
	protected var m_slaves : array< DeviceComponentPS >;
	private var m_slavesCached : Bool;

	public const function IsPartOfSystem( targetID : PersistentID ) : Bool
	{
		var i : Int32;
		for( i = 0; i < m_slaves.Size(); i += 1 )
		{
			if( targetID == m_slaves[ i ].GetID() )
			{
				return true;
			}
		}
		return false;
	}

	public const function IsPartOfSystem( target : DeviceComponentPS ) : Bool
	{
		var i : Int32;
		for( i = 0; i < m_slaves.Size(); i += 1 )
		{
			if( target == m_slaves[ i ] )
			{
				return true;
			}
		}
		return false;
	}

	public override function GetDeviceWidget( const context : ref< GetActionsContext > ) : SDeviceWidgetPackage
	{
		var widgetData : SDeviceWidgetPackage;
		var customData : TerminalSystemCustomData;
		widgetData = super.GetDeviceWidget( context );
		if( widgetData.isValid )
		{
			customData = new TerminalSystemCustomData;
			customData.connectedDevices = m_slaves.Size();
			widgetData.customData = customData;
		}
		return widgetData;
	}

	public function GetDeviceWidget( const context : ref< GetActionsContext >, data : ref< array< SDeviceWidgetPackage > > )
	{
		data.PushBack( GetDeviceWidget( context ) );
	}

	public const override function GetDeviceStatus() : String
	{
		if( IsON() )
		{
			return "LocKey#51459";
		}
		else
		{
			return "LocKey#51460";
		}
	}

	protected override function GetInkWidgetTweakDBID( const context : ref< GetActionsContext > ) : TweakDBID
	{
		return T"DevicesUIDefinitions.DynamicSystemLayoutWidget";
	}

	protected override function ActionThumbnailUI() : ThumbnailUI
	{
		var action : ThumbnailUI;
		action = super.ActionThumbnailUI();
		action.CreateThumbnailWidgetPackage( T"DevicesUIDefinitions.SystemDeviceThumnbnailWidget", "LocKey#42210" );
		return action;
	}

	public function Initialize( const slaves : ref< array< DeviceComponentPS > >, owner : MasterControllerPS )
	{
		var i : Int32;
		for( i = 0; i < slaves.Size(); i += 1 )
		{
			m_slaves.PushBack( slaves[ i ] );
		}
		m_slavesCached = true;
		m_owner = owner;
	}

	public override function GetActions( out outActions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		outActions.PushBack( ActionToggleON() );
		return true;
	}

	public override function OnToggleON( evt : ToggleON ) : EntityNotificationType
	{
		var actionToSend : ScriptableDeviceAction;
		super.OnToggleON( evt );
		if( IsON() )
		{
			actionToSend = ActionSetDeviceON();
		}
		else
		{
			actionToSend = ActionSetDeviceOFF();
		}
		SendActionToAllSlaves( actionToSend );
		return EntityNotificationType.DoNotNotifyEntity;
	}

	protected const override function SendActionToAllSlaves( action : ScriptableDeviceAction )
	{
		var i : Int32;
		for( i = 0; i < m_slaves.Size(); i += 1 )
		{
			ExecutePSAction( action, m_slaves[ i ] );
		}
	}

}

class SurveillanceSystemUIPS extends VirtualSystemPS
{
	default m_deviceName = "LocKey#50770";

	public override function GetActions( out outActions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		super.GetActions( outActions, context );
		outActions.PushBack( ActionToggleTakeOverControl() );
		return true;
	}

	protected override function OnToggleTakeOverControl( evt : ToggleTakeOverControl ) : EntityNotificationType
	{
		var i : Int32;
		for( i = 0; i < m_slaves.Size(); i += 1 )
		{
			if( ( ( ScriptableDeviceComponentPS )( m_slaves[ i ] ) ).CanPlayerTakeOverControl() )
			{
				QueuePSEvent( m_slaves[ i ], evt );
				return EntityNotificationType.DoNotNotifyEntity;
			}
		}
		return EntityNotificationType.DoNotNotifyEntity;
	}

	protected override function GetBackgroundTextureTweakDBID() : TweakDBID
	{
		return T"DeviceIcons.CameraDeviceBackground";
	}

	protected override function GetDeviceIconTweakDBID() : TweakDBID
	{
		return T"DeviceIcons.CameraDeviceIcon";
	}

}

class SecuritySystemUIPS extends SurveillanceSystemUIPS
{
	default m_deviceName = "LocKey#50988";
}

class DoorSystemUIPS extends VirtualSystemPS
{
	private persistent var m_isOpen : Bool;

	public override function GetActions( out outActions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		outActions.PushBack( ActionToggleOpen() );
		return true;
	}

	public function ActionToggleOpen() : ToggleOpen
	{
		var action : ToggleOpen;
		action = new ToggleOpen;
		action.clearanceLevel = DefaultActionsParametersHolder.GetToggleOpenClearance();
		action.SetUp( this );
		action.SetProperties( m_isOpen );
		action.AddDeviceName( GetDeviceName() );
		action.CreateInteraction();
		action.CreateActionWidgetPackage();
		return action;
	}

	protected function OnToggleOpen( evt : ToggleOpen ) : EntityNotificationType
	{
		SendActionToAllSlaves( evt );
		return EntityNotificationType.DoNotNotifyEntity;
	}

}

class MediaSystemUIPS extends VirtualSystemPS
{

	public override function GetActions( out outActions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		return true;
	}

}

class CustomSystemUIPS extends VirtualSystemPS
{

	public function Initialize( const slaves : ref< array< DeviceComponentPS > >, owner : MasterControllerPS, systemName : CName, const actions : ref< array< CName > > )
	{
		Initialize( slaves, owner );
	}

}

