public void CleanUp()
 {
     ConnectCallbackSent = false;
     ChannelsAndChannelGroupsAwaitingConnectCallback.Clear();
     channelEntitiesDictionary.Clear();
     AllPresenceChannelsOrChannelGroups.Clear();
     AllNonPresenceChannelsOrChannelGroups.Clear();
     AllChannels.Clear();
     AllChannelGroups.Clear();
     AllSubscribedChannelsAndChannelGroups.Clear();
     CompiledUserState = String.Empty;
     CurrentSubscribedChannelsCount      = 0;
     CurrentSubscribedChannelGroupsCount = 0;
     HasChannelGroups           = false;
     HasChannels                = false;
     HasChannelsOrChannelGroups = false;
     HasPresenceChannels        = false;
 }
        public void ResetChannelsAndChannelGroupsAndBuildState()
        {
            AllPresenceChannelsOrChannelGroups.Clear();
            AllNonPresenceChannelsOrChannelGroups.Clear();
            ChannelsAndChannelGroupsAwaitingConnectCallback.Clear();
            AllChannels.Clear();
            AllChannelGroups.Clear();
            AllSubscribedChannelsAndChannelGroups.Clear();
            HasChannelGroups                    = false;
            HasChannels                         = false;
            HasPresenceChannels                 = false;
            HasChannelsOrChannelGroups          = false;
            CurrentSubscribedChannelsCount      = 0;
            CurrentSubscribedChannelGroupsCount = 0;

            foreach (var ci in channelEntitiesDictionary)
            {
                if (ci.Value.IsSubscribed)
                {
                    if (ci.Key.IsChannelGroup)
                    {
                        CurrentSubscribedChannelGroupsCount++;
                        AllChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value));
                    }
                    else
                    {
                        CurrentSubscribedChannelsCount++;
                        AllChannels.Add(new ChannelEntity(ci.Key, ci.Value));
                    }
                    AllSubscribedChannelsAndChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value));

                    if (ci.Key.IsPresenceChannel)
                    {
                        AllPresenceChannelsOrChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value));
                    }
                    else
                    {
                        AllNonPresenceChannelsOrChannelGroups.Add(new ChannelEntity(ci.Key, ci.Value));
                    }

                    if (ci.Value.IsAwaitingConnectCallback)
                    {
                        ChannelsAndChannelGroupsAwaitingConnectCallback.Add(new ChannelEntity(ci.Key, ci.Value));
                    }
                }
                #if (ENABLE_PUBNUB_LOGGING)
                this.PubNubInstance.PNLog.WriteToLog(string.Format("ResetChannelsAndChannelGroupsAndBuildState: channelEntities subscription key/val {0} {1}", ci.Key.ChannelOrChannelGroupName, ci.Value.IsSubscribed), PNLoggingMethod.LevelInfo);
                #endif
            }
            if (CurrentSubscribedChannelGroupsCount > 0)
            {
                HasChannelGroups = true;
            }
            if (CurrentSubscribedChannelsCount > 0)
            {
                HasChannels = true;
            }
            if (AllPresenceChannelsOrChannelGroups.Count > 0)
            {
                HasPresenceChannels = true;
            }
            if (HasChannels || HasChannelGroups)
            {
                HasChannelsOrChannelGroups = true;
            }
            #if (ENABLE_PUBNUB_LOGGING)
            this.PubNubInstance.PNLog.WriteToLog(string.Format("PrevCompiledUserState: {0}", CompiledUserState), PNLoggingMethod.LevelInfo);
            #endif

            CompiledUserState = Helpers.BuildJsonUserState(AllSubscribedChannelsAndChannelGroups);

            #if (ENABLE_PUBNUB_LOGGING)
            this.PubNubInstance.PNLog.WriteToLog(string.Format("CompiledUserState: {0}", CompiledUserState), PNLoggingMethod.LevelInfo);
            #endif
        }