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 CoachListPage : Page, INotifyPropertyChanged
    {
        ObservableCollection<CoachModel> coach = new ObservableCollection<CoachModel>();
        bool enableCommandBar = true;
        public CoachListPage()
        {
            this.InitializeComponent();
            newRecordStackPanel.Visibility = Visibility.Collapsed;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            IntializeCoachList();

            if (Datagrid.ItemsSource == null)
            {
                Datagrid.ItemsSource = this.coach;
            }
        }
        private void IntializeCoachList()
        {
            string sql = "select * from Coach";
            var coachlijst = SqliteDataAccess.LoadData<CoachModel>(sql, new Dictionary<string, object>());

            coachlijst.ForEach(x => coach.Add(x));
        }
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            string sql = "delete from Coach where Id = @Id";

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

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

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

                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 = "";
            TelefoonTextBox.Text = "";

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

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

            string sql = "insert into Coach (Naam, Telefoon) values (@Naam, @Telefoon)";

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

                _ = messagedialog.ShowAsync();

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

                _ = messagedialog.ShowAsync();

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

            NaamTextBox.Text = "";
            TelefoonTextBox.Text = "";

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

            coach.Clear();
            IntializeCoachList();
        }

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

        private CoachModel _selectedItem;

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