using System;
using System.Collections.Generic;
using System.ComponentModel;
using Xamarin.Forms;

namespace Pixiview.UI
{
    public class SegmentedControl : View, IViewContainer<SegmentedControlOption>
    {
        public IList<SegmentedControlOption> Children { get; set; }

        public SegmentedControl()
        {
            Children = new List<SegmentedControlOption>();
        }

        public static readonly BindableProperty TintColorProperty = BindableProperty.Create(
            nameof(TintColor), typeof(Color), typeof(SegmentedControl));
        public static readonly BindableProperty DisabledColorProperty = BindableProperty.Create(
            nameof(DisabledColor), typeof(Color), typeof(SegmentedControl));
        public static readonly BindableProperty SelectedTextColorProperty = BindableProperty.Create(
            nameof(SelectedTextColor), typeof(Color), typeof(SegmentedControl));
        public static readonly BindableProperty SelectedSegmentIndexProperty = BindableProperty.Create(
            nameof(SelectedSegmentIndex), typeof(int), typeof(SegmentedControl));

        public Color TintColor
        {
            get => (Color)GetValue(TintColorProperty);
            set => SetValue(TintColorProperty, value);
        }
        public Color DisabledColor
        {
            get => (Color)GetValue(DisabledColorProperty);
            set => SetValue(DisabledColorProperty, value);
        }
        public Color SelectedTextColor
        {
            get => (Color)GetValue(SelectedTextColorProperty);
            set => SetValue(SelectedTextColorProperty, value);
        }
        public int SelectedSegmentIndex
        {
            get => (int)GetValue(SelectedSegmentIndexProperty);
            set => SetValue(SelectedSegmentIndexProperty, value);
        }

        public SegmentedControlOption SelectedSegment => Children[SelectedSegmentIndex];

        public event EventHandler<ValueChangedEventArgs> ValueChanged;

        [EditorBrowsable(EditorBrowsableState.Never)]
        public void SendValueChanged()
        {
            ValueChanged?.Invoke(this, new ValueChangedEventArgs { NewValue = SelectedSegmentIndex });
        }
    }

    public class SegmentedControlOption : View
    {
        public static readonly BindableProperty TextProperty = BindableProperty.Create(
            nameof(Text), typeof(string), typeof(SegmentedControlOption));

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

        public object Value { get; set; }
    }

    public class ValueChangedEventArgs : EventArgs
    {
        public int NewValue { get; set; }
    }
}
