public static TeamsExcelCreator CreateFormation(TeamEntity[] teams, List<MatchEntity> matches, PlayerEntity[] players, ClubEntity[] clubs)
        {
            var result = new List<TeamExcelModel>();
            foreach (var team in teams.OrderByDescending(x => x.Competition).ThenBy(x => x.TeamCode))
            {
                var teamModel = new TeamExcelModel(team.Competition + " " + team.TeamCode);

                foreach (var teamPlayer in team.Players)
                {
                    var player = players.First(x => x.Id == teamPlayer.PlayerId);
                    teamModel.Players.Add(new TeamPlayerExcelModel(player.NaamKort, teamPlayer.PlayerType));
                }

                foreach (var match in matches.Where(x => x.FrenoyDivisionId == team.FrenoyDivisionId).OrderBy(x => x.Date))
                {
                    var teamMatch = new TeamMatchExcelModel(match, clubs);
                    foreach (var matchPlayer in match.Players.Where(x => x.Status != PlayerMatchStatus.Captain && x.Status != PlayerMatchStatus.Major))
                    {
                        if (!teamMatch.PlayerDecisions.ContainsKey(matchPlayer.Name))
                        {
                            teamMatch.PlayerDecisions.Add(matchPlayer.Name, matchPlayer.Status);
                        }
                    }
                    if (!string.IsNullOrWhiteSpace(match.Block))
                    {
                        foreach (var matchPlayer in match.Players.Where(x => x.Status == match.Block))
                        {
                            if (!teamMatch.CaptainDecisions.Contains(matchPlayer.Name))
                            {
                                teamMatch.CaptainDecisions.Add(matchPlayer.Name);
                            }
                        }
                    }

                    teamModel.Matches.Add(teamMatch);
                }

                result.Add(teamModel);
            }

            return new TeamsExcelCreator(result);
        }
        private static void SetVttl(PlayerEntity player, MemberEntryType frenoyPlayer)
        {
            player.Gestopt = null;

            player.IndexVttl = int.Parse(frenoyPlayer.RankingIndex);
            player.VolgnummerVttl = int.Parse(frenoyPlayer.Position);
            player.ClubIdVttl = Constants.OwnClubId;
            player.KlassementVttl = frenoyPlayer.Ranking;
            player.ComputerNummerVttl = int.Parse(frenoyPlayer.UniqueIndex);
            //player.LinkKaartVttl
        }
        private static void SetSporta(PlayerEntity player, MemberEntryType frenoyPlayer)
        {
            player.Gestopt = null;

            player.IndexSporta = int.Parse(frenoyPlayer.RankingIndex);
            player.VolgnummerSporta = int.Parse(frenoyPlayer.Position);
            player.ClubIdSporta = Constants.OwnClubId;
            player.KlassementSporta = frenoyPlayer.Ranking;
            player.LidNummerSporta = int.Parse(frenoyPlayer.UniqueIndex);
            //player.LinkKaartSporta
        }
        private PlayerEntity CreatePlayerEntity(MemberEntryType frenoyPlayer)
        {
            string name = frenoyPlayer.LastName + " " + frenoyPlayer.FirstName;
            name = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(name.Trim().ToLowerInvariant());

            var existingPlayer = _db.Players.SingleOrDefault(x => x.Naam == name);
            if (existingPlayer != null)
            {
                if (_isVttl)
                    SetVttl(existingPlayer, frenoyPlayer);
                else
                    SetSporta(existingPlayer, frenoyPlayer);
                return null;
            }
            var newPlayer = new PlayerEntity();
            newPlayer.Naam = name;
            newPlayer.NaamKort = name;
            newPlayer.Toegang = PlayerToegang.Player;
            newPlayer.Email = frenoyPlayer.Email;
            if (frenoyPlayer.Phone != null)
            {
                newPlayer.Gsm = frenoyPlayer.Phone.Mobile;
            }
            if (frenoyPlayer.Address != null)
            {
                newPlayer.Adres = frenoyPlayer.Address.Line1;
                //frenoyPlayer.Address.Line1
                newPlayer.Gemeente = frenoyPlayer.Address.ZipCode + " " + frenoyPlayer.Address.Town;
            }
            return newPlayer;
        }
 public Player UpdatePlayer(Player player)
 {
     using (var dbContext = new TtcDbContext())
     {
         var existingSpeler = dbContext.Players.FirstOrDefault(x => x.Id == player.Id);
         if (existingSpeler == null)
         {
             existingSpeler = new PlayerEntity();
             MapPlayer(player, existingSpeler);
             dbContext.Players.Add(existingSpeler);
         }
         else
         {
             MapPlayer(player, existingSpeler);
         }
         
         dbContext.SaveChanges();
         player.Id = existingSpeler.Id;
     }
     var newPlayer = GetPlayer(player.Id);
     return newPlayer;
 }
        private static void MapPlayer(Player player, PlayerEntity existingSpeler)
        {
            existingSpeler.Gsm = player.Contact.Mobile;
            existingSpeler.Email = player.Contact.Email;
            existingSpeler.Adres = player.Contact.Address;
            existingSpeler.Gemeente = player.Contact.City;

            existingSpeler.Stijl = player.Style.Name;
            existingSpeler.BesteSlag = player.Style.BestStroke;

            existingSpeler.Gestopt = player.QuitYear;
            existingSpeler.Toegang = (PlayerToegang) Enum.Parse(typeof (PlayerToegang), player.Security);

            existingSpeler.Naam = player.Name;
            existingSpeler.NaamKort = player.Alias;
        }