context: /*
      Myna Bank
      Copyright (C) 2017 Niels Stockfleth
  
      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.
  
      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.
  
      You should have received a copy of the GNU General Public License
      along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
  using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.ComponentModel;
  using System.IO;
  using System.Security;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Input;
  using System.Windows.Threading;
  
  namespace Bank
  {
      public partial class MainWindow : Window
      {
          private Database database = new Database();
          private ObservableCollection<Account> accounts = new ObservableCollection<Account>();
          private List<Balance> balances = new List<Balance>();
          private ObservableCollection<Booking> bookings = new ObservableCollection<Booking>();
          private SecureString password;
          private StatisticsWindow statisticsWindow = null;
          private SearchWindow searchWindow = null;
          private SortDecorator sortDecorator = new SortDecorator(ListSortDirection.Descending);
  
          public MainWindow()
          {
              InitializeComponent();
              listView.ItemsSource = bookings;
              comboBox.ItemsSource = accounts;
          }
  
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
              try
              {
                  Init();
              }
              catch (Exception ex)
              {
                  HandleError(ex);
                  Close();
              }
          }
  
          private void Window_Closing(object sender, CancelEventArgs e)
          {
              try
              {
                  if (statisticsWindow != null && !statisticsWindow.IsClosed)
                  {
                      statisticsWindow.Close();
                      statisticsWindow = null;
                  }
                  if (searchWindow != null && !searchWindow.IsClosed)
                  {
                      searchWindow.Close();
                      searchWindow = null;
                  }
                  if (WindowState == WindowState.Normal)
                  {
                      Properties.Settings.Default.Left = Left;
                      Properties.Settings.Default.Top = Top;
                      Properties.Settings.Default.Width = Width;
                      Properties.Settings.Default.Height = Height;
                  }
                  Properties.Settings.Default.Save();
              }
              catch (Exception ex)
              {
                  HandleError(ex);
              }
          }
  
          private void Command_CanExecute(object sender, CanExecuteRoutedEventArgs e)
          {
              RoutedUICommand r = e.Command as RoutedUICommand;
              if (r == null) return;
              Account account = comboBox?.SelectedItem as Account;
              int selcount = (listView != null ? listView.SelectedItems.Count : 0);
              bool updatebalance = false;
              bool next = false;
              bool prev = false;
              if (account != null && balances.Count > 0)
              {
                  int sv = (int)slider.Value;
                  int sm = (int)slider.Maximum;
                  updatebalance = true;
                  if (balances.Count > 1)
                  {
                      next = sv < sm;
                      prev = sv > 0;
                  }
              }
              switch (r.Name)
              {
                  case "CreateAccount":
                  case "ImportAccount":
                  case "About":
                  case "Exit":
                      e.CanExecute = true;
                      break;
                  case "UpdateBalance":
                      e.CanExecute = updatebalance;
                      break;
                  case "Add":
                  case "DeleteAccount":
                  case "RenameAccount":
                  case "ConfigureDefaultText":
                  case "ConfigureDefaultBooking":
                  case "SetPassword":
                      e.CanExecute = account != null;
                      break;
                  case "ShowGraph":
                      e.CanExecute = account != null && (statisticsWindow == null || statisticsWindow.IsClosed);
                      break;
                  case "DeleteSheet":
                      e.CanExecute = balances.Count > 0;
                      break;
                  case "Search":
                      e.CanExecute = account != null && (searchWindow == null || searchWindow.IsClosed);
                      break;
                  case "Edit":
                      e.CanExecute = selcount == 1;
                      break;
                  case "Remove":
                      e.CanExecute = selcount >= 1;
                      break;
                  case "Import":
                      e.CanExecute = account != null;
                      break;
                  case "Next":
                  case "Last":
                      e.CanExecute = next;
                      break;
                  case "Previous":
                  case "First":
                      e.CanExecute = prev;
                      break;
                  default:
                      break;
              }
          }
  
          private void Command_Executed(object sender, ExecutedRoutedEventArgs e)
          {
              RoutedUICommand r = e.Command as RoutedUICommand;
              if (r == null) return;
              switch (r.Name)
              {
                  case "Exit":
                      Close();
                      break;
                  case "CreateAccount":
                      CreateAccount();
                      break;
                  case "ImportAccount":
                      ImportAccount();
                      break;
                  case "RenameAccount":
                      RenameAccount();
                      break;
                  case "DeleteAccount":
                      DeleteAccount();
                      break;
                  case "DeleteSheet":
                      DeleteSheet();
                      break;
                  case "UpdateBalance":
                      UpdateBalance();
                      break;
                  case "Search":
                      SearchBooking();
                      break;
                  case "Add":
                      InsertBooking();
                      break;
                  case "Edit":
                      UpdateBooking();
                      break;
                  case "Remove":
                      DeleteBooking();
                      break;
                  case "Import":
                      ImportBooking();
                      break;
                  case "About":
                      About();
                      break;
                  case "ConfigureDefaultText":
                      ConfigureDefaultText();
                      break;
                  case "ConfigureDefaultBooking":
                      ConfigureDefaultBooking();
                      break;
                  case "SetPassword":
                      SetPassword();
                      break;
                  case "Next":
                      Next();
                      break;
                  case "Previous":
                      Previous();
                      break;
                  case "First":
                      First();
                      break;
                  case "Last":
                      Last();
                      break;
                  case "ShowGraph":
                      ShowGraph();
                      break;
                  default:
                      break;
              }
          }
  
          private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
          {
              UpdateStatus();
          }
  
          private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
          {
  
gt:             var mousePosition = e.GetPosition(listView);