using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using Pixiview.iOS.Renderers;
using Pixiview.UI;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(SegmentedControl), typeof(SegmentedControlRenderer))]
namespace Pixiview.iOS.Renderers
{
    [SuppressMessage("Code Notifications", "XI0002:Notifies you from using newer Apple APIs when targeting an older OS version", Justification = "<Pending>")]
    public class SegmentedControlRenderer : ViewRenderer<SegmentedControl, UISegmentedControl>
    {
        private UISegmentedControl nativeControl;

        protected override void OnElementChanged(ElementChangedEventArgs<SegmentedControl> e)
        {
            base.OnElementChanged(e);

            var element = Element;
            if (Control == null && element != null)
            {
                nativeControl = new UISegmentedControl();

                for (var i = 0; i < element.Children.Count; i++)
                {
                    nativeControl.InsertSegment(element.Children[i].Text, i, false);
                }

                nativeControl.Enabled = element.IsEnabled;
                nativeControl.BackgroundColor = element.BackgroundColor.ToUIColor();
                nativeControl.SelectedSegmentTintColor = GetTintColor(element);
                SetTextColor();
                nativeControl.SelectedSegment = element.SelectedSegmentIndex;

                SetNativeControl(nativeControl);
            }

            if (e.OldElement != null)
            {
                if (nativeControl != null)
                {
                    nativeControl.ValueChanged -= NativeControl_ValueChanged;
                }
            }

            if (e.NewElement != null)
            {
                nativeControl.ValueChanged += NativeControl_ValueChanged;
            }
        }

        protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            base.OnElementPropertyChanged(sender, e);

            var element = Element;
            if (nativeControl == null || element == null)
            {
                return;
            }

            switch (e.PropertyName)
            {
                case "Renderer":
                    element.SendValueChanged();
                    break;

                case nameof(element.BackgroundColor):
                    nativeControl.BackgroundColor = element.BackgroundColor.ToUIColor();
                    break;

                case nameof(element.SelectedSegmentIndex):
                    nativeControl.SelectedSegment = element.SelectedSegmentIndex;
                    break;

                case nameof(element.TintColor):
                    nativeControl.SelectedSegmentTintColor = GetTintColor(element);
                    break;

                case nameof(element.IsEnabled):
                    nativeControl.Enabled = element.IsEnabled;
                    nativeControl.SelectedSegmentTintColor = GetTintColor(element);
                    break;

                case nameof(element.SelectedTextColor):
                    SetTextColor();
                    break;
            }
        }

        private void SetTextColor()
        {
            var color = Element.SelectedTextColor;
            UIColor c = color == default ? UIColor.LabelColor : color.ToUIColor();
            var attribute = new UITextAttributes
            {
                TextColor = c
            };
            nativeControl.SetTitleTextAttributes(attribute, UIControlState.Selected);
            attribute = new UITextAttributes
            {
                TextColor = c.ColorWithAlpha(.6f)
            };
            nativeControl.SetTitleTextAttributes(attribute, UIControlState.Normal);
        }

        private UIColor GetTintColor(SegmentedControl element)
        {
            if (element.IsEnabled)
            {
                var tintColor = element.TintColor;
                if (tintColor == default)
                {
                    return UIColor.QuaternaryLabelColor;
                }
                else
                {
                    return tintColor.ToUIColor().ColorWithAlpha(.3f);
                }
            }
            else
            {
                var disabledColor = element.DisabledColor;
                if (disabledColor == default)
                {
                    return UIColor.SecondaryLabelColor;
                }
                else
                {
                    return disabledColor.ToUIColor();
                }
            }
        }

        private void NativeControl_ValueChanged(object sender, EventArgs e)
        {
            Element.SelectedSegmentIndex = (int)nativeControl.SelectedSegment;
        }

        protected override void Dispose(bool disposing)
        {
            if (nativeControl != null)
            {
                nativeControl.ValueChanged -= NativeControl_ValueChanged;
                nativeControl.Dispose();
                nativeControl = null;
            }

            base.Dispose(disposing);
        }
    }
}
