public static void GetUserTeamCupJson(int userTeamId, UserTeam userTeam, UserTeamCupMatches userTeamCupInsert, SqlConnection db)
        {
            try
            {
                //Load Cup data
                UserTeamCupRepository cupRepository = new UserTeamCupRepository();

                //List<int> CupIds = cupRepository.GetAllCupIdsForUserId(userTeamId, db);

                //int cupid, gameweekid;

                foreach (UserTeamCupMatch match in userTeam.leagues.cup.matches)
                {
                    match.fromuserteamid = userTeamId;

                    //if (!CupIds.Contains(match.id) && match.@event < Globals.LatestGameweek)
                    if (match.@event < Globals.LatestGameweek)
                    {
                        //cupRepository.InsertUserTeamCup(match, db);
                        userTeamCupInsert.Add(match);
                        Logger.Out("Cup: " + match.entry_1_name + " v " + match.entry_2_name + " - added");
                    }
                    //else
                    //{
                    //    cupRepository.UpdateUserTeamCup(match, db);
                    //}

                    //if (match.tiebreak != null)
                    //{
                    //    cupid = match.id;
                    //    gameweekid = match.@event;
                    //    GetUserTeamCupTiebreakJson(userTeamId, cupid, gameweekid, cup);
                    //}
                }
            }
            catch (Exception ex)
            {
                Logger.Error("GetUserTeamCupJson data exception (UserTeamId: " + userTeamId.ToString() + "): " + ex.Message);
                //throw new Exception("GetUserTeamCupJson data exception (UserTeamId: " + userTeamId.ToString() + "): " + ex.Message);
                //GetUserTeamCupJson(userTeamId, userTeamHistoryData);
            }
        }
        public bool InsertUserTeam(UserTeam userTeam, SqlConnection db)
        {
            try
            {
                long rowsAffected = 0;

                rowsAffected = db.Insert(userTeam, commandTimeout: 0);

                if (rowsAffected > 0)
                {
                    Logger.Out("UserTeam: " + userTeam.name + " (" + Convert.ToString(userTeam.id) + ") - inserted");
                    //Logger.Out("");
                    return(true);
                }
                return(false);
            }
            catch (Exception ex)
            {
                Logger.Error("UserTeam Repository (insert) error: " + ex.Message);
                throw ex;
            }
        }
        public static void GetUserTeamCupJson(int userTeamId, UserTeam userTeam, SqlConnection db)
        {
            try
            {
                //Load Cup data
                UserTeamCupRepository cupRepository = new UserTeamCupRepository();

                List <int> CupIds = cupRepository.GetAllCupIdsForUserId(userTeamId, db);

                int cupid, gameweekid;

                foreach (UserTeamCupMatch match in userTeam.leagues.cup.matches)
                {
                    match.fromuserteamid = userTeamId;

                    if (!CupIds.Contains(match.id))
                    {
                        cupRepository.InsertUserTeamCup(match, db);
                    }
                    else
                    {
                        cupRepository.UpdateUserTeamCup(match, db);
                    }

                    if (match.tiebreak != null)
                    {
                        cupid      = match.id;
                        gameweekid = match.@event;
                        //GetUserTeamCupTiebreakJson(userTeamId, cupid, gameweekid, cup);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("GetUserTeamCupJson data exception (UserTeamId: " + userTeamId.ToString() + "): " + ex.Message);
                throw new Exception("GetUserTeamCupJson data exception (UserTeamId: " + userTeamId.ToString() + "): " + ex.Message);
                //GetUserTeamCupJson(userTeamId, userTeamHistoryData);
            }
        }
        public static void GetUserTeamData(int userTeamId, List <int> userTeamIds, string userTeamUrl, UserTeams userTeamInsert, UserTeamClassicLeagues userTeamClassicLeaguesInsert, UserTeamH2hLeagues userTeamH2hLeaguesInsert, SqlConnection db)
        {
            try
            {
                userTeamUrl = string.Format(userTeamUrl, userTeamId);

                HttpClient     client     = new HttpClient();
                JsonSerializer serializer = new JsonSerializer();
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                using (Stream s = client.GetStreamAsync(userTeamUrl).Result)
                    using (StreamReader sr = new StreamReader(s))
                        using (JsonReader reader = new JsonTextReader(sr))
                        {
                            Globals.apiCalls         += 1;
                            Globals.apiUserTeamCalls += 1;

                            // read the json from a stream
                            // json size doesn't matter because only a small piece is read at a time from the HTTP request
                            var userTeamData = serializer.Deserialize <UserTeam>(reader);

                            UserTeam userTeam = userTeamData;

                            //Load UserTeam data
                            //string userTeamNameDB = "";
                            string userTeamName = "";

                            userTeamName = userTeam.name;
                            Logger.Out(userTeamName + " (" + Convert.ToString(userTeamId) + ")");
                            //Logger.Out("");

                            UserTeamRepository userTeamRepository = new UserTeamRepository();

                            if (!userTeamIds.Contains(userTeam.id) && !userTeamInsert.Contains(userTeam))
                            {
                                userTeamInsert.Add(userTeam);
                            }
                            //else
                            //{
                            //    userTeamNameDB = userTeamRepository.GetUserTeamName(userTeamId, db);
                            //    if (userTeamName != userTeamNameDB)
                            //    {
                            //        userTeamRepository.UpdateUserTeam(userTeam, db);
                            //    }
                            //}

                            GetUserTeamClassicLeagueJson(userTeamId, userTeamData, userTeamClassicLeaguesInsert, db);

                            if (userTeam.leagues.h2h != null)
                            {
                                GetUserTeamH2hLeagueJson(userTeamId, userTeamData, userTeamH2hLeaguesInsert, db);
                            }

                            //if (userTeam.leagues.cup != null)
                            //{
                            //    GetUserTeamCupJson(userTeamId, userTeamData, db);
                            //}
                        }
            }
            catch (Exception ex)
            {
                Logger.Error("GetUserTeamData data exception (UserTeamId: " + userTeamId.ToString() + "): " + ex.Message);
                throw new Exception("GetUserTeamData data exception (UserTeamId: " + userTeamId.ToString() + "): " + ex.Message);
            }
        }
        public static void GetUserTeamLeagueAndCupJson(int userTeamId, List <int> userTeamIds, string userTeamUrl, UserTeams userTeamsUpdateInsert, UserTeamCupMatches userTeamCupInsert, UserTeamClassicLeagues userTeamClassicLeaguesInsert, UserTeamH2hLeagues userTeamH2hLeaguesInsert, SqlConnection db)
        {
            try
            {
                var url = "";
                url = string.Format(userTeamUrl, userTeamId);

                JsonSerializer serializer = new JsonSerializer()
                {
                    Formatting = Formatting.None
                };

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                using (HttpClient client = new HttpClient())
                    using (Stream s = client.GetStreamAsync(url).Result)
                        using (StreamReader sr = new StreamReader(s))
                            using (JsonReader reader = new JsonTextReader(sr))
                            {
                                Globals.ApiCalls         += 1;
                                Globals.ApiUserTeamCalls += 1;

                                // read the json from a stream
                                // json size doesn't matter because only a small piece is read at a time from the HTTP request
                                var userTeamData = serializer.Deserialize <UserTeam>(reader);

                                if (userTeamData != null)
                                {
                                    UserTeam userTeam = userTeamData;

                                    //Load UserTeam data
                                    string userTeamName = "";

                                    //Set Global variable startedEvent
                                    Globals.StartGameweekId = userTeam.started_event;

                                    userTeamName = userTeam.name;
                                    Logger.Out(userTeamName);
                                    //Logger.Out("");

                                    UserTeamRepository userTeamRepository = new UserTeamRepository();

                                    if (!userTeamIds.Contains(userTeam.id))
                                    {
                                        userTeamRepository.InsertUserTeam(userTeam, db);
                                        userTeamIds.Add(userTeam.id);
                                        Globals.UserTeamInsertCount += 1;
                                    }
                                    else
                                    {
                                        userTeamsUpdateInsert.Add(userTeam);
                                        //userTeamRepository.UpdateUserTeam(userTeamsInsert, db);
                                    }

                                    if (userTeamData.leagues.classic.Count != Globals.LeagueCountFromUserTeamClassicLeagueForUserTeamId)
                                    {
                                        GetUserTeamClassicLeagueJson(userTeamId, userTeamData, userTeamClassicLeaguesInsert, db);
                                    }

                                    if (userTeam.leagues.h2h != null)
                                    {
                                        GetUserTeamH2hLeagueJson(userTeamId, userTeamData, userTeamH2hLeaguesInsert, db);
                                    }

                                    if (userTeam.leagues.cup != null)
                                    {
                                        GetUserTeamCupJson(userTeamId, userTeamData, userTeamCupInsert, db);
                                    }
                                }
                            }
            }
            catch (Exception ex)
            {
                Logger.Error("GetUserTeamLeagueAndCupJson data exception (UserTeamId: " + userTeamId.ToString() + "): " + ex.Message);
                //throw new Exception("GetUserTeamLeagueAndCupJson data exception (UserTeamId: " + userTeamId.ToString() + "): " + ex.Message);
            }
        }