/*
        public string FranchisesDataString { get; set; }
        public string TeamsResultsDataString { get; set; }
        public string TeamsDrillDownDataString { get; set; }
        */

        public async Task<bool> LoadFranchisesDataAsync(string cachedJson = "")
        {
            List<FranchiseModel> franchises;

            if (string.IsNullOrEmpty(cachedJson))
            {
                var dataContext = new DataContext();
                var svcResult = string.Empty;

                svcResult = await dataContext.GetFranchisesAsync();
                if (svcResult == null) { return false; } //no results message
                //FranchisesDataString = svcResult;
                franchises = JsonConvert.DeserializeObject<List<FranchiseModel>>(svcResult);
            }
            else
            {
                franchises = JsonConvert.DeserializeObject<List<FranchiseModel>>(cachedJson);
            }

            var groupedItems =
                from franchise in franchises
                orderby franchise.Location
                select new TeamsFranchisesViewModel
                {
                    FirstLetter = franchise.Location.Substring(0, 1),
                    LineOne = franchise.GetDisplayName(),
                    LineTwo = franchise.GetDetail(),
                    TeamId = franchise.RetroId,
                    Location = franchise.Location
                } into list
                group list by list.FirstLetter into listByYear
                select new KeyedList<string, TeamsFranchisesViewModel>(listByYear);

            if (FranchiseList.Count <= 0)
            {
                foreach (var franchise in franchises)
                {
                    FranchiseList.Add(new TeamsFranchisesViewModel()
                    {
                        LineOne = franchise.GetDisplayName(),
                        LineTwo = franchise.GetDetail(),
                        TeamId = franchise.RetroId,
                        Location = franchise.Location
                    });
                }
            }

            FranchiseItems.Clear();
            foreach (var viewModelItem in groupedItems)
            {
                FranchiseItems.Add(viewModelItem);
            }

            return true;
        }
        public async Task<bool> LoadTeamDrillDownDataAsync(TeamsContextViewModel context, string cachedJson = "")
        {
            List<TeamsDrillDownModel> items;

            if (string.IsNullOrEmpty(cachedJson))
            {
                var dataContext = new DataContext();
                var svcResult = string.Empty;

                svcResult = await dataContext.GetTeamDrillDownAsync(context.SelectedTeamId, context.SelectedOpponentId, context.SelectedYearId);
                if (svcResult == null) { return false; } //no results message
                //TeamsDrillDownDataString = svcResult;
                items = JsonConvert.DeserializeObject<List<TeamsDrillDownModel>>(svcResult);
            }
            else
            {
                items = JsonConvert.DeserializeObject<List<TeamsDrillDownModel>>(cachedJson);
            }

            var groupedItems =
                from item in items
                orderby item.GameDate descending
                select new TeamsDrillDownViewModel
                {
                    GameDate = item.GameDate,
                    Team = item.Team,
                    Opponent = item.Opponent,
                    Win = item.Win,
                    Loss = item.Loss,
                    RunsFor = item.RunsFor,
                    RunsAgainst = item.RunsAgainst
                } into list
                group list by list.GameDate into listByYear
                select new KeyedList<string, TeamsDrillDownViewModel>(listByYear);

            TeamDrillDownItems.Clear();
            foreach (var viewModelItem in groupedItems)
            {
                TeamDrillDownItems.Add(viewModelItem);
            }
            return true;
        }
        public async Task<bool> LoadPlayerDrillDownDataAsync(PlayersContextViewModel context, string cachedJson = "")
        {
            List<PlayersDrillDownModel> items;

            if (string.IsNullOrEmpty(cachedJson))
            {
                var dataContext = new DataContext();
                var svcResult = string.Empty;

                svcResult = await dataContext.GetPlayerDrillDownAsync(context.SelectedBatterId, context.SelectedPitcherId, context.SelectedGameYear);
                if (svcResult == null) { return false; } //no results message
                //PlayersDrillDownDataString = svcResult;
                items = JsonConvert.DeserializeObject<List<PlayersDrillDownModel>>(svcResult);
            }
            else
            {
                items = JsonConvert.DeserializeObject<List<PlayersDrillDownModel>>(cachedJson);
            } 

            var groupedItems =
                from item in items
                orderby item.GameDate descending
                select new PlayersDrillDownViewModel
                {
                    GameDate = item.GameDate,
                    AtBat = item.AtBat,
                    Hit = item.Hit,
                    Walks = item.Walks,
                    SecondBase = item.SecondBase,
                    ThirdBase = item.ThirdBase,
                    HomeRun = item.HomeRun,
                    RunBattedIn = item.RunBattedIn,
                    StrikeOut = item.StrikeOut,
                    Average = item.Average.ToString("N3", CultureInfo.InvariantCulture).TrimStart('0')
                } into list
                group list by list.GameDate into listByYear
                select new KeyedList<string, PlayersDrillDownViewModel>(listByYear);

            PlayerDrillDownItems.Clear();
            foreach (var viewModelItem in groupedItems)
            {
                PlayerDrillDownItems.Add(viewModelItem);
            }
            return true;
        }
        public async Task<bool> LoadPitcherDataAsync(PlayersContextViewModel context, string cachedJson = "")
        {
            List<PitcherModel> pitchers;

            if (string.IsNullOrEmpty(cachedJson))
            {
                var dataContext = new DataContext();
                var svcResult = string.Empty;

                svcResult = await dataContext.GetPitchersAsync(context.SelectedBatterId, context.SelectedYearId);
                if (svcResult == null) { return false; } //no results message
                //PitchersDataString = svcResult;
                pitchers = JsonConvert.DeserializeObject<List<PitcherModel>>(svcResult);
            }
            else
            {
                pitchers = JsonConvert.DeserializeObject<List<PitcherModel>>(cachedJson);
            } 

            var groupedItems =
                from pitcher in pitchers
                orderby pitcher.FirstName
                select new PlayersPitchersViewModel
                {
                    FirstLetter = pitcher.FirstName.Substring(0, 1),
                    LineOne = pitcher.GetDisplayName(),
                    PitcherId = pitcher.RetroId
                } into list
                group list by list.FirstLetter into listByYear
                select new KeyedList<string, PlayersPitchersViewModel>(listByYear);

            PitcherItems.Clear();
            foreach (var viewModelItem in groupedItems)
            {
                PitcherItems.Add(viewModelItem);
            }
            return true;
        }
        public async Task<bool> LoadTeamDataAsync(PlayersContextViewModel context, string cachedJson = "")
        {
            List<TeamModel> teams;

            if (string.IsNullOrEmpty(cachedJson))
            {
                var dataContext = new DataContext();
                var svcResult = string.Empty;

                svcResult = await dataContext.GetTeamsAsync(context.SelectedYearId);
                if (svcResult == null) { return false; } //no results message
                //TeamsDataString = svcResult;
                teams = JsonConvert.DeserializeObject<List<TeamModel>>(svcResult);
            }
            else
            {
                teams = JsonConvert.DeserializeObject<List<TeamModel>>(cachedJson);
            } 
            
            var groupedItems =
                from team in teams
                orderby team.Location
                select new PlayersTeamsViewModel
                {
                    FirstLetter = team.Location.Substring(0, 1),
                    LineOne = team.GetDisplayName(),
                    LineTwo = team.GetDetail(),
                    TeamId = team.TeamId
                } into list
                group list by list.FirstLetter into listByYear
                select new KeyedList<string, PlayersTeamsViewModel>(listByYear);

            TeamItems.Clear();
            foreach (var viewModelItem in groupedItems)
            {
                TeamItems.Add(viewModelItem);
            }
            return true;
        }
        /*
        public string YearsDataString { get; set; }
        public string TeamsDataString { get; set; }
        public string BattersDataString { get; set; }
        public string PitchersDataString { get; set; }
        public string PlayersResultsDataString { get; set; }
        public string PlayersDrillDownDataString { get; set; }
        */

        public async Task<bool> LoadYearDataAsync(PlayersContextViewModel context, string cachedJson = "")
        {
            List<YearModel> years;

            if (string.IsNullOrEmpty(cachedJson))
            {
                var dataContext = new DataContext();
                var svcResult = string.Empty;

                svcResult = await dataContext.GetYearsAsync();
                if (svcResult == null) { return false; } //no results message
                //YearsDataString = svcResult;
                years = JsonConvert.DeserializeObject<List<YearModel>>(svcResult);
            }
            else
            {
                years = JsonConvert.DeserializeObject<List<YearModel>>(cachedJson);
            }

            YearItems.Clear();
            foreach (var year in years)
            {
                this.YearItems.Add(new PlayersYearsViewModel() { LineOne = year.Id.ToString(CultureInfo.CurrentCulture) });
            }
            return true;
        }