/// <summary>
 /// Event handler for a completed HTTP poll
 /// Will update GUI for the CM modified
 /// </summary>
 /// <param name="sender">Sender of event, expected to be null</param>
 /// <param name="e">CmHelperEventArgs with relevant details</param>
 private void OnPollComplete(object sender, CmHelperEventArgs e)
 {
     try
     {
         // This VM is not the sender, return
         if (GetHashCode() != e.SubscriberHashCode)
         {
             return;
         }
         // Set CM Status
         if (CmsDisplayExec.Any(c => c.Id == e.Cm.Id))
         {
             CmsDisplayExec.Single(c => c.Id == e.Cm.Id).CmStatus = e.CmStatus == HttpStatusCode.OK ? CmStates.Alive : CmStates.Offline;
         }
         else if (CmsDisplayOperDb.Any(c => c.Id == e.Cm.Id))
         {
             CmsDisplayOperDb.Single(c => c.Id == e.Cm.Id).CmStatus = e.CmStatus == HttpStatusCode.OK ? CmStates.Alive : CmStates.Offline;
         }
         else if (CmsDisplayOperAppOne.Any(c => c.Id == e.Cm.Id))
         {
             CmsDisplayOperAppOne.Single(c => c.Id == e.Cm.Id).CmStatus = e.CmStatus == HttpStatusCode.OK ? CmStates.Alive : CmStates.Offline;
         }
         else if (CmsDisplayOperAppTwo.Any(c => c.Id == e.Cm.Id))
         {
             CmsDisplayOperAppTwo.Single(c => c.Id == e.Cm.Id).CmStatus = e.CmStatus == HttpStatusCode.OK ? CmStates.Alive : CmStates.Offline;
         }
         else if (CmsDisplayMs.Any(c => c.Id == e.Cm.Id))
         {
             CmsDisplayMs.Single(c => c.Id == e.Cm.Id).CmStatus = e.CmStatus == HttpStatusCode.OK ? CmStates.Alive : CmStates.Offline;
         }
     }
     catch (Exception ex)
     {
         LogAndReportException(ex, $"Error updating VIBES Swap GUI with HTTP status: {ex.Message}", false);
     }
 }
        /// <summary>
        /// Event handler for a completed SSH command
        /// Will update GUI for the CM changed
        /// </summary>
        /// <param name="sender">Sender of event, expected to be null</param>
        /// <param name="e">CmHelperEventArgs with relevant details</param>
        private void OnCmCommandComplete(object sender, CmHelperEventArgs e)
        {
            try
            {
                // This VM is not the sender, return
                if (e.SubscriberHashCode != GetHashCode())
                {
                    return;
                }

                // Set CM Status
                if (CmsDisplayExec.Contains(e.CmChanged))
                {
                    switch (e.CmStatus)
                    {
                    case HttpStatusCode.OK:
                        CmsDisplayExec.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Alive;
                        break;

                    case HttpStatusCode.ServiceUnavailable:
                        CmsDisplayExec.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Offline;
                        break;

                    case HttpStatusCode.NoContent:
                        CmsDisplayExec.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Altered;
                        break;

                    case HttpStatusCode.Continue:
                        CmsDisplayExec.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Updated;
                        break;

                    default:
                        CmsDisplayExec.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Unchecked;
                        PollCmAsync(CmsDisplayExec.Single(c => c.Id == e.CmChanged.Id));
                        break;
                    }
                }
                else if (CmsDisplayOperDb.Contains(e.CmChanged))
                {
                    switch (e.CmStatus)
                    {
                    case HttpStatusCode.OK:
                        CmsDisplayOperDb.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Alive;
                        break;

                    case HttpStatusCode.ServiceUnavailable:
                        CmsDisplayOperDb.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Offline;
                        break;

                    case HttpStatusCode.NoContent:
                        CmsDisplayOperDb.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Altered;
                        break;

                    case HttpStatusCode.Continue:
                        CmsDisplayOperDb.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Updated;
                        break;

                    default:
                        CmsDisplayOperDb.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Unchecked;
                        PollCmAsync(CmsDisplayOperDb.Single(c => c.Id == e.CmChanged.Id));
                        break;
                    }
                }
                else if (CmsDisplayOperAppOne.Contains(e.CmChanged))
                {
                    switch (e.CmStatus)
                    {
                    case HttpStatusCode.OK:
                        CmsDisplayOperAppOne.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Alive;
                        break;

                    case HttpStatusCode.ServiceUnavailable:
                        CmsDisplayOperAppOne.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Offline;
                        break;

                    case HttpStatusCode.NoContent:
                        CmsDisplayOperAppOne.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Altered;
                        break;

                    case HttpStatusCode.Continue:
                        CmsDisplayOperAppOne.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Updated;
                        break;

                    default:
                        CmsDisplayOperAppOne.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Unchecked;
                        PollCmAsync(CmsDisplayOperAppOne.Single(c => c.Id == e.CmChanged.Id));
                        break;
                    }
                }
                else if (CmsDisplayOperAppTwo.Contains(e.CmChanged))
                {
                    switch (e.CmStatus)
                    {
                    case HttpStatusCode.OK:
                        CmsDisplayOperAppTwo.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Alive;
                        break;

                    case HttpStatusCode.ServiceUnavailable:
                        CmsDisplayOperAppTwo.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Offline;
                        break;

                    case HttpStatusCode.NoContent:
                        CmsDisplayOperAppTwo.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Altered;
                        break;

                    case HttpStatusCode.Continue:
                        CmsDisplayOperAppTwo.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Updated;
                        break;

                    default:
                        CmsDisplayOperAppTwo.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Unchecked;
                        PollCmAsync(CmsDisplayOperAppTwo.Single(c => c.Id == e.CmChanged.Id));
                        break;
                    }
                }
                else if (CmsDisplayMs.Contains(e.CmChanged))
                {
                    switch (e.CmStatus)
                    {
                    case HttpStatusCode.OK:
                        CmsDisplayMs.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Alive;
                        break;

                    case HttpStatusCode.ServiceUnavailable:
                        CmsDisplayMs.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Offline;
                        break;

                    case HttpStatusCode.NoContent:
                        CmsDisplayMs.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Altered;
                        break;

                    case HttpStatusCode.Continue:
                        CmsDisplayMs.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Updated;
                        break;

                    default:
                        CmsDisplayMs.Single(c => c.Id == e.CmChanged.Id).CmStatus = CmStates.Unchecked;
                        PollCmAsync(CmsDisplayMs.Single(c => c.Id == e.CmChanged.Id));
                        break;
                    }
                }

                // Update properties
                if (e.DeploymentProperties != null && (e.Host.HostType == HostTypes.EXEC || e.Host.HostType == HostTypes.OPERDB || e.Host.HostType == HostTypes.OPERAPP1 || e.Host.HostType == HostTypes.OPERAPP2 || e.Host.HostType == HostTypes.MS))
                {
                    StoreDeploymentProperties(e.CmChanged, e.DeploymentProperties);
                    Application.Current.Dispatcher.Invoke(delegate
                    {
                        LoadData(e.CmChanged);
                    });
                }

                // Popup hosts
                if (e.HostsFile != null && e.Host != null && (e.Host.HostType == HostTypes.EXEC || e.Host.HostType == HostTypes.OPERAPP1 || e.Host.HostType == HostTypes.OPERAPP2))
                {
                    PopupHostsFile(e);
                }
            }
            catch (Exception ex)
            {
                LogAndReportException(ex, $"Error updating Vibes CM swap GUI with CM changes: {ex.Message}", false);
            }
        }
        /// <summary>
        /// Load data from database
        /// </summary>
        /// <param name="parameter">Not used, only for GUI binding</param>
        private void LoadData(object parameter)
        {
            try
            {
                // Single CM is updated, only update that one CM
                using (DataContext context = new DataContext())
                {
                    if (parameter is VibesCm cmChanged)
                    {
                        VibesCm newCm = context.HostCms.Where(c => c.Id == cmChanged.Id).Include(c => c.DeploymentProperties).Include(c => c.VibesHost).FirstOrDefault().DeepCopy();
                        newCm.CmStatus = CmStates.Updated;

                        if (CmsDisplayExec.Contains(cmChanged))
                        {
                            CmsDisplayExec.Remove(CmsDisplayExec.Single(c => c.Id == cmChanged.Id));
                            CmsDisplayExec.Add(newCm);
                            SelectedCmExec   = newCm;
                            SelectedHostExec = HostsDisplayExec.SingleOrDefault(h => h.Id == newCm.VibesHost.Id);
                        }
                        if (CmsDisplayOperDb.Contains(cmChanged))
                        {
                            CmsDisplayOperDb.Remove(CmsDisplayOperDb.Single(c => c.Id == cmChanged.Id));
                            CmsDisplayOperDb.Add(newCm);
                            SelectedCmOperDb   = newCm;
                            SelectedHostOperDb = HostsDisplayOperDb.SingleOrDefault(h => h.Id == newCm.VibesHost.Id);
                        }
                        if (CmsDisplayOperAppOne.Contains(cmChanged))
                        {
                            CmsDisplayOperAppOne.Remove(CmsDisplayOperAppOne.Single(c => c.Id == cmChanged.Id));
                            CmsDisplayOperAppOne.Add(newCm);
                            SelectedCmOperAppOne   = newCm;
                            SelectedHostOperAppOne = HostsDisplayOperAppOne.SingleOrDefault(h => h.Id == newCm.VibesHost.Id);
                        }
                        if (CmsDisplayOperAppTwo.Contains(cmChanged))
                        {
                            CmsDisplayOperAppTwo.Remove(CmsDisplayOperAppTwo.Single(c => c.Id == cmChanged.Id));
                            CmsDisplayOperAppTwo.Add(newCm);
                            SelectedCmOperAppTwo   = newCm;
                            SelectedHostOperAppTwo = HostsDisplayOperAppTwo.SingleOrDefault(h => h.Id == newCm.VibesHost.Id);
                        }
                        if (CmsDisplayMs.Contains(cmChanged))
                        {
                            CmsDisplayMs.Remove(CmsDisplayMs.Single(c => c.Id == cmChanged.Id));
                            CmsDisplayMs.Add(newCm);
                            SelectedCmMs   = newCm;
                            SelectedHostMs = HostsDisplayMs.SingleOrDefault(h => h.Id == newCm.VibesHost.Id);
                        }
                        return;
                    }
                }

                // Initial Load
                using (DataContext context = new DataContext())
                {
                    HostsDisplayExec.Clear();
                    CmsDisplayExec.Clear();
                    if (context.EnvironmentHosts.Any(h => h.HostType == HostTypes.EXEC))
                    {
                        foreach (VibesHost host in context.EnvironmentHosts.Where(h => h.HostType == HostTypes.EXEC).OrderBy(h => h.Name))
                        {
                            VibesHost newHost = host.DeepCopy();
                            HostsDisplayExec.Add(newHost);
                        }
                        SelectedHostExec = HostsDisplayExec.FirstOrDefault();
                    }
                    HostsDisplayOperDb.Clear();
                    CmsDisplayOperDb.Clear();
                    if (context.EnvironmentHosts.Any(h => h.HostType == HostTypes.OPERDB))
                    {
                        foreach (VibesHost host in context.EnvironmentHosts.Where(h => h.HostType == HostTypes.OPERDB).OrderBy(h => h.Name))
                        {
                            VibesHost newHost = host.DeepCopy();
                            HostsDisplayOperDb.Add(newHost);
                        }
                        SelectedHostOperDb = HostsDisplayOperDb.FirstOrDefault();
                    }
                    HostsDisplayOperAppOne.Clear();
                    CmsDisplayOperAppOne.Clear();
                    if (context.EnvironmentHosts.Any(h => h.HostType == HostTypes.OPERAPP1))
                    {
                        foreach (VibesHost host in context.EnvironmentHosts.Where(h => h.HostType == HostTypes.OPERAPP1).OrderBy(h => h.Name))
                        {
                            VibesHost newHost = host.DeepCopy();
                            HostsDisplayOperAppOne.Add(newHost);
                        }
                        SelectedHostOperAppOne = HostsDisplayOperAppOne.FirstOrDefault();
                    }
                    HostsDisplayOperAppTwo.Clear();
                    CmsDisplayOperAppTwo.Clear();
                    if (context.EnvironmentHosts.Any(h => h.HostType == HostTypes.OPERAPP2))
                    {
                        foreach (VibesHost host in context.EnvironmentHosts.Where(h => h.HostType == HostTypes.OPERAPP2).OrderBy(h => h.Name))
                        {
                            VibesHost newHost = host.DeepCopy();
                            HostsDisplayOperAppTwo.Add(newHost);
                        }
                        SelectedHostOperAppTwo = HostsDisplayOperAppTwo.FirstOrDefault();
                    }
                    HostsDisplayMs.Clear();
                    CmsDisplayMs.Clear();
                    if (context.EnvironmentHosts.Any(h => h.HostType == HostTypes.MS))
                    {
                        foreach (VibesHost host in context.EnvironmentHosts.Where(h => h.HostType == HostTypes.MS).OrderBy(h => h.Name))
                        {
                            VibesHost newHost = host.DeepCopy();
                            HostsDisplayMs.Add(newHost);
                        }
                        SelectedHostMs = HostsDisplayMs.FirstOrDefault();
                    }
                }
            }
            catch (Exception ex)
            {
                LogAndReportException(ex, $"Error loading VIBES host's from DB: {ex.Message}", true);
            }
        }