/// <summary>
        /// Gets the notifications state text representation.
        /// </summary>
        /// <param name="notificationsState">The notification state.</param>
        /// <returns>The text representation for list of <see cref="NotificationStateItem"/>.</returns>
        private string GetNotificationsStateTextRepresentation(NotificationsState notificationsState)
        {
            if (notificationsState.UseGlobalSettings.IsActive)
            {
                return("use main settings"); // TODO resource
            }

            var result = string.Join(", ", notificationsState.ItemsState.Where(n => n.IsActive).Select(n => n.Caption));

            if (notificationsState.OnlyIfStateChanged.IsActive)
            {
                if (!string.IsNullOrWhiteSpace(result))
                {
                    result += ", ";
                }

                result += notificationsState.OnlyIfStateChanged.Caption;
            }

            if (string.IsNullOrWhiteSpace(result))
            {
                return("none"); // TODO resource
            }

            return(result);
        }
 private void AssignPropertyChangeOfStateItems(NotificationsState notificationsState, IConfigurationItem configItem)
 {
     notificationsState.UseGlobalSettings.PropertyChanged  += (s, e) => this.NotificationStateChanged(notificationsState.UseGlobalSettings, configItem, e.PropertyName);
     notificationsState.OnlyIfStateChanged.PropertyChanged += (s, e) => this.NotificationStateChanged(notificationsState.OnlyIfStateChanged, configItem, e.PropertyName);
     foreach (var notificationStateItem in notificationsState.ItemsState)
     {
         notificationStateItem.PropertyChanged += (s, e) => this.NotificationStateChanged(notificationStateItem, configItem, e.PropertyName);
     }
 }
        /// <summary>
        /// Creates a new control and returns it.
        /// </summary>
        /// <param name="configItem">The configuration item.</param>
        /// <param name="configItemAttribute">The configuration item attribute.</param>
        /// <returns>
        /// Returns the <see cref="Control" /> for the <see cref="configItem" />.
        /// </returns>
        public override FrameworkElement GetControlInternal(IConfigurationItem configItem, ConfigurationItemAttribute configItemAttribute)
        {
            var card = new Card();

            card.Margin = new Thickness(4, 0, 4, 0);
            card.SetResourceReference(Control.BackgroundProperty, "MaterialDesignBackground");
            var expander = new Expander();

            card.Content = expander;
            expander.HorizontalAlignment = HorizontalAlignment.Stretch;
            var itemsStackPanel = new StackPanel();

            itemsStackPanel.Orientation = Orientation.Horizontal;
            expander.Content            = itemsStackPanel;
            var itemsStackPanelColumn1 = new StackPanel();

            itemsStackPanelColumn1.Margin = new Thickness(0, 0, 50, 0);
            var itemsStackPanelColumn2 = new StackPanel();

            itemsStackPanel.Children.Add(itemsStackPanelColumn1);
            itemsStackPanel.Children.Add(itemsStackPanelColumn2);

            var notificationState = new NotificationsState();

            this.ApplyConfigurationItemToNotificationState(notificationState, configItem, configItemAttribute, expander);
            var configItemPropertyChanged = (INotifyPropertyChanged)configItem;

            configItemPropertyChanged.PropertyChanged += (s, e) => this.ApplyConfigurationItemToNotificationState(notificationState, configItem, configItemAttribute, expander);

            foreach (var notificationStateItem in notificationState.ItemsState)
            {
                var stackPanelForToggleButton = this.CreateControlForNotificationStateItem(notificationStateItem);
                itemsStackPanelColumn1.Children.Add(stackPanelForToggleButton);
            }

            var stackPanelForUseGlobalToggleButton = this.CreateControlForNotificationStateItem(notificationState.UseGlobalSettings, false);

            itemsStackPanelColumn2.Children.Add(stackPanelForUseGlobalToggleButton);

            var stackPanelForOnlyIfStateChangedToggleButton = this.CreateControlForNotificationStateItem(notificationState.OnlyIfStateChanged);

            itemsStackPanelColumn2.Children.Add(stackPanelForOnlyIfStateChangedToggleButton);

            this.AssignPropertyChangeOfStateItems(notificationState, configItem);
            this.UpdateExpanderHeader(expander, notificationState);
            return(card);
        }
        /// <summary>
        /// Applies the <see cref="IConfigurationItem" />to <see cref="NotificationsState" />s.
        /// </summary>
        /// <param name="notificationsState">The notification state.</param>
        /// <param name="configItem">The configuration item.</param>
        /// <param name="configItemAttribute">The configuration item attribute.</param>
        /// <param name="expander">The expander control.</param>
        private void ApplyConfigurationItemToNotificationState(NotificationsState notificationsState, IConfigurationItem configItem, ConfigurationItemAttribute configItemAttribute, Expander expander)
        {
            var configItemValue   = configItem.GetOrCreateConnectorNotificationConfiguration();
            var observationStates = Enum.GetValues(typeof(ObservationState)).Cast <ObservationState>().ToList();

            if (configItemAttribute is NotificationConfigurationItemAttribute notificationConfigurationItemAttribute)
            {
                this.RemoveNotSupportedObservationStates(observationStates, notificationConfigurationItemAttribute);
            }

            var useGlobalSettingsNewState = configItemValue.UseGlobalNotificationSettings;

            if (useGlobalSettingsNewState != notificationsState.UseGlobalSettings.IsActive)
            {
                notificationsState.UseGlobalSettings.IsActive = useGlobalSettingsNewState;
            }

            var onlyIfStateChangedSettingsNewState = configItemValue.OnlyIfChanged;

            if (onlyIfStateChangedSettingsNewState != notificationsState.OnlyIfStateChanged.IsActive)
            {
                notificationsState.OnlyIfStateChanged.IsActive = onlyIfStateChangedSettingsNewState;
            }

            foreach (var observationState in observationStates)
            {
                var notificationStateItem = notificationsState.ItemsState.FirstOrDefault(n => n.ObservationState == observationState);
                if (notificationStateItem == null)
                {
                    notificationStateItem                  = new NotificationStateItem(notificationsState);
                    notificationStateItem.IsActive         = configItemValue.AsObservationStateFlag(observationState);
                    notificationStateItem.ObservationState = observationState;
                    notificationStateItem.Caption          = observationState.ToString(); // TODO load from resources
                    notificationStateItem.PropertyChanged += (s, e) => this.NotificationStateChanged(notificationStateItem, configItem, e.PropertyName);
                    notificationStateItem.PropertyChanged += (s, e) => this.UpdateExpanderHeader(expander, notificationsState);
                    notificationsState.ItemsState.Add(notificationStateItem);
                }
                else
                {
                    var newActiveState = configItemValue.UseGlobalNotificationSettings;
                    if (notificationStateItem.IsActive != newActiveState)
                    {
                        notificationStateItem.IsActive = configItemValue.AsObservationStateFlag(observationState);
                    }
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="NotificationStateItem"/> class.
 /// </summary>
 /// <param name="notificationsState">The notification state.</param>
 public NotificationStateItem(NotificationsState notificationsState)
 {
     this.NotificationsState = notificationsState;
 }
 /// <summary>
 /// Updates the expander header.
 /// </summary>
 /// <param name="expander">The expander.</param>
 /// <param name="notificationsState">The notification state.</param>
 private void UpdateExpanderHeader(Expander expander, NotificationsState notificationsState)
 {
     expander.Header = $"Notification settings ({this.GetNotificationsStateTextRepresentation(notificationsState)})"; // TODO resources
 }