using MahApps.Metro.Controls;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
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;

namespace ZKuP
{
    /// <summary>
    /// Interaktionslogik für Family.xaml
    /// </summary>
    public partial class Family : MetroWindow
    {
        DataTable dtOwn;
        //DataTable dtAll;

        public Family()
        {
            InitializeComponent();
            SetCalendar();

            dgOwn.DataContext = dtOwn;
            //dgAll.DataContext = dtAll;

            QueryTables();
        }

        private void SetCalendar()
        {            
            dPdate.DisplayDateStart = DateTime.Today.AddDays(-(int)DateTime.Today.DayOfWeek + 1);
            dPdate.DisplayDateEnd = DateTime.Now + TimeSpan.FromDays(365);

            var minDate = dPdate.DisplayDateStart.Value;
            var maxDate = dPdate.DisplayDateEnd.Value;

            for (var d = minDate; d <= maxDate && (DateTime.MaxValue - d.AddDays(7)).Days > 7; d = d.AddDays(7))
            {
                var range = new CalendarDateRange(d, d.AddDays(4));
                dPdate.BlackoutDates.Add(range);
            }
        }

        private async void QueryTables()
        {
            dtOwn = await SQL.ReadSQL($"SELECT `Date`, `Slot` FROM {MainWindow.table}.family WHERE `User` = '{Environment.UserName}'");
            //dtAll = await SQL.ReadSQL($"SELECT `Date`, count(`Date`) AS Count FROM zkup.family GROUP BY `Date`");

            dgOwn.ItemsSource = dtOwn.DefaultView;
            //dgAll.ItemsSource = dtAll.DefaultView;

            if(dPdate.SelectedDate.HasValue)
                DateChanged(dPdate);
        }

        private void btnSatSlot1_Click(object sender, RoutedEventArgs e)
        {
            if (dPdate.SelectedDate.HasValue)
            {
                FamilyBooking fb1 = new FamilyBooking(1, dPdate.SelectedDate.Value);
                fb1.Owner = this;
                fb1.Closed += Child_Closed;
                fb1.ShowDialog();
            }
            else
            {
                MessageBox.Show("Bitte zuerst Datum wählen", "Kein Datum gewählt", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void btnSatSlot2_Click(object sender, RoutedEventArgs e)
        {
            if (dPdate.SelectedDate.HasValue)
            {
                FamilyBooking fb2 = new FamilyBooking(2, dPdate.SelectedDate.Value);
                fb2.Owner = this;
                fb2.ShowDialog();
                fb2.Closed += Child_Closed;
                QueryTables();                
            }
            else
            {
                MessageBox.Show("Bitte zuerst Datum wählen", "Kein Datum gewählt", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }


        private void btnSatSlot3_Click(object sender, RoutedEventArgs e)
        {
            if (dPdate.SelectedDate.HasValue)
            {
                FamilyBooking fb3 = new FamilyBooking(3, dPdate.SelectedDate.Value);
                fb3.Owner = this;
                fb3.ShowDialog();
                fb3.Closed += Child_Closed;
                QueryTables();
            }
            else
            {
                MessageBox.Show("Bitte zuerst Datum wählen", "Kein Datum gewählt", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }


        private void Child_Closed(object sender, EventArgs e)
        {
            QueryTables();
        }



        private void dPdate_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            DateChanged(sender);
        }

        private void DateChanged(object sender)
        {
            var count = SQL.ReadSingleValue($"SELECT COUNT(`Date`) FROM {MainWindow.table}.family WHERE `Date` = '{(sender as DatePicker).SelectedDate.Value.ToString("yyyy-MM-dd")}'");

            btnSatSlot1.IsEnabled = true;
            btnSatSlot2.IsEnabled = true;
            btnSatSlot3.IsEnabled = true;

            btnSatSlot1.Content = "14:30 - 16:00 buchen";
            btnSatSlot2.Content = "16:15 - 17:45 buchen";
            btnSatSlot3.Content = "18:00 - 19:30 buchen";

            if (int.Parse(count) > 0)
            {
                var usedSlots = SQL.ReadListString($"SELECT `Slot` FROM {MainWindow.table}.family WHERE `Date` = '{(sender as DatePicker).SelectedDate.Value.ToString("yyyy-MM-dd")}'").Result;

                if(usedSlots.Contains("1"))
                {
                    btnSatSlot1.IsEnabled = false;
                    btnSatSlot1.Content = "Slot 1 belegt";
                }

                if(usedSlots.Contains("2"))
                {
                    btnSatSlot2.IsEnabled = false;
                    btnSatSlot2.Content = "Slot 2 belegt";
                }

                if(usedSlots.Contains("3"))
                {
                    btnSatSlot3.IsEnabled = false;
                    btnSatSlot3.Content = "Slot 3 belegt";
                }
            }
        }

        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            var y = dgOwn.SelectedItem as DataRowView;

            var res = MessageBox.Show($"Möchten Sie Ihre Buchung am {DateTime.Parse(y.Row.ItemArray[0].ToString()).ToString("dd.MM.yyyy")} stornieren?", "Wirklich löschen?", MessageBoxButton.YesNoCancel, MessageBoxImage.Exclamation);
            if(res == MessageBoxResult.Yes)
            {
                await SQL.WriteSQL($"DELETE FROM {MainWindow.table}.family WHERE `User` = '{Environment.UserName}' AND `Date` = '{DateTime.Parse(y.Row.ItemArray[0].ToString()).ToString("yyyy-MM-dd")}'");

                QueryTables();
            }
        }



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

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