public void GetHistoryList(){

            using (var context = new GameModelContainer())
            {

                historyList = new ObservableCollection<HistoryViewModel>(context.Histories.ToList().ConvertAll(new Converter<History, HistoryViewModel>(x =>
            {
                return new HistoryViewModel()
                {
                    Date = x.Date,
                    GameName = x.Game.Name,
                    Id = x.Id,
                    PlayerName = x.Player.Name

                };
            })));
            }

        }
 public DatabaseInitializer()
 {
     using (var context = new GameModelContainer())
     {
         if (!context.Database.Exists())
         {
             context.Database.Create();
             GenerateDefaultPlayer(context);
             GenerateGamesFromResource();
             GenerateGameParamsFromResources();
             GenerateGameResultsFromResources();
             context.GameParams.AddRange(_gameParams);
             context.GameResults1.AddRange(_gameResults);
             context.Games.AddRange(_games);
             context.SaveChanges();
         }
         context.Database.Initialize(false);
     }
 }
        private void AddButton_Click(object sender, RoutedEventArgs e)
        {
            if (player.Name.Length > 0 && player.Surname.Length > 0)
            {


                using (var context = new GameModelContainer())
                {
                    context.Players.Add(new Player()
                    {
                        Name = player.Name,
                        Surname = player.Surname,
                        Age = player.Age
                    });
                    context.SaveChanges();
                }
                _lPlayerAdded.Visibility = Visibility.Visible;

            }
            

        }
        private void ShowPlayerDetailsButton_Click(object sender, RoutedEventArgs e)
        {   
           
            int index = -1;
            if (HistoryGrid.SelectedCells.Count != 0)
            {
                try
                {
                    var cellInfo = HistoryGrid.SelectedCells[0];

                    var content = cellInfo.Column.GetCellContent(cellInfo.Item);
                    HistoryViewModel row = (HistoryViewModel)content.DataContext;
                    index = row.Id;
                }
                catch (Exception ex)
                {
                    //zaznaczono pusty rekord
                }
                
            }
            if (index>-1)
            {
                using (var context = new GameModelContainer())
                {
                    
                    historyResultList = new ObservableCollection<GameHistoryResultViewModel>(context.HistoryResults.Where(x => x.History.Id == index).ToList().ConvertAll(new Converter<HistoryResult, GameHistoryResultViewModel>(x =>
                    {
                        return new GameHistoryResultViewModel()
                        {
                            Value = x.Value,
                            Name = x.GameResult.Name

                        };
                    })));

                    historyParamList = new ObservableCollection<GameHistroyParamViewModel>(context.HistoryParams1.Where(x => x.History.Id == index).ToList().ConvertAll(new Converter<HistoryParam, GameHistroyParamViewModel>(x =>
                    {
                        return new GameHistroyParamViewModel()
                        {
                            Value = x.Value,
                            Name = x.GameParam.Name
                            

                        };
                    })));

                }

                HistoryResultGrid.DataContext = historyParamList;
                GameResultGrid.DataContext = historyResultList;
            }

        }
        private void HistoryGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            using (var context = new GameModelContainer())
            {
                object item = HistoryGrid.SelectedItem;
                int index = Convert.ToInt32((HistoryGrid.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text);// Convert.ToInt32(((DataRowView)HistoryGrid.SelectedItems[0])["Id"].ToString());
                historyResultList = new ObservableCollection<GameHistoryResultViewModel>(context.HistoryResults.Where(x => x.Id == index).ToList().ConvertAll(new Converter<HistoryResult, GameHistoryResultViewModel>(x =>
                {
                    return new GameHistoryResultViewModel()
                    {
                        Value = x.Value,
                        Name = x.GameResult.Name

                    };
                })));
            }
            

        }
        private void GameResultGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            using (var context = new GameModelContainer())
            {

                historyResultList = new ObservableCollection<GameHistoryResultViewModel>(context.HistoryResults.Where(x => x.Id == Convert.ToInt32(((DataRowView)HistoryGrid.SelectedItems[0])["Id"].ToString())).ToList().ConvertAll(new Converter<HistoryResult, GameHistoryResultViewModel>(x =>
                {
                    return new GameHistoryResultViewModel()
                    {
                       Value = x.Value,
                       Name = x.GameResult.Name

                    };
                })));
            }
            

        }
 private void GenerateDefaultPlayer(GameModelContainer context)
 {
     context.Players.Add(new Player {Name = "Jan", Surname = "Kowalski", Age = 10});
 }