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 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 GolfclubListPage : Page, INotifyPropertyChanged
    {
        ObservableCollection<GolfclubModel> golfclub = new ObservableCollection<GolfclubModel>();
        ObservableCollection<SpelerModel> speler = new ObservableCollection<SpelerModel>();

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

            GetData();

            if (Datagrid.ItemsSource == null)
            {
                Datagrid.ItemsSource = this.golfclub;
            }
        }
        private void GetData()
        {
            string sql = "select * from Golfclub";
            var golfclublijst = SqliteDataAccess.LoadData<GolfclubModel>(sql, new Dictionary<string, object>());

            golfclublijst.ForEach(x => golfclub.Add(x));

            
        }
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            MessageDialog messagedialog = new MessageDialog("Golfclubs worden door andere tabellen gebruikt en kunnen niet gewist worden");
            _ = messagedialog.ShowAsync();

            //string sql = "delete from Golfclub where Id = @Id";

            //try
            //{
            //    Dictionary<string, object> parameters = new Dictionary<string, object>
            //    {
            //        { "@Id", SelectedItem.Id }
            //    };
            //    SqliteDataAccess.DeleteData(sql, parameters);
            //    golfclub.Clear();
            //    GetData();
            //    MessageDialog messagedialog = new MessageDialog("Succes: wijzigingen weggeschreven");
            //    _ = messagedialog.ShowAsync();
            //}
            //catch
            //{


            //    return;
            //}
        }
        private void UpdateRecord_Click(object sender, RoutedEventArgs e)
        {
            string sql = "update Golfclub set Naam = @Naam where Id = @Id";

            try
            {
                Dictionary<string, object> parameters = new Dictionary<string, object>
                {
                    { "@Id", SelectedItem.Id},
                    { "@Naam", SelectedItem.Naam }                    
                };
                SqliteDataAccess.SaveData(sql, parameters);
                golfclub.Clear();
                GetData();

                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 = "";
            

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

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

            string sql = "insert into Golfclub (Naam) values (@Naam)";

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

                _ = messagedialog.ShowAsync();

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

                _ = messagedialog.ShowAsync();

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

            NaamTextBox.Text = "";
            enableCommandBar = true;
            newRecordStackPanel.Visibility = Visibility.Collapsed;

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

        private GolfclubModel _selectedItem;

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