using System;
using System.Globalization;
using System.Net;
using Syncfusion.XForms.Core;
using Syncfusion.XForms.PopupLayout;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using XF.Material.Forms.Resources.Typography;

namespace InABox.Mobile
{
    
    public class TimeButtonChangedArgs : EventArgs
    {
        public TimeSpan Time { get; private set; }

        public TimeButtonChangedArgs(TimeSpan time)
        {
            Time = time;
        }
    }
    
    public delegate void TimeButtonChanged(object sender, TimeButtonChangedArgs args);

    public class TimeSpanFormatter : UtilityConverter<TimeSpan,String>
    {

        public string Format { get; set; }
        public String Prompt { get; set; }
        public String Prefix { get; set; }

        protected override string Convert(TimeSpan value)
        {

            if (value != TimeSpan.Zero || String.IsNullOrWhiteSpace(Prompt))
            {
                try
                {
                    var sFormat = String.IsNullOrWhiteSpace(Format)
                        ? "hh\\:mm tt"
                        : Format;
                    String fmt = "{0} {1:" + sFormat + "}";
                    return String.Format(fmt,Prefix,DateTime.Today.Add(value)).Trim();
                    //return $"{DateTime.Today.Add(value):hh\\:mm tt}";
                }
                catch (Exception e)
                {

                }

            }
            return Prompt;
        }
        
    }
    
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MobileTimeButton
    {
        private SfPopupLayout popup;
        
        public event TimeButtonChanged Changed;
        
        public static readonly BindableProperty PromptProperty = BindableProperty.Create(
            nameof(Prompt), 
            typeof(string), 
            typeof(MobileTimeButton)
        );
        
        public String Prompt
        {
            get => (string)GetValue(PromptProperty);
            set
            {
                _timespanFormatter.Prompt = value;
                SetValue(PromptProperty, value);
            }
        }
        
        public static readonly BindableProperty PrefixProperty = BindableProperty.Create(
            nameof(Prefix), 
            typeof(string), 
            typeof(MobileTimeButton)
        );
        
        public String Prefix
        {
            get => (string)GetValue(PrefixProperty);
            set
            {
                _timespanFormatter.Prefix = value;
                SetValue(PrefixProperty, value);
            }
        }
        
        public static readonly BindableProperty FormatProperty = BindableProperty.Create(
            nameof(Format), 
            typeof(string), 
            typeof(MobileTimeButton)
        );
        
        public String Format
        {
            get => (string)GetValue(FormatProperty);
            set
            {
                _timespanFormatter.Format = value;
                SetValue(FormatProperty, value);
            }
        }
        
        public static readonly BindableProperty TimeProperty = BindableProperty.Create(
            nameof(Time), 
            typeof(TimeSpan), 
            typeof(MobileTimeButton));
        
        public TimeSpan Time
        {
            get => (TimeSpan)GetValue(TimeProperty);
            set => SetValue(TimeProperty,value);
        }
        
        public static readonly BindableProperty TextColorProperty = BindableProperty.Create(
            nameof(TextColor),
            typeof(Color),
            typeof(MobileTimeButton),
            XF.Material.Forms.Material.Color.OnSecondary);

        public Color TextColor
        {
            get => (Color)GetValue(TextColorProperty);
            set => SetValue(TextColorProperty, value);
        }
        
        public static readonly BindableProperty TypeScaleProperty = BindableProperty.Create(
            nameof(TypeScale), 
            typeof(MaterialTypeScale), 
            typeof(MobileTimeButton), 
            MaterialTypeScale.Button);
        
        public MaterialTypeScale TypeScale
        {
            get => (MaterialTypeScale)GetValue(TypeScaleProperty);
            set => SetValue(TypeScaleProperty, value);
        }
        
        public static readonly BindableProperty ButtonColorProperty = BindableProperty.Create(
            nameof(ButtonColor),
            typeof(Color),
            typeof(MobileTimeButton),
            XF.Material.Forms.Material.Color.Secondary);

        public Color ButtonColor
        {
            get => (Color)GetValue(ButtonColorProperty);
            set => SetValue(ButtonColorProperty, value);
        }

        public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(
            nameof(BorderColor),
            typeof(Color),
            typeof(MobileTimeButton),
            XF.Material.Forms.Material.Color.SecondaryVariant);

        public Color BorderColor
        {
            get => (Color)GetValue(BorderColorProperty);
            set => SetValue(BorderColorProperty, value);
        }
        
        // public static readonly BindableProperty PaddingProperty = BindableProperty.Create(
        //     nameof(Padding),
        //     typeof(Thickness),
        //     typeof(TimeButton),
        //     new Thickness(5));
        //
        // public new Thickness Padding
        // {
        //     get => (Thickness)GetValue(PaddingProperty);
        //     set => SetValue(PaddingProperty, value);
        // }
        
        public MobileTimeButton()
        {
            InitializeComponent();
            popup = new SfPopupLayout();
        }
        
        private void _frame_OnClicked(object sender, EventArgs e)
        {
            
            popup.PopupView.WidthRequest = 300;
            popup.PopupView.HeightRequest = 500;
            popup.PopupView.ShowHeader = false;
            popup.PopupView.ShowFooter = false;
            //popup.PopupView.PopupStyle.HasShadow = false;
            popup.PopupView.PopupStyle.CornerRadius = 5;
            //popup.PopupView.Background = new SolidColorBrush(Color.White);
            MobileTimeSelector popupContent = new MobileTimeSelector();
            popupContent.Time = Time;
            popupContent.Changed += (o, args) =>
            {
                Time = args.Time;
                DoChanged();
                popup.Closing -= RestrictClose;
                popup.Dismiss();
            };
            popupContent.Cancelled += (o, args) =>
            {
                popup.Closing -= RestrictClose;
                popup.Dismiss();
            };
            popup.Closing += RestrictClose;
            popupContent.Margin = new Thickness(10);
            popupContent.HorizontalOptions = LayoutOptions.Fill;
            popupContent.VerticalOptions = LayoutOptions.Fill;
            popup.PopupView.ContentTemplate = new DataTemplate(() => popupContent);
            
            popup.Show();
        }

        protected virtual void DoChanged()
        {
            Changed?.Invoke(this,new TimeButtonChangedArgs(Time));
        }

        private void RestrictClose(object sender, CancelEventArgs e)
        {
            e.Cancel = true;
        }
    }
}