internal static void ResetData()
 {
     ifRefresh = true;
     bd = null;
     completedBuildsList = null;
     queuedBuildsList = null;
     completedBuildsBDId = "";
     queuedBuildsBDId = "";
 }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            SetDefaultUI();
            bdReceived = e.Parameter as BuildDefinition;
            bdTB.Text = bdReceived.Name;

            GetAndSetPItems();
        }
        private async void showQueueBuildContentDialog(BuildDefinition b)
        {
            QueuedBuild qb = new QueuedBuild();
            if (b == null) return;

            QueueBuildCB.DataContext = b;
            QueueBuildCB.HorizontalAlignment = HorizontalAlignment.Center;
            QueueBuildCB.VerticalAlignment = VerticalAlignment.Center;

            var result = await QueueBuildCB.ShowAsync();

            if (result == ContentDialogResult.Primary)
            {
                qb = queueBuild(b, "");

                if (!String.IsNullOrWhiteSpace(qb.BuildNumber))
                {
                    Utility.ShowToastMessage("Build queued successfully!", qb.BuildNumber);
                    ifRefresh = true;
                }
                else
                {
                    Utility.ShowToastMessage("Failed to queue build.", "Please retry later...");
                }
            }
        }
        private QueuedBuild queueBuild(BuildDefinition b, string branch)
        {
            bool success = false;
            QueuedBuild queuedB = new QueuedBuild();
            if (String.IsNullOrWhiteSpace(branch))
            {
                if(b.Repository != null)
                {
                    branch = b.Repository.DefaultBranch == null?"": b.Repository.DefaultBranch;
                }
            }

            success = VSTSService.QueueBuild(b.Id, branch, "", out queuedB);

            return queuedB;
        }
        private void GetAndSetPItems()
        {
            Pivot p = BuildP;
            if (p != null)
            {
                PivotItem pi = p.SelectedItem as PivotItem;
                if (pi == null) return;

                if (pi.Name.Equals("summaryPI"))
                {
                    if (bd == null ||  ifRefresh || !bd.Id.Equals(bdReceived.Id))
                    {
                        summarySP.Visibility = Visibility.Collapsed;
                        ShowProgressBar();
                        Task.Factory.StartNew(() =>
                        {
                            bd = VSTSService.GetABuildDefinition(bdReceived);
                        }).ContinueWith(async (Task t) =>
                        {
                            await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                            {
                                summaryPI.DataContext = bd;
                                HideProgressBar();

                                summarySP.Visibility = Visibility.Visible;
                            });
                        });
                    }
                    else
                    {
                        summaryPI.DataContext = bd;
                        summarySP.Visibility = Visibility.Visible;
                    }
                }
                else if (pi.Name.Equals("completedBuildsPI"))
                {
                    if (completedBuildsList == null || completedBuildsList.Count < 1 || ifRefresh || !bd.Id.Equals(completedBuildsBDId))
                    {
                        ShowProgressBar();
                        Task.Factory.StartNew(() =>
                        {
                            completedBuildsList = VSTSService.GetBuilds(bd);
                            completedBuildsBDId = bd.Id;
                        }).ContinueWith(async (Task t) =>
                        {
                            await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                            {
                                completedBuildsLV.ItemsSource = completedBuildsList;
                                HideProgressBar();
                            });
                        });
                    }
                    else
                    {
                        completedBuildsLV.ItemsSource = completedBuildsList;
                        HideProgressBar();
                    }
                }
                else if (pi.Name.Equals("queuedBuildsPI"))
                {
                    if (queuedBuildsList == null || queuedBuildsList.Count < 1 || ifRefresh || !bd.Id.Equals(queuedBuildsBDId))
                    {
                        ShowProgressBar();
                        Task.Factory.StartNew(() =>
                        {
                            queuedBuildsList = VSTSService.GetQueuedBuilds(bd);
                            queuedBuildsBDId = bd.Id;
                        }).ContinueWith(async (Task t) =>
                        {
                            await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                            {
                                queuedBuildsLV.ItemsSource = queuedBuildsList;
                                HideProgressBar();
                            });
                        });
                    }
                    else
                    {
                        queuedBuildsLV.ItemsSource = queuedBuildsList;
                        HideProgressBar();
                    }
                }

                ifRefresh = false;
            }
        }
        internal static List<Build> GetBuilds(BuildDefinition bd)
        {
            //int numDays = -1 * SettingsContext.GetSettingsContext().NumDaysToShowData;
            //buildAPIUrl = String.Format(Constants.API_BUILDS_QUEUED, LoginContext.GetLoginContext().VsoAccountUrl, ProjectContext.GetProjectContext().Project);
            string buildAPIUrl = String.Format(Constants.API_GET_BUILDS_COMPLETED_FOR_BD, LoginContext.GetLoginContext().VSTSAccountUrl, ProjectContext.GetProjectContext().Project, bd.Id);

            string responseBody = GetResponseFromGetRESTUri(buildAPIUrl);

            List<Build> buildsList = new List<Build>();
            if (responseBody != null)
            {
                buildsList = getBuildsFromJson(responseBody);
            }
            buildsList.Sort((a, b) => b.FinishTime.CompareTo(a.FinishTime));
            return buildsList;
        }
        internal static List<QueuedBuild> GetQueuedBuilds(BuildDefinition bd)
        {
            string buildAPIUrl = String.Format(Constants.API_GET_BUILDS_QUEUED_FOR_BD, LoginContext.GetLoginContext().VSTSAccountUrl, ProjectContext.GetProjectContext().Project, bd.Id);

            string responseBody = GetResponseFromGetRESTUri(buildAPIUrl);

            List<QueuedBuild> queuedBuildsList = new List<QueuedBuild>();

            if (responseBody != null)
            {
                queuedBuildsList = getQueuedBuildsFromJson(responseBody);
            }


            queuedBuildsList.Sort((a, b) => b.QueuedDate.CompareTo(a.QueuedDate));
            return queuedBuildsList;
        }
        public static BuildDefinition GetABuildDefinition(BuildDefinition bdRef)
        {
            if (bdRef == null || bdRef.Url == null || bdRef.Url.Equals(String.Empty))
            {
                return null;
            }

            string responseBody = GetResponseFromGetRESTUri(bdRef.Url);

            dynamic json = JsonConvert.DeserializeObject(responseBody);

            return getABuildDefinitionFromJson(json, false);
        }
        private static BuildDefinition getABuildDefinitionFromJson(dynamic bdBody, bool shallowReference = true)
        {
            if (bdBody == null) return null;
            BuildDefinition bd = new BuildDefinition
            {
                Id = bdBody.id,
                Name = bdBody.name,
                Url = bdBody.url,
                Type = bdBody.type,
            };

            if (!shallowReference)
            {
                bd.BuildNumberFormat = bdBody.buildNumberFormat;

                if (bdBody.createdDate != null)
                {
                    bd.CreatedDate = bdBody.createdDate;
                }

                dynamic identityJson = bdBody.authoredBy;
                if (identityJson != null)
                {
                    bd.AuthoredBy = getIdentityFronJson(identityJson);
                }

                dynamic repoJson = bdBody.repository;
                if (repoJson != null)
                {
                    bd.Repository = getRepoFromJson(repoJson);
                }

                dynamic queue = bdBody.queue;

                if (queue != null)
                {
                    bd.Queue = new Queue
                    {
                        Id = queue.id,
                        Name = queue.name
                    };
                }
            }
            return bd;
        }