using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace InABox.Mobile
{
    
    public class MobileRadioListChangedEventArgs : EventArgs
    {
    }

    public delegate void MobileRadioListChangedEvent(object sender, MobileRadioListChangedEventArgs args);
    
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MobileRadioList
    {
        
        private readonly BindableProperty BackgroundColorProperty = BindableProperty.Create(
            nameof(BackgroundColor),
            typeof(Color),
            typeof(MobileRadioList),
            XF.Material.Forms.Material.Color.Surface);

        [TypeConverter(typeof(ColorTypeConverter))]
        public Color BackgroundColor
        {
            get => (Color)GetValue(BackgroundColorProperty);
            set => SetValue(BackgroundColorProperty, value);
        }        
        
        private readonly BindableProperty TextColorProperty = BindableProperty.Create(
            nameof(TextColor),
            typeof(Color),
            typeof(MobileRadioList),
            XF.Material.Forms.Material.Color.OnSurface);

        [TypeConverter(typeof(ColorTypeConverter))]
        public Color TextColor
        {
            get => (Color)GetValue(TextColorProperty);
            set => SetValue(TextColorProperty, value);
        }
        
        private readonly BindableProperty BorderColorProperty = BindableProperty.Create(
            nameof(BorderColor),
            typeof(Color),
            typeof(MobileRadioList),
            XF.Material.Forms.Material.Color.Secondary);
        
        public Color BorderColor
        {
            get => (Color)GetValue(BorderColorProperty);
            set => SetValue(BorderColorProperty, value);
        }
        
        public IList<MobileRadioListItem> Items { get; }
        
        public MobileRadioListItem SelectedItem
        {
            get => Items.FirstOrDefault(x => x.IsChecked);
            set
            {
                foreach (var item in Items)
                    item.IsChecked = item == value;
            }
        }

        public event MobileRadioListChangedEvent Changed;
        
        public MobileRadioList()
        {
            Items = new ObservableCollection<MobileRadioListItem>();
            InitializeComponent();
            BindingContext = this;
            _list.ItemsSource = Items;
            BackgroundColor = XF.Material.Forms.Material.Color.Surface;
        }
        
        private void Tapped(object sender, EventArgs e)
        {
            if ((sender as BindableObject)?.BindingContext is MobileRadioListItem item)
            {
                if (item != SelectedItem)
                {
                    SelectedItem = item;
                    Changed?.Invoke(this, new MobileRadioListChangedEventArgs());
                    _list.ItemsSource = null;
                    _list.ItemsSource = Items;
                }    
            }
        }
    }
}