using System;
using System.Collections.Generic;
using System.Linq;
using Xamarin.Forms;
using Aurora.Models.Media;

namespace Aurora.Design.Components.Library
{
    public partial class Library : ContentView
    {
        public Library()
        {
            InitializeComponent();
            this.LibraryDataGrid.ItemSelected += (sender, e) =>
            {
                this.SelectedItem = e.SelectedItem;
            };
        }

        #region ItemsSource Property
        /// <summary>
        /// Bindable Property for the ItemsSource of the datagrid.
        /// </summary>
        /// <param name=""ItemsSource""></param>
        /// <param name="typeof(IEnumerable<object>"></param>
        /// <returns></returns>
        public static readonly BindableProperty ItemsSourceProperty =
            BindableProperty.Create(propertyName: "ItemsSource",
            returnType: typeof(IEnumerable<object>),
            declaringType: typeof(Library),
            defaultBindingMode: BindingMode.Default,
            propertyChanged: OnItemsSourceChanged);

        /// <summary>
        /// Backing property for the ItemsSource property.
        /// </summary>
        /// <value></value>
        public IEnumerable<object> ItemsSource
        {
            get
            {
                return (IEnumerable<object>)GetValue(ItemsSourceProperty);
            }
            set
            {
                SetValue(ItemsSourceProperty, value);
            }
        }

        /// <summary>
        /// ItemsSource Changed event handler
        /// </summary>
        /// <param name="bindable"></param>
        /// <param name="oldValue"></param>
        /// <param name="newValue"></param>
        private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
        {
            Library control = bindable as Library;

            var libraryDataGrid = control.LibraryDataGrid;
            libraryDataGrid.ItemsSource = newValue as IEnumerable<object>;
        }

        #endregion ItemsSource Property

        /// <summary>
        /// Bindable property for the selected item field on the datagrid.
        /// </summary>
        /// <param name=""SelectedItem""></param>
        /// <param name="typeof(BaseMetadata"></param>
        /// <returns></returns>
        public static readonly BindableProperty SelectedItemProperty =
            BindableProperty.Create(propertyName: "SelectedItem",
            returnType: typeof(object),
            declaringType: typeof(Library),
            defaultBindingMode: BindingMode.TwoWay);

        /// <summary>
        /// Backing property for the SelectedItem property.
        /// </summary>
        /// <value></value>
        public object SelectedItem
        {
            get
            {
                return ((object)GetValue(SelectedItemProperty));
            }
            set
            {
                SetValue(SelectedItemProperty, value);
            }
        }


        /// <summary>
        /// Bindable property for the item double clicked command
        /// </summary>
        /// <param name=""SelectedItem""></param>
        /// <param name="typeof(BaseMetadata"></param>
        /// <returns></returns>
        public static readonly BindableProperty ItemDoubleClickedProperty =
            BindableProperty.Create(propertyName: "ItemDoubleClicked",
            returnType: typeof(Command),
            declaringType: typeof(Library),
            propertyChanged: OnDoubleClickPropertyChanged);

        /// <summary>
        /// Public backing property
        /// </summary>
        /// <value></value>
        public Command ItemDoubleClicked
        {
            get
            {
                return (Command)GetValue(ItemDoubleClickedProperty);
            }
            set
            {
                SetValue(ItemDoubleClickedProperty, value);
            }
        }

        /// <summary>
        /// Event handler for double click property. Adds command execute handler.
        /// </summary>
        /// <param name="bindable"></param>
        /// <param name="newValue"></param>
        /// <param name="oldValue"></param>
        private static void OnDoubleClickPropertyChanged(BindableObject bindable, object newValue, object oldValue)
        {
            Library control = bindable as Library;
            var queueDataGrid = control.LibraryDataGrid;
            if (queueDataGrid.GestureRecognizers.Count > 0)
            {
                var gestureRecognizer = queueDataGrid.GestureRecognizers.First();

                if (gestureRecognizer is TapGestureRecognizer)
                {
                    TapGestureRecognizer tap = gestureRecognizer as TapGestureRecognizer;
                    tap.Tapped += (sender, e) =>
                    {
                        control.ItemDoubleClicked.Execute(null);
                    };
                }
            }
        }
    }
}