protected int FindIndexOfPlayerWFStateType(PlayerWFStateType type)
 {
     lock (SyncObj)
         for (int i = 0; i < _playerWfStateInstances.Count; i++)
         {
             PlayerWFStateInstance wfStateInstance = _playerWfStateInstances[i];
             if (wfStateInstance.WFStateType == type)
             {
                 return(i);
             }
         }
     return(-1);
 }
 /// <summary>
 /// Check if one of our tracked workflow states has the given <paramref name="type"/>. If yes, go out of that state.
 /// </summary>
 protected void StepOutOfPlayerWFState(PlayerWFStateType type)
 {
     for (int i = 0; i < _playerWfStateInstances.Count; i++)
     {
         PlayerWFStateInstance wfStateInstance = _playerWfStateInstances[i];
         if (wfStateInstance.WFStateType == type)
         { // Found FSC state - step out of it
             ServiceRegistration.Get <ILogger>().Debug("PlayerContextManager: Leaving {0} Workflow State '{1}'", type, wfStateInstance.WFStateId);
             lock (SyncObj)
                 // Remove all workflow states until the removed player workflow state
                 _playerWfStateInstances.RemoveRange(i, _playerWfStateInstances.Count - i);
             ServiceRegistration.Get <IWorkflowManager>().NavigatePopToStateAsync(wfStateInstance.WFStateId, true);
             return;
         }
     }
 }
 protected void HandleStatesRemovedFromWorkflowStack(ICollection <Guid> statesRemoved)
 {
     lock (SyncObj)
     {
         // If one of our remembered player workflow states was removed from workflow navigation stack,
         // take it from our player workflow state cache. Our player workflow state cache is in the same order
         // as the workflow navigation stack, so if we tracked everything correctly, each removal of states should
         // remove states from the end of our cache.
         for (int i = 0; i < _playerWfStateInstances.Count; i++)
         {
             PlayerWFStateInstance wfStateInstance = _playerWfStateInstances[i];
             if (statesRemoved.Contains(wfStateInstance.WFStateId))
             {
                 _playerWfStateInstances.RemoveRange(i, _playerWfStateInstances.Count - i);
                 break;
             }
         }
     }
 }
        /// <summary>
        /// Checks if our "currently playing" and "fullscreen content" states still fit to the
        /// appropriate players, i.e. if we are in a "currently playing" state and the current player context was
        /// changed, the workflow state will be adapted to match the new current player context's "currently playing" state.
        /// The same check will happen for the primary player context and the "fullscreen content" state.
        /// </summary>
        /// <remarks>
        /// This method must not be called when the player manager's lock is held.
        /// </remarks>
        protected void CheckMediaWorkflowStates_NoLock()
        {
            ISystemStateService sss = ServiceRegistration.Get <ISystemStateService>();

            if (sss.CurrentState != SystemState.Running)
            {
                // Only automatically change workflow states in running state
                return;
            }
            IWorkflowManager workflowManager = ServiceRegistration.Get <IWorkflowManager>();

            workflowManager.StartBatchUpdateAsync();
            ILogger        log           = ServiceRegistration.Get <ILogger>();
            IPlayerManager playerManager = ServiceRegistration.Get <IPlayerManager>();

            try
            {
                for (int i = 0; i < _playerWfStateInstances.Count; i++)
                {
                    // Find the first workflow state of our cached player workflow states which doesn't fit any more
                    // and update to the new player workflow state of the same player workflow state type, if necessary.
                    PlayerWFStateInstance wfStateInstance = _playerWfStateInstances[i];
                    Guid?  newStateId;
                    string stateName;
                    switch (wfStateInstance.WFStateType)
                    {
                    case PlayerWFStateType.CurrentlyPlaying:
                        newStateId = GetPotentialCPStateId();
                        stateName  = "Currently Playing";
                        break;

                    case PlayerWFStateType.FullscreenContent:
                        newStateId = GetPotentialFSCStateId();
                        stateName  = "Fullscreen Content";
                        break;

                    default:
                        throw new NotImplementedException(string.Format("No handler for player workflow state type '{0}'",
                                                                        wfStateInstance.WFStateType));
                    }
                    if (newStateId != wfStateInstance.WFStateId)
                    {
                        // Found the first player workflow state which doesn't fit any more
                        log.Debug("PlayerContextManager: {0} Workflow State '{1}' doesn't fit any more to the current situation. Leaving workflow state.",
                                  stateName, wfStateInstance.WFStateId);
                        lock (playerManager.SyncObj)
                            // Remove all workflow states until the player workflow state which doesn't fit any more
                            _playerWfStateInstances.RemoveRange(i, _playerWfStateInstances.Count - i);
                        workflowManager.NavigatePopToStateAsync(wfStateInstance.WFStateId, true);
                        if (newStateId.HasValue)
                        {
                            log.Debug("PlayerContextManager: Auto-switching to new {0} Workflow State '{1}'",
                                      stateName, newStateId.Value);
                            workflowManager.NavigatePushAsync(newStateId.Value);
                        }
                        break;
                    }
                }
            }
            finally
            {
                workflowManager.EndBatchUpdateAsync();
            }
        }