using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Collections;
using System.Linq;
using Xamarin.Forms;
using Aurora.Proto.Party;
using DLToolkit.Forms.Controls;

namespace Aurora.Design.Components.MemberList
{
    public partial class MemberList : ContentView
    {
        public MemberList()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Bindable property for members list.
        /// </summary>
        /// <param name=""Members""></param>
        /// <param name="typeof(IEnumerable<string>"></param>
        /// <returns></returns>
        public static readonly BindableProperty MembersProperty =
            BindableProperty.Create(propertyName: "Members",
            returnType: typeof(ObservableCollection<Member>),
            declaringType: typeof(MemberList),
            defaultBindingMode: BindingMode.Default,
            propertyChanged: OnMembersChanged);

        /// <summary>
        /// Backing property for MembersProperty
        /// </summary>
        /// <value></value>
        public ObservableCollection<Member> Members
        {
            get
            {
                return (ObservableCollection<Member>)GetValue(MembersProperty);
            }
            set
            {
                SetValue(MembersProperty, value);
            }
        }


        /// <summary>
        /// Memberes changed event handler. Assign member list source.
        /// </summary>
        /// <param name="bindable"></param>
        /// <param name="oldValue"></param>
        /// <param name="newValue"></param>
        private static void OnMembersChanged(BindableObject bindable, object oldValue, object newValue)
        {
            var control = (MemberList)bindable;
            var membersList = control.FindByName("MembersList") as FlowListView;

            if (newValue is ICollection source)
            {
                membersList.FlowItemsSource = source;
            }
        }
    }
}