using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
using System.Windows.Threading;

namespace ZKuP
{
    /// <summary>
    /// Interaktionslogik für ManageParkausweis.xaml
    /// </summary>
    public partial class ManageParkausweis : MetroWindow
    {
        private DispatcherTimer _searchTimer;


        public ManageParkausweis()
        {
            InitializeComponent();

            var list = SQL.ReadSQL($"SELECT * FROM {MainWindow.table}.parkausweise").Result;
            dgManage.DataContext = list;

            _searchTimer = new DispatcherTimer();
                _searchTimer.Interval = TimeSpan.FromMilliseconds(300);
            _searchTimer.Tick += SearchTimer_Tick;
        }

        private async void DgManage_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var dg = sender as DataGrid;
            var row = dg.SelectedItem as System.Data.DataRowView;
            var arr = row.Row.ItemArray;

            if (e.Key == Key.Delete)
            {
                if (MessageBoxResult.Yes == MessageBox.Show($"Möchten Sie den Parkausweis mit der ID {arr[0]} sicher löschen?", "Sicher löschen?", MessageBoxButton.YesNo, MessageBoxImage.Question))
                {
                    await SQL.WriteSQL($"DELETE FROM {MainWindow.table}.parkausweise WHERE `idparkausweise` = '{arr[0]}'");

                    var list = SQL.ReadSQL($"SELECT * FROM {MainWindow.table}.parkausweise").Result;
                    dgManage.DataContext = list;
                }
            }
        }

        private async void OnChecked(object sender, RoutedEventArgs e)
        {
            DataRowView myDRV = (sender as DataGridCell).DataContext as DataRowView;
            string columnToSet = (sender as DataGridCell).Column.Header.ToString();

            if (!((sender as DataGridCell).Content as CheckBox).IsChecked.Value)
            {
                if (myDRV != null)
                    await SQL.WriteSQL($"UPDATE {MainWindow.table}.parkausweise SET `{columnToSet}` = '1' WHERE `idparkausweise` = '{myDRV.Row.ItemArray[0]}'");
            }
            else if (((sender as DataGridCell).Content as CheckBox).IsChecked.Value)
                if (myDRV != null)
                    await SQL.WriteSQL($"UPDATE {MainWindow.table}.parkausweise SET `{columnToSet}` = '0' WHERE `idparkausweise` = '{myDRV.Row.ItemArray[0]}'");


            //await Task.Run(() => QueryProjekte());
        }


        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            RestartSearchTimer();
        }
        private void RestartSearchTimer()
        {
            _searchTimer.Stop();
            _searchTimer.Start();
        }

        private void SearchTimer_Tick(object sender, EventArgs e)
        {
            _searchTimer.Stop();

            var tb = searchBox.Text;
            (dgManage.ItemsSource as DataView).RowFilter = $"MA_Vorname LIKE '%{(tb)}%' OR MA_Name LIKE '%{(tb)}%' OR Abteilung LIKE '%{(tb)}%' OR Kennzeichen LIKE '%{(tb)}%'";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Helper.CheckWindowIsInScreenSpace(this);
        }

        private void Window_LocationChanged(object sender, EventArgs e)
        {
            Helper.CheckWindowIsInScreenSpace(this);
        }
    }
}
