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 : AbstractConverter<TimeSpan,String>
    {

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

        protected override string Convert(TimeSpan value, object? parameter = null)
        {

            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
    {
        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();
        }
        
        private void _frame_OnClicked(object sender, EventArgs e)
        {
            
            MobileTimeSelector popupContent = new MobileTimeSelector();
            popupContent.Time = Time;
            popupContent.Changed += (o, args) =>
            {
                Time = args.Time;
                DoChanged();
                PopupManager.DismissPopup();
            };
            popupContent.Cancelled += (o, args) =>
            {
                PopupManager.DismissPopup();
            };
            popupContent.HorizontalOptions = LayoutOptions.Fill;
            popupContent.VerticalOptions = LayoutOptions.Fill;
            
            PopupManager.ShowPopup(
                this, 
                () => popupContent, 
                new PopupManagerConfiguration()
                {
                    Modal = true,
                    RequestedHeight = 500,
                    RequestedWidth = 350
                }
            );            

        }

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