using System.Collections;
using Pixiview.UI.Theme;
using Pixiview.Utils;
using Xamarin.Forms;

namespace Pixiview.UI
{
    public class OptionEntry : Entry { }
    public class OptionPicker : Picker { }

    public abstract class OptionCell : ViewCell
    {
        public static readonly BindableProperty TitleProperty = BindableProperty.Create(
            nameof(Title), typeof(string), typeof(OptionCell));

        public string Title
        {
            get => (string)GetValue(TitleProperty);
            set => SetValue(TitleProperty, value);
        }

        protected abstract View Content { get; }

        public OptionCell()
        {
            View = new Grid
            {
                BindingContext = this,
                Padding = new Thickness(20, 0),
                ColumnDefinitions =
                {
                    new ColumnDefinition { Width = new GridLength(.3, GridUnitType.Star) },
                    new ColumnDefinition { Width = new GridLength(.7, GridUnitType.Star) }
                },
                Children =
                {
                    new Label
                    {
                        LineBreakMode = LineBreakMode.TailTruncation,
                        VerticalOptions = LayoutOptions.Center
                    }
                    .Binding(Label.TextProperty, nameof(Title))
                    .DynamicResource(Label.TextColorProperty, ThemeBase.TextColor),

                    Content.GridColumn(1)
                }
            }
            .DynamicResource(VisualElement.BackgroundColorProperty, ThemeBase.OptionTintColor);
        }
    }

    public class OptionSwitchCell : OptionCell
    {
        public static readonly BindableProperty IsToggledProperty = BindableProperty.Create(
            nameof(IsToggled), typeof(bool), typeof(OptionSwitchCell));

        public bool IsToggled
        {
            get => (bool)GetValue(IsToggledProperty);
            set => SetValue(IsToggledProperty, value);
        }

        protected override View Content => new Switch
        {
            HorizontalOptions = LayoutOptions.End,
            VerticalOptions = LayoutOptions.Center
        }.Binding(Switch.IsToggledProperty, nameof(IsToggled), BindingMode.TwoWay);
    }

    public class OptionDropCell : OptionCell
    {
        public static readonly BindableProperty ItemsProperty = BindableProperty.Create(
            nameof(Items), typeof(IList), typeof(OptionDropCell));
        public static readonly BindableProperty SelectedIndexProperty = BindableProperty.Create(
            nameof(SelectedIndex), typeof(int), typeof(OptionDropCell));

        public IList Items
        {
            get => (IList)GetValue(ItemsProperty);
            set => SetValue(ItemsProperty, value);
        }
        public int SelectedIndex
        {
            get => (int)GetValue(SelectedIndexProperty);
            set => SetValue(SelectedIndexProperty, value);
        }

        protected override View Content => new OptionPicker
        {
            HorizontalOptions = LayoutOptions.Fill,
            VerticalOptions = LayoutOptions.Center
        }
        .Binding(Picker.ItemsSourceProperty, nameof(Items))
        .Binding(Picker.SelectedIndexProperty, nameof(SelectedIndex), BindingMode.TwoWay)
        .DynamicResource(Picker.TextColorProperty, ThemeBase.TextColor)
        .DynamicResource(VisualElement.BackgroundColorProperty, ThemeBase.OptionTintColor);
    }

    public class OptionEntryCell : OptionCell
    {
        public static readonly BindableProperty TextProperty = BindableProperty.Create(
            nameof(Text), typeof(string), typeof(OptionSwitchCell));
        public static readonly BindableProperty KeyboardProperty = BindableProperty.Create(
            nameof(Keyboard), typeof(Keyboard), typeof(OptionSwitchCell));
        public static readonly BindableProperty PlaceholderProperty = BindableProperty.Create(
            nameof(Placeholder), typeof(string), typeof(OptionSwitchCell));

        public string Text
        {
            get => (string)GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }
        public Keyboard Keyboard
        {
            get => (Keyboard)GetValue(KeyboardProperty);
            set => SetValue(KeyboardProperty, value);
        }
        public string Placeholder
        {
            get => (string)GetValue(PlaceholderProperty);
            set => SetValue(PlaceholderProperty, value);
        }

        protected override View Content => new OptionEntry
        {
            HorizontalOptions = LayoutOptions.Fill,
            HorizontalTextAlignment = TextAlignment.End,
            VerticalOptions = LayoutOptions.Center,
            ReturnType = ReturnType.Next
        }
        .Binding(Entry.TextProperty, nameof(Text), BindingMode.TwoWay)
        .Binding(InputView.KeyboardProperty, nameof(Keyboard))
        .Binding(Entry.PlaceholderProperty, nameof(Placeholder))
        .DynamicResource(Entry.TextColorProperty, ThemeBase.TextColor)
        .DynamicResource(Entry.PlaceholderColorProperty, ThemeBase.SubTextColor)
        .DynamicResource(VisualElement.BackgroundColorProperty, ThemeBase.OptionTintColor);
    }
}
