using DataAccessLibrary.DataAccess;
using DataAccessLibrary.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.WindowsRuntime;
using Telerik.UI.Xaml.Controls.Input;
using Telerik.UI.Xaml.Controls.Primitives;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;



// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238

namespace GolfComp.Views
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class KlasseListPage : Page, INotifyPropertyChanged
    {
        ObservableCollection<KlasseModel> klasse = new ObservableCollection<KlasseModel>();
       

        bool enableCommandBar = true;     
        public KlasseListPage()            
        {
            this.InitializeComponent();
            newRecordStackPanel.Visibility = Visibility.Collapsed;            
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            IntializeKlasseList();

            if (Datagrid.ItemsSource == null)
            {
                Datagrid.ItemsSource = this.klasse;
            }               
        }
        private void IntializeKlasseList()
        {
            string sql = "select * from Klasse";
            var klasselijst = SqliteDataAccess.LoadData<KlasseModel>(sql, new Dictionary<string, object>());

            klasselijst.ForEach(x => klasse.Add(x));

            
        }
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            string sql = "delete from Klasse where Klasse_Id = @Klasse_Id";

            try
            {
                Dictionary<string, object> parameters = new Dictionary<string, object>
                {
                    { "@Klasse_Id", SelectedItem.Klasse_Id }
                };
                SqliteDataAccess.DeleteData(sql, parameters);
                klasse.Clear();
                IntializeKlasseList();
            }
            catch
            {
                MessageDialog messagedialog = new MessageDialog("Fout: Selecteer een record");
                _ = messagedialog.ShowAsync();

                return;
            }
        }
        private void UpdateRecord_Click(object sender, RoutedEventArgs e)
        {  
            string sql = "update Klasse set Klasse_Naam = @Klasse_Naam, Klasse_Omschrijving = @Klasse_Omschrijving where Klasse_Id = @Klasse_Id";
   
            try
            {
                Dictionary<string, object> parameters = new Dictionary<string, object>
                {
                    { "@Klasse_Id", SelectedItem.Klasse_Id},
                    { "@Klasse_Naam", SelectedItem.Klasse_Naam },
                    { "@Klasse_Omschrijving", SelectedItem.Klasse_Omschrijving}
                };
                SqliteDataAccess.SaveData(sql, parameters);
                klasse.Clear();
                IntializeKlasseList();

                MessageDialog messagedialog = new MessageDialog("Succes: wijzigingen weggeschreven");
                _ = messagedialog.ShowAsync();
            }
            catch
            {
                MessageDialog messagedialog = new MessageDialog("Fout: Selecteer een record");
                _ = messagedialog.ShowAsync();

                return;
            }
        }
        private void Insert_new_entry(object sender, RoutedEventArgs e)
        {
            enableCommandBar = false;
            newRecordStackPanel.Visibility = Visibility.Visible;
        }
        private void DeleteNewRecord_Click(object sender, RoutedEventArgs e)
        {
            NaamTextBox.Text = "";
            klasseTextBox.Text = "";

            enableCommandBar = true;
            newRecordStackPanel.Visibility = Visibility.Collapsed;
        }
        private (bool isValid, KlasseModel model) ValidateForm()
        {
            bool isValid = true;
            KlasseModel model = new KlasseModel();

            try
            {
                model.Klasse_Naam = NaamTextBox.Text;
                model.Klasse_Omschrijving = klasseTextBox.Text;
            }
            catch
            {
                isValid = false;
            }
            return (isValid, model);
        }
        private void SaveNewRecord_Click(object sender, RoutedEventArgs e)
        {
            KlasseModel model = new KlasseModel();

            string sql = "insert into Klasse (Klasse_Naam, Klasse_Omschrijving) values (@Klasse_Naam, @Klasse_Omschrijving)";

            var form = ValidateForm();
            if(form.isValid == false)
            {
                MessageDialog messagedialog = new MessageDialog("foutieve invoer. Probeer het opnieuw");

                _= messagedialog.ShowAsync();

                return;
            }
            if(NaamTextBox.Text == "" || klasseTextBox.Text == "")
            {
                MessageDialog messagedialog = new MessageDialog("Vul alle velden in");

                _ = messagedialog.ShowAsync();

                return;
            }
            Dictionary<string, object> parameters = new Dictionary<string, object>
            {
                {"@Klasse_Naam", form.model.Klasse_Naam },
                {"@Klasse_Omschrijving", form.model.Klasse_Omschrijving }
            };
            SqliteDataAccess.SaveData(sql, parameters);
           

            NaamTextBox.Text = "";
            klasseTextBox.Text = "";

            enableCommandBar = true;
            newRecordStackPanel.Visibility = Visibility.Collapsed;

            klasse.Clear();
            IntializeKlasseList();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string propertyName = null) =>
             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        private KlasseModel _selectedItem;

        public KlasseModel SelectedItem
        {
            get => _selectedItem;
            set
            {
                if (_selectedItem != value)
                {
                    _selectedItem = value;
                    OnPropertyChanged();
                }
            }
        }
    }
}
