public IEnumerable<OtherMatch> GetLastOpponentMatches(int teamId, int clubId, string teamCode)
 {
     var opponent = new OpposingTeam
     {
         ClubId = clubId,
         TeamCode = teamCode
     };
     var result = _service.GetLastOpponentMatches(teamId, opponent);
     return result;
 }
        public ICollection<OtherMatch> GetLastOpponentMatches(int teamId, OpposingTeam opponent)
        {
            using (var dbContext = new TtcDbContext())
            {
                var team = dbContext.Teams.Single(x => x.Id == teamId);
                var frenoy = new FrenoyMatchesApi(dbContext, Constants.NormalizeCompetition(team.Competition));

                var firstMatch = dbContext.Matches.Where(x => x.FrenoySeason == Constants.FrenoySeason).Min(x => x.Date);
                if (DateTime.Now > firstMatch)
                {
                    frenoy.SyncLastOpponentMatches(team, opponent);

                    var matchEntities = dbContext.Matches
                        .WithIncludes()
                        .Where(match => (match.AwayClubId == opponent.ClubId && match.AwayTeamCode == opponent.TeamCode) || (match.HomeClubId == opponent.ClubId && match.HomeTeamCode == opponent.TeamCode))
                        .Where(match => match.FrenoyDivisionId == team.FrenoyDivisionId)
                        .ToList();

                    // No comments for OpponentMatches

                    var result = Mapper.Map<IList<MatchEntity>, IList<OtherMatch>>(matchEntities);
                    return result;
                }
                else
                {
                    // Pre season: Fetch last year matches instead
                    int? divisionId = frenoy.SyncLastYearOpponentMatches(team, opponent);
                    if (divisionId.HasValue)
                    {
                        var matchEntities = dbContext.Matches
                            .WithIncludes()
                            .Where(match => (match.AwayClubId == opponent.ClubId && match.AwayTeamCode == opponent.TeamCode) || (match.HomeClubId == opponent.ClubId && match.HomeTeamCode == opponent.TeamCode))
                            .Where(match => match.FrenoyDivisionId == divisionId.Value)
                            .ToList();

                        // No comments for OpponentMatches

                        // HACK: hack om vorig jaar matchen te tonen in de frontend zonder te moeten berekenen wat hun "last year division id" is
                        foreach (var match in matchEntities)
                        {
                            match.FrenoyDivisionId = team.FrenoyDivisionId;
                        }

                        var result = Mapper.Map<IList<MatchEntity>, IList<OtherMatch>>(matchEntities);
                        return result;
                    }
                }
            }
            return null;
        }
        private static bool ShouldAttemptOpponentMatchSync(OpposingTeam team, int teamId, int season = Constants.FrenoySeason)
        {
            string hash = season + team.TeamCode + team.ClubId + '-' + teamId;
            lock (FrenoyOpponentLock)
            {
                if (!FrenoyOpponentCache.ContainsKey(hash))
                {
                    FrenoyOpponentCache.Add(hash, DateTime.Now);
                    return true;
                }

                bool shouldSync = FrenoyOpponentCache[hash] > DateTime.Now + FrenoyPesterExpiration;
                if (shouldSync)
                {
                    FrenoyOpponentCache.Remove(hash);
                }
                return shouldSync;
            }
        }
 public void SyncLastOpponentMatches(TeamEntity team, OpposingTeam opponent)
 {
     if (ShouldAttemptOpponentMatchSync(opponent, team.Id))
     {
         GetMatchesResponse matches = _frenoy.GetMatches(new GetMatchesRequest
         {
             Club = GetFrenoyClubdId(opponent.ClubId),
             Season = _settings.FrenoySeason.ToString(),
             Team = opponent.TeamCode,
             WithDetailsSpecified = false,
             WithDetails = false,
             DivisionId = team.FrenoyDivisionId.ToString()
         });
         SyncMatches(team.Id, team.FrenoyDivisionId, matches, false);
     }
 }
        public int? SyncLastYearOpponentMatches(TeamEntity team, OpposingTeam opponent)
        {
            const int prevFrenoySeason = Constants.FrenoySeason - 1;
            string frenoyOpponentClub = GetFrenoyClubdId(opponent.ClubId);

            var opponentTeams = _frenoy.GetClubTeams(new GetClubTeamsRequest
            {
                Club = frenoyOpponentClub,
                Season = prevFrenoySeason.ToString()
            });

            var lastYearTeam = opponentTeams.TeamEntries.SingleOrDefault(x => x.Team == opponent.TeamCode && x.DivisionCategory == Constants.FrenoyTeamCategory);
            if (lastYearTeam != null)
            {
                int lastYearDivisionId = int.Parse(lastYearTeam.DivisionId);
                var ourTeam = _db.Teams.SingleOrDefault(x => x.Year == Constants.CurrentSeason - 1 && x.FrenoyDivisionId == lastYearDivisionId && x.Competition == _settings.Competition.ToString());
                if (ShouldAttemptOpponentMatchSync(opponent, team.Id, prevFrenoySeason))
                {
                    GetMatchesResponse matches = _frenoy.GetMatches(new GetMatchesRequest
                    {
                        Club = frenoyOpponentClub,
                        Season = prevFrenoySeason.ToString(),
                        Team = opponent.TeamCode,
                        WithDetailsSpecified = false,
                        WithDetails = false,
                        DivisionId = lastYearTeam.DivisionId
                    });
                    SyncMatches(ourTeam?.Id, lastYearDivisionId, matches, false, prevFrenoySeason);
                }

                return lastYearDivisionId;
            }
            return null;
        }