public void ViewPlayerProfile_Executed(object sender, ExecutedRoutedEventArgs e)
 {
     SelectedPlayer = (FootballPlayer)e.Parameter;
     CurrentView    = new PlayerProfileView();
     CurrentTitle   = SelectedPlayer.FullName;
     PropertyChanged(this, new PropertyChangedEventArgs("CurrentView"));
     PropertyChanged(this, new PropertyChangedEventArgs("CurrentTitle"));
 }
        public async Task <PlayerProfileView> GetPlayerProfileAsync(Guid userId)
        {
            var seasonsTask      = _seasonInfoRepository.GetAllSeasonsAsync();
            var summonerTask     = _summonerInfoRepository.ReadOneByUserIdAsync(userId);
            var achievementsTask = _achievementRepository.GetAchievementsForUserAsync(userId);

            var summoner = await summonerTask;

            if (summoner == null)
            {
                var message = $"User {userId} was not found.";
                _logger.LogError(message);
                throw new SummonerInfoException($"User {userId} was not found.");
            }
            var achievements = await achievementsTask;

            var alternateAccountsTask = _alternateAccountRepository.ReadAllForSummonerAsync(summoner.Id);
            var teamPlayer            = (await _teamPlayerRepository.GetAllRostersForPlayerAsync(summoner.Id)).ToList();

            var seasons = (await seasonsTask).OrderBy(x => x.SeasonStartDate).ToList();

            var alternateAccounts = await alternateAccountsTask;
            var altAccountsMapped = _alternateAccountMapper.Map(alternateAccounts);
            var achievementViews  = new List <AchievementView>();

            foreach (var achievement in achievements)
            {
                var achievementView = new AchievementView(achievement);
                var seasonAchieved  = seasons.FirstOrDefault(x =>
                                                             x.SeasonStartDate <achievement.AchievedDate &&
                                                                                (x.SeasonEndDate ?? DateTime.MaxValue)> achievement.AchievedDate);
                achievementView.Season = seasonAchieved != null ? seasonAchieved.SeasonName : "PreSeason";
                achievementViews.Add(achievementView);
            }
            var playerStatsDictionary = new Dictionary <int, PlayerStatsView>();
            var matchDetails          = await _matchDetailRepository.GetMatchDetailsForPlayerAsync(new List <Guid> {
                summoner.Id
            });

            var statIds = matchDetails.Values.SelectMany(x => x.Select(y => y.PlayerStatsId));

            var allPlayerStats = await _playerStatsRepository.GetStatsAsync(statIds);

            var teamname = "None";

            if (teamPlayer.Any())
            {
                var latestSeason = seasons.Last();
                var team         = await _teamRosterRepository.GetTeamAsync(latestSeason.Id, teamPlayer.Select(x => x.TeamRosterId));

                if (team != null)
                {
                    teamname = team.TeamName;
                }
            }
            foreach (var seasonKvp in allPlayerStats)
            {
                var season      = seasons.First(x => x.Id == seasonKvp.Key.SeasonId);
                var seasonStats = allPlayerStats.SelectMany(x => x.Value.Where(y => y.SeasonInfoId == season.Id));
                var mappedStats = _playerStatsMapper.MapForSeason(seasonStats, 0);
                if (int.TryParse(season.SeasonName.Split(" ").Last(), out var seasonNum))
                {
                    playerStatsDictionary.Add(seasonNum, mappedStats);
                }
            }
            var view = new PlayerProfileView
            {
                PlayerName            = summoner.SummonerName,
                Rank                  = _tierDivisionMapper.Map(summoner.Tier_DivisionId),
                TeamName              = string.IsNullOrEmpty(teamname) ? "None" : teamname,
                Achievements          = achievementViews,
                PlayerStats           = playerStatsDictionary,
                AlternateAccountViews = altAccountsMapped
            };

            if (summoner.PreviousSeasonRankId != null)
            {
                view.PreviousSeasonRank = _tierDivisionMapper.Map(summoner.PreviousSeasonRankId.Value);
            }

            return(view);
        }