using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wpf_Demo.DataAccess;
using Wpf_Demo.ViewModels;

namespace Wpf_Demo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        public static MainWindow instance;
        public static MainWindow Instance
        {
            get
            {
                if (instance == null)
                    instance = new MainWindow();
                return instance;
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Are you sure you want to delete the selected item?", "Confirmation", MessageBoxButton.OKCancel);
            if (result == MessageBoxResult.OK)
            {
                var item = App.ViewModel.Items[MainListBox.SelectedIndex];
                // Mark as deleted
                App.ViewModel.Items.Remove(item);
                item.Delete();
                // Commit action to the db
                item.Save();
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            // Navigate to the new page
            var newModel = new GoalViewModel();

            DetailsWindow instance = new DetailsWindow();
            instance.ShowDialog(newModel);

            // Selecting the last added item
            this.MainListBox.SelectedIndex = App.ViewModel.Items.IndexOf(newModel);
        }

        private void MainListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (MainListBox.SelectedIndex == -1)
                btnDelete.IsEnabled = false;
            else
                btnDelete.IsEnabled = true;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // Set the data context of the listbox control to the sample data
            if (DataContext == null)
                DataContext = App.ViewModel;
        }

        private void MainListBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            // If selected index is -1 (no selection) do nothing
            if (MainListBox.SelectedIndex == -1)
                return;

            var selectedModel = App.ViewModel.Items[this.MainListBox.SelectedIndex];
            DetailsWindow instance = new DetailsWindow();
            instance.ShowDialog(selectedModel);

            // Selecting it in the Main List BOx
            this.MainListBox.SelectedIndex = App.ViewModel.Items.IndexOf(selectedModel);
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            DatabaseAccessLayer.CloseDatabase();
            Application.Current.Shutdown();
        }
    }
}
