public RankingWindow()
 {
     InitializeComponent();
     CbbVersion.ItemsSource        = ApiRequester.Get <IEnumerable <Models.RankingVersionPivot> >($"/Ranking/{GType()}");
     DtpStartDate.DisplayDateStart = OPEN_ERA_BEGIN;
     DtpStartDate.DisplayDateEnd   = DateTime.Today;
     DtpStartDate.SelectedDate     = OPEN_ERA_BEGIN;
     DtpEndDate.DisplayDateStart   = OPEN_ERA_BEGIN;
     DtpEndDate.DisplayDateEnd     = DateTime.Today;
     DtpEndDate.SelectedDate       = DateTime.Today;
     CbbSpeed.SelectedIndex        = 1;
     LblCurrentDate.Content        = "Current date : animation is not running.";
 }
        private void Bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            var arguments = e.Argument as object[];

            var versionId   = Convert.ToUInt32(arguments[0]);
            var currentDate = (DateTime)arguments[1];
            var endDate     = (DateTime)arguments[2];

            while (currentDate < endDate)
            {
                var flagDate = DateTime.Now;
                var ranking  = ApiRequester.Get <IEnumerable <Models.RankingPivot> >($"/Ranking/{GType()}/{versionId}/{currentDate.ToString("yyyy-MM-dd")}/{TOP_RANKING}");
                (sender as BackgroundWorker).ReportProgress(0, new object[] { ranking, currentDate });
                var timeSpan            = Convert.ToInt32(Math.Floor((DateTime.Now - flagDate).TotalMilliseconds));
                int referenceTimeElapse = GetSpeedDelay(_speed);
                if (timeSpan < referenceTimeElapse)
                {
                    System.Threading.Thread.Sleep(referenceTimeElapse - timeSpan);
                }
                currentDate = currentDate.AddDays(7);
            }
        }
        private void BtnGenerate_Click(object sender, RoutedEventArgs e)
        {
            PgbGenerate.Visibility = Visibility.Visible;
            BtnGenerate.IsEnabled  = false;
            BtnImport.IsEnabled    = false;
            BtnSaveToJpg.IsEnabled = false;

            BackgroundWorker worker = new BackgroundWorker
            {
                WorkerReportsProgress = true
            };

            worker.DoWork += delegate(object w, DoWorkEventArgs evt)
            {
                uint total = YEAR_END - YEAR_BEGIN;
                uint count = 0;
                for (uint year = YEAR_BEGIN; year <= YEAR_END; year++)
                {
                    ApiRequester.Get <IEnumerable <Models.MatchPivot> >($"/Match/{Gtype()}/{year}/true");
                    count++;
                    (w as BackgroundWorker).ReportProgress((int)Math.Floor((count / (double)total) * 100));
                }
            };
            worker.ProgressChanged += delegate(object w, ProgressChangedEventArgs evt)
            {
                PgbGenerate.Value = evt.ProgressPercentage;
            };
            worker.RunWorkerCompleted += delegate(object w, RunWorkerCompletedEventArgs evt)
            {
                var withRunnerUp = ChkWithRunnerUp.IsChecked == true;

                GrdChan.RowDefinitions.Add(new RowDefinition {
                    Height = new GridLength(DEFAULT_BLOCK_SIZE)
                });
                GrdChan.ColumnDefinitions.Add(new ColumnDefinition {
                    Width = new GridLength(DEFAULT_BLOCK_SIZE)
                });

                var editionsInRangeBase = ApiRequester.Get <IEnumerable <Models.EditionPivot> >($"/Edition/{Gtype()}/{YEAR_BEGIN}/{YEAR_END}");
                var editionsInRange     = editionsInRangeBase.Where(me => displayedLevels.Contains(me.Level.Code));

                var slotsBase = ApiRequester.Get <IEnumerable <Models.SlotPivot> >($"/Slot/{Gtype()}");
                var slots     = slotsBase
                                .Where(me => editionsInRange.Any(you => you.Slot?.Id == me.Id))
                                .OrderBy(me => me.Level.DisplayOrder)
                                .ThenBy(me => me.DisplayOrder);

                int    column           = 1;
                string currentLevelName = null;
                foreach (var slot in slots)
                {
                    if (currentLevelName == null)
                    {
                        currentLevelName = slot.Level.Name;
                    }
                    else if (currentLevelName != slot.Level.Name)
                    {
                        GrdChan.ColumnDefinitions.Add(new ColumnDefinition {
                            Width = new GridLength(DEFAULT_BLOCK_SIZE / 5)
                        });
                        AddBlock(0, column, string.Empty, Brushes.DarkGray);
                        column++;
                        currentLevelName = slot.Level.Name;
                    }

                    GrdChan.ColumnDefinitions.Add(new ColumnDefinition {
                        Width = new GridLength(DEFAULT_BLOCK_SIZE)
                    });

                    // Header slot
                    AddBlock(0, column, string.Concat(slot.Name, "(", slot.Level.Name, ")"));
                    column++;
                }

                int row = 1;
                for (uint year = YEAR_BEGIN; year <= YEAR_END; year++)
                {
                    GrdChan.RowDefinitions.Add(new RowDefinition {
                        Height = new GridLength(DEFAULT_BLOCK_SIZE)
                    });

                    // Year column.
                    AddBlock(row, 0, year.ToString());

                    // Slots columns
                    column           = 1;
                    currentLevelName = null;
                    foreach (var slot in slots)
                    {
                        if (currentLevelName == null)
                        {
                            currentLevelName = slot.Level.Name;
                        }
                        else if (currentLevelName != slot.Level.Name)
                        {
                            AddBlock(row, column, string.Empty, Brushes.DarkGray);
                            column++;
                            currentLevelName = slot.Level.Name;
                        }

                        var currentEdition = editionsInRange.Where(me => me.Year == year && me.Slot?.Id == slot.Id).FirstOrDefault();
                        if (currentEdition != null)
                        {
                            var final = currentEdition.Final;

                            if (final != null)
                            {
                                AddBlock(row, column, final.Winner.Name, ColorBySurfaceId(currentEdition.Surface, currentEdition.Indoor),
                                         File.Exists(final.Winner.ProfilePicturePath) && !final.Winner.IsJohnDoeProfilePicture ?
                                         final.Winner.ProfilePicturePath : null,
                                         File.Exists(final.Loser.ProfilePicturePath) && !final.Loser.IsJohnDoeProfilePicture ?
                                         final.Loser.ProfilePicturePath : null,
                                         withRunnerUp ? final.Loser.Name : null);
                            }
                            else
                            {
                                AddBlock(row, column, string.Empty, Brushes.Black);
                            }
                        }
                        else
                        {
                            AddBlock(row, column, string.Empty, Brushes.Black);
                        }

                        column++;
                    }
                    row++;
                }

                BtnGenerate.IsEnabled  = true;
                BtnImport.IsEnabled    = true;
                BtnSaveToJpg.IsEnabled = true;
                PgbGenerate.Visibility = Visibility.Collapsed;
            };
            worker.RunWorkerAsync();
        }