using MahApps.Metro.Controls;
using MySql.Data.MySqlClient;
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;

namespace ZKuP
{
    /// <summary>
    /// Interaktionslogik für Notifications.xaml
    /// </summary>
    public partial class Notifications : MetroWindow
    {
        DataTable dt = new DataTable();
        public Notifications()
        {
            InitializeComponent();

            QueryList();
        }

        private void QueryList()
        {
            dt = SQL.ReadSQL($"SELECT Visitor, SMS, SMSSent, SMSRepeat, id_notification FROM {MainWindow.table}.notification WHERE `Username` = '{Environment.UserName}'", dt).Result;
            dgNotifications.ItemsSource = dt.DefaultView;
        }

        private void cbCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string query = "";

            switch ((sender as ComboBox).SelectedIndex)
            {
                case 1:
                    query = $"SELECT DISTINCT Name FROM {MainWindow.table}.firmen ORDER BY Name";
                    break;
                case 2:
                    query = $"SELECT DISTINCT Name FROM {MainWindow.table}.besucher ORDER BY Name";
                    break;
                case 3:
                    query = $"SELECT DISTINCT Firma FROM {MainWindow.table}.lieferanten ORDER BY Firma";
                    break;
                case 4:
                    query = $"SELECT DISTINCT Firma FROM {MainWindow.table}.lieferanten WHERE Firma LIKE 'DB%'\" ORDER BY Firma";
                    break;
            }

            var res = SQL.ReadListString(query).Result;
            res.Insert(0, "");

            cBVisitor.ItemsSource = res;
            cBVisitor.IsEnabled = true;
        }

        private void cBVisitor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //if ((sender as ComboBox).SelectedIndex != 0 && (sender as ComboBox).SelectedIndex != -1)
            //    cbSMS.IsEnabled = true;
            //else
            //    cbSMS.IsEnabled = false;
        }



        private void cbSMS_Checked(object sender, RoutedEventArgs e)
        {
            //tbSMS.IsEnabled = true;
        }

        private void cbWin_Checked(object sender, RoutedEventArgs e)
        {

        }


        private void cbSMS_Unchecked(object sender, RoutedEventArgs e)
        {
            //tbSMS.IsEnabled = false;
        }

        private void cbWin_Unchecked(object sender, RoutedEventArgs e)
        {

        }


        private void btnAddNotification_Click(object sender, RoutedEventArgs e)
        {
            IntToBool iTb = new IntToBool();

            //SQL.WriteSQL($"INSERT INTO {MainWindow.table}.notification (Username, Visitor, SMS, SMSRepeat) VALUES ('{Environment.UserName}', '{cBVisitor.SelectedValue.ToString()}', '{tbSMS.Text}', '{iTb.ConvertBack(rbRepeatYes.IsChecked.Value, typeof(int), null, null)}')");
            SQL.CreateAndWriteSQL($"INSERT INTO {MainWindow.table}.notification (Username, Visitor, SMS, SMSRepeat) VALUES (@UserName, @cBVisitor, @tbSMS, @iTb", new List<MySqlParameter>()
                                {
                                    new MySqlParameter("@UserName", Environment.UserName),
                                    new MySqlParameter("@cBVisitor", cBVisitor.SelectedValue.ToString()),
                                    new MySqlParameter("@tbSMS", tbSMS.Text),
                                    new MySqlParameter("@iTb", iTb.ConvertBack(rbRepeatYes.IsChecked.Value, typeof(int), null, null))
                                });


            QueryList();
        }

        private void dgNotifications_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Delete) 
            {
                SQL.WriteSQL($"DELETE FROM {MainWindow.table}.notification WHERE id_notification = {(dgNotifications.SelectedItem as DataRowView).Row.ItemArray[4]}");

                QueryList();
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }



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

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