private void DeleteDeck(Deck deck)
        {
            if (deck == null)
            {
                return;
            }

            var deckStats = DeckStatsList.Instance.DeckStats.FirstOrDefault(ds => ds.BelongsToDeck(deck));

            if (deckStats != null)
            {
                if (deckStats.Games.Any())
                {
                    if (Config.Instance.KeepStatsWhenDeletingDeck)
                    {
                        DefaultDeckStats.Instance.GetDeckStats(deck.Class).Games.AddRange(deckStats.Games);
                        DefaultDeckStats.Save();
                        Logger.WriteLine(string.Format("Moved deckstats for deck {0} to default stats", deck.Name), "Edit");
                    }
                    else
                    {
                        try
                        {
                            foreach (var game in deckStats.Games)
                            {
                                game.DeleteGameFile();
                            }
                            Logger.WriteLine("Deleted games from deck: " + deck.Name, "Edit");
                        }
                        catch (Exception)
                        {
                            Logger.WriteLine("Error deleting games", "Edit");
                        }
                    }
                }
                DeckStatsList.Instance.DeckStats.Remove(deckStats);
                DeckStatsList.Save();
                Logger.WriteLine("Removed deckstats from deck: " + deck.Name, "Edit");
            }

            HearthStatsManager.DeleteDeckAsync(deck, false, true);

            DeckList.DecksList.Remove(deck);
            WriteDecks();
            DeckPickerList.RemoveDeck(deck);
            ListViewDeck.ItemsSource = null;
            Logger.WriteLine("Deleted deck: " + deck.Name, "Edit");
        }
        private async void BtnDeleteDeck_Click(object sender, RoutedEventArgs e)
        {
            var deck = DeckPickerList.SelectedDeck;

            if (deck != null)
            {
                var settings = new MetroDialogSettings {
                    AffirmativeButtonText = "Yes", NegativeButtonText = "No"
                };
                var result =
                    await
                    this.ShowMessageAsync("Deleting " + deck.Name, "Are you Sure?",
                                          MessageDialogStyle.AffirmativeAndNegative, settings);

                if (result == MessageDialogResult.Affirmative)
                {
                    try
                    {
                        var deckStats = DeckStatsList.Instance.DeckStats.FirstOrDefault(ds => ds.Name == deck.Name);
                        if (deckStats != null)
                        {
                            foreach (var game in deckStats.Games)
                            {
                                game.DeleteGameFile();
                            }
                            DeckStatsList.Instance.DeckStats.Remove(deckStats);
                            DeckStatsList.Save();
                            Logger.WriteLine("Deleted deckstats for deck: " + deck.Name);
                        }
                        DeckList.DecksList.Remove(deck);
                        WriteDecks();
                        DeckPickerList.RemoveDeck(deck);
                        ListViewDeck.ItemsSource = null;
                        Logger.WriteLine("Deleted deck: " + deck.Name);
                    }
                    catch (Exception)
                    {
                        Logger.WriteLine("Error deleting deck");
                    }
                }
            }
        }
        private async void BtnSaveDeck_Click(object sender, RoutedEventArgs e)
        {
            //NewDeck.Cards =
            //	new ObservableCollection<Card>(
            //		NewDeck.Cards.OrderBy(c => c.Cost).ThenByDescending(c => c.Type).ThenBy(c => c.Name).ToList());
            //ListViewNewDeck.ItemsSource = NewDeck.Cards;
            var deckName = TextBoxDeckName.Text;

            if (EditingDeck)
            {
                var settings = new MetroDialogSettings {
                    AffirmativeButtonText = "Overwrite", NegativeButtonText = "Save as new"
                };
                var result =
                    await
                    this.ShowMessageAsync("Saving deck", "How do you wish to save the deck?", MessageDialogStyle.AffirmativeAndNegative,
                                          settings);

                if (result == MessageDialogResult.Affirmative)
                {
                    SaveDeck(true);
                }
                else if (result == MessageDialogResult.Negative)
                {
                    SaveDeck(false);
                }
            }
            else if (DeckList.DecksList.Any(d => d.Name == deckName))
            {
                var settings = new MetroDialogSettings {
                    AffirmativeButtonText = "Overwrite", NegativeButtonText = "Set new name"
                };
                var result =
                    await
                    this.ShowMessageAsync("A deck with that name already exists", "Overwriting the deck can not be undone!",
                                          MessageDialogStyle.AffirmativeAndNegative, settings);

                if (result == MessageDialogResult.Affirmative)
                {
                    Deck oldDeck;
                    while ((oldDeck = DeckList.DecksList.FirstOrDefault(d => d.Name == deckName)) != null)
                    {
                        var deckStats = DeckStatsList.Instance.DeckStats.FirstOrDefault(ds => ds.Name == oldDeck.Name);
                        if (deckStats != null)
                        {
                            foreach (var game in deckStats.Games)
                            {
                                game.DeleteGameFile();
                            }
                            DeckStatsList.Instance.DeckStats.Remove(deckStats);
                            DeckStatsList.Save();
                            Logger.WriteLine("Deleted deckstats for deck: " + oldDeck.Name);
                        }
                        DeckList.DecksList.Remove(oldDeck);
                        DeckPickerList.RemoveDeck(oldDeck);
                    }

                    SaveDeck(true);
                }
                else if (result == MessageDialogResult.Negative)
                {
                    SaveDeck(false);
                }
            }
            else
            {
                SaveDeck(false);
            }

            editedDeckName = string.Empty;
        }
        private async void SaveDeck(bool overwrite)
        {
            var deckName = TextBoxDeckName.Text;

            if (string.IsNullOrEmpty(deckName))
            {
                var settings = new MetroDialogSettings {
                    AffirmativeButtonText = "Set", DefaultText = deckName
                };

                var name = await this.ShowInputAsync("No name set", "Please set a name for the deck", settings);

                if (String.IsNullOrEmpty(name))
                {
                    return;
                }

                deckName             = name;
                TextBoxDeckName.Text = name;
            }

            while (DeckList.DecksList.Any(d => d.Name == deckName) && (!EditingDeck || !overwrite))
            {
                var settings = new MetroDialogSettings {
                    AffirmativeButtonText = "Set", DefaultText = deckName
                };
                var name =
                    await
                    this.ShowInputAsync("Name already exists", "You already have a deck with that name, please select a different one.", settings);

                if (String.IsNullOrEmpty(name))
                {
                    return;
                }

                deckName             = name;
                TextBoxDeckName.Text = name;
            }

            if (_newDeck.Cards.Sum(c => c.Count) != 30)
            {
                var settings = new MetroDialogSettings {
                    AffirmativeButtonText = "Yes", NegativeButtonText = "No"
                };

                var result =
                    await
                    this.ShowMessageAsync("Not 30 cards",
                                          string.Format("Deck contains {0} cards. Is this what you want to save anyway?",
                                                        _newDeck.Cards.Sum(c => c.Count)),
                                          MessageDialogStyle.AffirmativeAndNegative, settings);

                if (result != MessageDialogResult.Affirmative)
                {
                    return;
                }
            }

            if (EditingDeck && overwrite)
            {
                DeckList.DecksList.Remove(_newDeck);
                DeckPickerList.RemoveDeck(_newDeck);
            }

            var oldDeckName = _newDeck.Name;

            _newDeck.Name = deckName;
            _newDeck.Tags = TagControlEdit.GetTags();

            var newDeckClone = (Deck)_newDeck.Clone();

            DeckList.DecksList.Add(newDeckClone);

            newDeckClone.LastEdited = DateTime.Now;

            WriteDecks();
            Logger.WriteLine("Saved Decks");

            if (EditingDeck)
            {
                TagControlEdit.SetSelectedTags(new List <string>());
                if (deckName != oldDeckName)
                {
                    var statsEntry = DeckStatsList.Instance.DeckStats.FirstOrDefault(d => d.Name == oldDeckName);
                    if (statsEntry != null)
                    {
                        if (overwrite)
                        {
                            statsEntry.Name = deckName;
                            Logger.WriteLine("Deck has new name, updated deckstats");
                        }
                        else
                        {
                            var newStatsEntry = DeckStatsList.Instance.DeckStats.FirstOrDefault(d => d.Name == deckName);
                            if (newStatsEntry == null)
                            {
                                newStatsEntry = new DeckStats(deckName);
                                DeckStatsList.Instance.DeckStats.Add(newStatsEntry);
                            }
                            foreach (var game in statsEntry.Games)
                            {
                                newStatsEntry.AddGameResult(game.CloneWithNewId());
                            }
                            Logger.WriteLine("cloned gamestats for \"Set as new\"");
                        }
                        DeckStatsList.Save();
                    }
                }
            }

            //after cloning the stats, otherwise new stats will be generated
            DeckPickerList.AddAndSelectDeck(newDeckClone);

            EditingDeck = false;

            foreach (var tag in _newDeck.Tags)
            {
                SortFilterDecksFlyout.AddSelectedTag(tag);
            }

            DeckPickerList.UpdateList();
            DeckPickerList.SelectDeck(newDeckClone);

            CloseNewDeck();
            ClearNewDeckSection();
        }