Gallifrey.UI.Modern.Flyouts.AddTimer.AddButton C# (CSharp) Method

AddButton() private method

private AddButton ( object sender, RoutedEventArgs e ) : void
sender object
e System.Windows.RoutedEventArgs
return void
        private async void AddButton(object sender, RoutedEventArgs e)
        {
            if (!modelHelpers.Gallifrey.Settings.InternalSettings.IsPremium)
            {
                if (DataModel.LocalTimer)
                {
                    if (modelHelpers.Gallifrey.JiraTimerCollection.GetAllLocalTimers().Count() >= 2)
                    {
                        modelHelpers.ShowGetPremiumMessage("Without Gallifrey Premium You Are Limited To A Maximum Of 2 Local Timers");
                        Focus();
                        return;
                    }
                }
            }

            if (!DataModel.StartDate.HasValue)
            {
                await DialogCoordinator.Instance.ShowMessageAsync(modelHelpers.DialogContext, "Missing Date", "You Must Enter A Start Date");
                Focus();
                return;
            }

            if (DataModel.StartDate.Value < DataModel.MinDate || DataModel.StartDate.Value > DataModel.MaxDate)
            {
                await DialogCoordinator.Instance.ShowMessageAsync(modelHelpers.DialogContext, "Invalid Date", $"You Must Enter A Start Date Between {DataModel.MinDate.ToShortDateString()} And {DataModel.MaxDate.ToShortDateString()}");
                Focus();
                return;
            }

            TimeSpan seedTime;
            if (DataModel.TimeEditable)
            {
                seedTime = new TimeSpan(DataModel.StartHours ?? 0, DataModel.StartMinutes ?? 0, 0);
            }
            else
            {
                seedTime = new TimeSpan();
            }

            Issue jiraIssue = null;
            var jiraRef = string.Empty;
            
            if (!DataModel.LocalTimer)
            {
                try
                {
                    jiraRef = DataModel.JiraReference;
                    var jiraDownloadResult = await progressDialogHelper.Do(() => modelHelpers.Gallifrey.JiraConnection.GetJiraIssue(jiraRef), "Searching For Jira Issue", true, true);

                    if (jiraDownloadResult.Status == ProgressResult.JiraHelperStatus.Success)
                    {
                        jiraIssue = jiraDownloadResult.RetVal;
                    }
                    else
                    {
                        Focus();
                        return;
                    }
                }
                catch (NoResultsFoundException)
                {
                    await DialogCoordinator.Instance.ShowMessageAsync(modelHelpers.DialogContext, "Invalid Jira", $"Unable To Locate The Jira '{jiraRef}'");
                    Focus();
                    return;
                }

                if (DataModel.JiraReferenceEditable)
                {
                    var result = await DialogCoordinator.Instance.ShowMessageAsync(modelHelpers.DialogContext, "Correct Jira?", $"Jira found!\n\nRef: {jiraIssue.key}\nName: {jiraIssue.fields.summary}\n\nIs that correct?", MessageDialogStyle.AffirmativeAndNegative, new MetroDialogSettings { AffirmativeButtonText = "Yes", NegativeButtonText = "No", DefaultButtonFocus = MessageDialogResult.Affirmative });

                    if (result == MessageDialogResult.Negative)
                    {
                        Focus();
                        return;
                    }
                }
            }

            if (DataModel.DatePeriod)
            {
                AddedTimer = await AddPeriodTimer(jiraIssue, seedTime);
            }
            else
            {
                AddedTimer = await AddSingleTimer(jiraIssue, seedTime, DataModel.StartDate.Value);
            }

            if (!AddedTimer)
            {
                Focus();
                return;
            }

            var stillDoingThings = false;

            if (!DataModel.LocalTimer)
            {
                if (DataModel.AssignToMe)
                {
                    try
                    {
                        await progressDialogHelper.Do(() => modelHelpers.Gallifrey.JiraConnection.AssignToCurrentUser(jiraRef), "Assigning Issue", false, true);
                    }
                    catch (JiraConnectionException)
                    {
                        await DialogCoordinator.Instance.ShowMessageAsync(modelHelpers.DialogContext, "Assign Jira Error", "Unable To Locate Assign Jira To Current User");
                    }
                }

                if (DataModel.ChangeStatus)
                {
                    try
                    {
                        var transitionResult = await progressDialogHelper.Do(() => modelHelpers.Gallifrey.JiraConnection.GetTransitions(jiraRef), "Getting Transitions To Change Status", false, true);

                        if (transitionResult.Status == ProgressResult.JiraHelperStatus.Success)
                        {
                            var transitionsAvaliable = transitionResult.RetVal;
                            
                            var timeSelectorDialog = (BaseMetroDialog)this.Resources["TransitionSelector"];
                            await DialogCoordinator.Instance.ShowMetroDialogAsync(modelHelpers.DialogContext, timeSelectorDialog);

                            var comboBox = timeSelectorDialog.FindChild<ComboBox>("Items");
                            comboBox.ItemsSource = transitionsAvaliable.Select(x => x.name).ToList();

                            var messageBox = timeSelectorDialog.FindChild<TextBlock>("Message");
                            messageBox.Text = $"Please Select The Status Update You Would Like To Perform To {DataModel.JiraReference}";

                            stillDoingThings = true;
                        }
                    }
                    catch (Exception)
                    {
                        await DialogCoordinator.Instance.ShowMessageAsync(modelHelpers.DialogContext, "Status Update Error", "Unable To Change The Status Of This Issue");
                    }
                }
            }

            if (!stillDoingThings)
            {
                modelHelpers.CloseFlyout(this);
                modelHelpers.RefreshModel();
            }
        }