private void OnAddClicked(object sender, RoutedEventArgs e)
 {
     if (!IsGBAGame || GBAGameSave.SecretBaseManager.SharedSecretBases.Count < 19)
     {
         var location = SecretBaseLocationChooser.Show(Window.GetWindow(this), 0, IsGBAGame ? GBAGameSave.SecretBaseManager : null);
         if (location.HasValue && location.Value != 0)
         {
             SharedSecretBase newSecretBase = new SharedSecretBase(location.Value, null);
             if (IsGBAGame)
             {
                 bool result = SecretBaseEditTrainerWindow.Show(Window.GetWindow(this), newSecretBase, false);
                 if (result)
                 {
                     newSecretBase = GBAGameSave.SecretBaseManager.AddSecretBase(newSecretBase);
                     AddListViewItems();
                     listViewSecretBases.SelectedIndex = 2 + GBAGameSave.SecretBaseManager.SharedSecretBases.IndexOf(newSecretBase);
                     listViewSecretBases.ScrollIntoView(listViewSecretBases.SelectedItem);
                     ((Control)listViewSecretBases.SelectedItem).Focus();
                 }
             }
             else
             {
                 newSecretBase = PokeManager.AddSecretBase(newSecretBase);
                 AddListViewItems();
                 listViewSecretBases.SelectedIndex = PokeManager.SecretBases.IndexOf(newSecretBase);
                 listViewSecretBases.ScrollIntoView(listViewSecretBases.SelectedItem);
                 ((Control)listViewSecretBases.SelectedItem).Focus();
             }
         }
     }
     else
     {
         TriggerMessageBox.Show(Window.GetWindow(this), "This game already has the maximum amount of 19 shared Secret Bases", "Can't Import");
     }
 }
        private void OnChangeLocationClicked(object sender, RoutedEventArgs e)
        {
            byte?newLocation = SecretBaseLocationChooser.Show(this, secretBase.LocationID, (secretBase is SharedSecretBase ? ((SharedSecretBase)secretBase).SecretBaseManager : null));

            if (newLocation.HasValue)
            {
                secretBase.SetNewLocation(newLocation.Value);
                LoadSecretBase(secretBase);
            }
        }
 private void OnEditClicked(object sender, RoutedEventArgs e)
 {
     if (secretBase == null && IsGBAGame && listViewSecretBases.SelectedIndex == 0)
     {
         var location = SecretBaseLocationChooser.Show(Window.GetWindow(this), 0, IsGBAGame ? GBAGameSave.SecretBaseManager : null);
         if (location.HasValue && location.Value != 0)
         {
             GBAGameSave.SecretBaseLocation = location.Value;
             UpdateListViewItems();
             OnSecretBaseSelected(null, null);
         }
     }
     else
     {
         SecretBaseEditor.Show(Window.GetWindow(this), secretBase);
         //UpdateListViewItems();
         int newIndex;
         if (!secretBase.IsPlayerSecretBase)
         {
             if (IsGBAGame)
             {
                 GBAGameSave.SecretBaseManager.Sort();
                 newIndex = 2 + GBAGameSave.SecretBaseManager.SharedSecretBases.IndexOf((SharedSecretBase)secretBase);
             }
             else
             {
                 PokeManager.SortSecretBases();
                 newIndex = PokeManager.SecretBases.IndexOf((SharedSecretBase)secretBase);
             }
             AddListViewItems();
             listViewSecretBases.SelectedIndex = newIndex;
             OnSecretBaseSelected(null, null);
         }
         else
         {
             UpdateListViewItems();
             OnSecretBaseSelected(null, null);
         }
     }
 }
        private void OnImportClicked(object sender, RoutedEventArgs e)
        {
            if (!IsGBAGame || GBAGameSave.SecretBaseManager.SharedSecretBases.Count < 19)
            {
                OpenFileDialog fileDialog = new OpenFileDialog();
                fileDialog.Title  = "Import Secret Base";
                fileDialog.Filter = "Secret Base Files (*.scrtb)|*.scrtb|All Files (*.*)|*.*";
                var result = fileDialog.ShowDialog(Window.GetWindow(this));
                if (result.HasValue && result.Value)
                {
                    string filePath = fileDialog.FileName;
                    filePath = System.IO.Path.GetFullPath(filePath);

                    try {
                        FileInfo fileInfo = new FileInfo(filePath);
                        if (fileInfo.Length != 160)
                        {
                            TriggerMessageBox.Show(Window.GetWindow(this), "Cannot import Secret Base. A Secret Base file must be 160 bytes in size", "Import Error");
                        }
                        else
                        {
                            SharedSecretBase newSecretBase = new SharedSecretBase(File.ReadAllBytes(filePath), null);
                            foreach (PlacedDecoration decoration in newSecretBase.PlacedDecorations)
                            {
                                if (decoration.DecorationData == null)
                                {
                                    throw new Exception("Invalid Decoration ID in Secret Base. ID=" + decoration.ID);
                                }
                            }
                            if (IsGBAGame)
                            {
                                bool added = false;
                                if (GBAGameSave.SecretBaseManager.IsLocationInUse(newSecretBase.LocationID))
                                {
                                    MessageBoxResult result2 = TriggerMessageBox.Show(Window.GetWindow(this), "Cannot import this Secret Base because its location is already in use. Would you like to select a new location?", "Location in Use", MessageBoxButton.YesNo);
                                    if (result2 == MessageBoxResult.Yes)
                                    {
                                        byte?location = SecretBaseLocationChooser.Show(Window.GetWindow(this), newSecretBase.LocationID, GBAGameSave.SecretBaseManager, true);
                                        if (location.HasValue && location.Value != 0 && !GBAGameSave.SecretBaseManager.IsLocationInUse(location.Value))
                                        {
                                            newSecretBase.SetNewLocation(location.Value);
                                            added = true;
                                        }
                                    }
                                }
                                else
                                {
                                    added = true;
                                }
                                if (added && newSecretBase.PokemonTeam.Count == 0)
                                {
                                    MessageBoxResult result3 = TriggerMessageBox.Show(Window.GetWindow(this), "This Secret Base has no Pokemon in its Team. You will need to add at least one Pokémon in order to import this Secret Base. Would you like to continue?", "No Team", MessageBoxButton.YesNo);
                                    if (result3 == MessageBoxResult.Yes)
                                    {
                                        added = SecretBaseEditTrainerWindow.Show(Window.GetWindow(this), newSecretBase, false);
                                    }
                                }
                                if (added)
                                {
                                    newSecretBase = GBAGameSave.SecretBaseManager.AddSecretBase(newSecretBase);
                                    AddListViewItems();
                                    listViewSecretBases.SelectedIndex = 2 + GBAGameSave.SecretBaseManager.SharedSecretBases.IndexOf(newSecretBase);
                                    listViewSecretBases.ScrollIntoView(listViewSecretBases.SelectedItem);
                                    ((Control)listViewSecretBases.SelectedItem).Focus();

                                    /*for (int i = 0; i < GBAGameSave.SecretBaseManager.SharedSecretBases.Count; i++) {
                                     *      if (GBAGameSave.SecretBaseManager.SharedSecretBases[i] == newSecretBase) {
                                     *              listViewSecretBases.SelectedIndex = i + 2;
                                     *              break;
                                     *      }
                                     * }*/
                                }
                            }
                            else
                            {
                                newSecretBase = PokeManager.AddSecretBase(newSecretBase);
                                AddListViewItems();
                                listViewSecretBases.SelectedIndex = PokeManager.SecretBases.IndexOf(newSecretBase);
                                listViewSecretBases.ScrollIntoView(listViewSecretBases.SelectedItem);
                                ((Control)listViewSecretBases.SelectedItem).Focus();

                                /*for (int i = 0; i < PokeManager.SecretBases.Count; i++) {
                                 *      if (PokeManager.SecretBases[i] == newSecretBase) {
                                 *               = i;
                                 *              break;
                                 *      }
                                 * }*/
                            }
                        }
                    }
                    catch (Exception ex) {
                        MessageBoxResult result2 = TriggerMessageBox.Show(Window.GetWindow(this), "Error reading Secret Base file. Would you like to see the error?", "Import Error", MessageBoxButton.YesNo);
                        if (result2 == MessageBoxResult.Yes)
                        {
                            ErrorMessageBox.Show(ex);
                        }
                    }
                }
            }
            else
            {
                TriggerMessageBox.Show(Window.GetWindow(this), "This game already has the maximum amount of 19 shared Secret Bases", "Can't Import");
            }
        }
        private void OnSendToClicked(object sender, RoutedEventArgs e)
        {
            if (secretBase.HasTeam)
            {
                var gameIndex = SecretBaseSendToWindow.ShowDialog(Window.GetWindow(this), gameSave.GameIndex);

                if (gameIndex.HasValue)
                {
                    if (gameIndex.Value == -1)
                    {
                        PokeManager.AddSecretBase(secretBase);
                    }
                    else
                    {
                        GBAGameSave sendToGameSave = (GBAGameSave)PokeManager.GetGameSaveAt(gameIndex.Value);
                        if (sendToGameSave.SecretBaseManager.SharedSecretBases.Count < 19)
                        {
                            if (sendToGameSave.SecretBaseManager.IsLocationInUse(secretBase.LocationID))
                            {
                                MessageBoxResult result2 = TriggerMessageBox.Show(Window.GetWindow(this), "Cannot send this Secret Base because its location is already in use. Would you like to select a new location?", "Location in Use", MessageBoxButton.YesNo);
                                if (result2 == MessageBoxResult.Yes)
                                {
                                    byte?location = SecretBaseLocationChooser.Show(Window.GetWindow(this), secretBase.LocationID, sendToGameSave.SecretBaseManager, true);
                                    if (location.HasValue && location.Value != 0 && !sendToGameSave.SecretBaseManager.IsLocationInUse(location.Value))
                                    {
                                        SharedSecretBase newSecretBase;
                                        if (secretBase.IsPlayerSecretBase)
                                        {
                                            newSecretBase = new SharedSecretBase((PlayerSecretBase)secretBase, null);
                                        }
                                        else
                                        {
                                            newSecretBase = new SharedSecretBase((SharedSecretBase)secretBase, null);
                                        }
                                        newSecretBase.SetNewLocation(location.Value);
                                        sendToGameSave.SecretBaseManager.AddSecretBase((SharedSecretBase)newSecretBase);
                                    }
                                }
                            }
                            else
                            {
                                if (secretBase.IsPlayerSecretBase)
                                {
                                    sendToGameSave.SecretBaseManager.AddSecretBase((PlayerSecretBase)secretBase);
                                }
                                else
                                {
                                    sendToGameSave.SecretBaseManager.AddSecretBase((SharedSecretBase)secretBase);
                                }
                            }
                        }
                        else
                        {
                            TriggerMessageBox.Show(Window.GetWindow(this), "This game already has the maximum amount of 19 shared Secret Bases", "Can't Send");
                        }
                    }
                }
            }
            else
            {
                TriggerMessageBox.Show(Window.GetWindow(this), "You cannot send a Secret Base without a Pokémon Team", "Can't Send");
            }
        }