enum IntercomStatus
{
	DEFAULT = 0,
	CALLING = 1,
	TALKING = 2,
	CALL_MISSED = 3,
	CALL_ENDED = 4,
}

class IntercomController extends ScriptableDeviceComponent
{

	public const override function GetPS() : IntercomControllerPS
	{
		return ( ( IntercomControllerPS )( GetBasePS() ) );
	}

}

class IntercomControllerPS extends ScriptableDeviceComponentPS
{
	default m_deviceName = "LocKey#163";
	default m_tweakDBRecord = T"Devices.Intercom";
	default m_tweakDBDescriptionRecord = T"device_descriptions.Intercom";
	protected var m_isCalling : Bool;
	protected var m_sceneStarted : Bool;
	protected var m_endingCall : Bool;
	private var m_forceLookAt : EntityID;
	private var m_forceFollow : Bool;

	protected event OnInstantiated()
	{
		super.OnInstantiated();
	}

	protected override function Initialize()
	{
		super.Initialize();
	}

	protected override function GameAttached() {}

	public override function GetActions( out actions : array< DeviceAction >, context : GetActionsContext ) : Bool
	{
		super.GetActions( actions, context );
		if( ( IsON() && !( m_isCalling ) ) && !( m_sceneStarted ) )
		{
			actions.PushBack( ActionStartCall() );
		}
		SetActionIllegality( actions, m_illegalActions.regularActions );
		return true;
	}

	public override function GetQuestActionByName( actionName : CName ) : DeviceAction
	{
		var action : DeviceAction;
		action = super.GetQuestActionByName( actionName );
		if( action == NULL )
		{
			switch( actionName )
			{
				case 'PickUpCall':
					action = ActionQuestPickUpCall();
				break;
				case 'HangUpCall':
					action = ActionQuestHangUpCall();
				break;
			}
		}
		return action;
	}

	public override function GetQuestActions( out actions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		super.GetQuestActions( actions, context );
		actions.PushBack( ActionQuestPickUpCall() );
		actions.PushBack( ActionQuestHangUpCall() );
	}

	protected const override function CanCreateAnyQuickHackActions() : Bool
	{
		return true;
	}

	protected override function GetQuickHackActions( out actions : array< DeviceAction >, const context : ref< GetActionsContext > )
	{
		var action : ScriptableDeviceAction;
		action = ActionQuickHackDistraction();
		action.SetObjectActionID( T"DeviceAction.MalfunctionClassHack" );
		action.SetDurationValue( GetDistractionDuration( action ) );
		action.SetInactiveWithReason( !( IsDistracting() ), "LocKey#7004" );
		actions.PushBack( action );
		FinalizeGetQuickHackActions( actions, context );
	}

	public quest const function CallStarted() : Bool
	{
		return m_isCalling;
	}

	protected function ActionStartCall() : StartCall
	{
		var action : StartCall;
		action = new StartCall;
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( GetDeviceName() );
		action.SetDurationValue( 6.0 );
		action.CreateActionWidgetPackage();
		return action;
	}

	public function OnStartCall( evt : StartCall ) : EntityNotificationType
	{
		UseNotifier( evt );
		if( evt.IsStarted() )
		{
			m_isCalling = true;
			m_forceFollow = true;
			m_forceLookAt = evt.GetExecutor().GetEntityID();
			RefreshSlaves_Event();
			ExecutePSActionWithDelay( evt, this, evt.GetDurationValue() );
		}
		else
		{
			if( !( m_sceneStarted ) )
			{
				m_forceFollow = false;
				RefreshSlaves_Event();
				ExecutePSActionWithDelay( ActionResetIntercom(), this, 2.0 );
			}
			else
			{
				return EntityNotificationType.DoNotNotifyEntity;
			}
		}
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected function ActionQuestPickUpCall() : QuestPickUpCall
	{
		var action : QuestPickUpCall;
		action = new QuestPickUpCall;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public function OnQuestPickUpCall( evt : QuestPickUpCall ) : EntityNotificationType
	{
		m_sceneStarted = true;
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected function ActionQuestHangUpCall() : QuestHangUpCall
	{
		var action : QuestHangUpCall;
		action = new QuestHangUpCall;
		action.clearanceLevel = DefaultActionsParametersHolder.GetQuestClearance();
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public function OnQuestHangUpCall( evt : QuestHangUpCall ) : EntityNotificationType
	{
		m_forceFollow = false;
		RefreshSlaves_Event();
		UseNotifier( evt );
		ExecutePSActionWithDelay( ActionResetIntercom(), this, 2.0 );
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected function ActionResetIntercom() : DelayEvent
	{
		var delayEvent : DelayEvent;
		delayEvent = new DelayEvent;
		delayEvent.SetUp( this );
		delayEvent.SetProperties();
		delayEvent.AddDeviceName( m_deviceName );
		return delayEvent;
	}

	public function OnResetIntercom( evt : DelayEvent ) : EntityNotificationType
	{
		m_isCalling = false;
		m_sceneStarted = false;
		UseNotifier( evt );
		return EntityNotificationType.SendThisEventToEntity;
	}

	protected function ActionForceFollowTarget() : QuestLookAtTarget
	{
		var action : QuestLookAtTarget;
		action = new QuestLookAtTarget;
		action.SetUp( this );
		action.SetPropertiesFromScripts( m_forceLookAt );
		action.AddDeviceName( m_deviceName );
		return action;
	}

	protected function ActionStopFollowingTarget() : QuestStopLookAtTarget
	{
		var action : QuestStopLookAtTarget;
		action = new QuestStopLookAtTarget;
		action.SetUp( this );
		action.SetProperties();
		action.AddDeviceName( m_deviceName );
		return action;
	}

	public const function GetImmediateSlaves() : array< DeviceComponentPS >
	{
		var entityID : EntityID;
		var immediateSlaves : array< DeviceComponentPS >;
		entityID = PersistentID.ExtractEntityID( GetID() );
		GameInstance.GetDeviceSystem( GetGameInstance() ).GetChildren( entityID, immediateSlaves );
		return immediateSlaves;
	}

	public function RefreshSlaves_Event()
	{
		var evt : RefreshSlavesEvent;
		evt = new RefreshSlavesEvent;
		GetPersistencySystem().QueuePSEvent( GetID(), GetClassName(), evt );
	}

	protected function OnRefreshSlavesEvent( evt : RefreshSlavesEvent ) : EntityNotificationType
	{
		RefreshSlaves();
		return EntityNotificationType.DoNotNotifyEntity;
	}

	private function RefreshSlaves()
	{
		var i : Int32;
		var devices : array< DeviceComponentPS >;
		devices = GetImmediateSlaves();
		for( i = 0; i < devices.Size(); i = i + 1 )
		{
			if( ( ( SurveillanceCameraControllerPS )( devices[ i ] ) ) )
			{
				if( m_forceFollow )
				{
					ExecutePSAction( ActionForceFollowTarget(), devices[ i ] );
				}
				else
				{
					ExecutePSAction( ActionStopFollowingTarget(), devices[ i ] );
				}
			}
		}
	}

	public const override function GetBlackboardDef() : IntercomBlackboardDef
	{
		return GetAllBlackboardDefs().IntercomBlackboard;
	}

}

