using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using InABox.Core;
using Syncfusion.Windows.Shared;

//using Xceed.Wpf.Toolkit;

namespace InABox.DynamicGrid
{
    public class TimeOfDayEditorControl : DynamicEditorControl<TimeSpan, TimeOfDayEditor>
    {

        static TimeOfDayEditorControl()
        {
            //DynamicEditorControlFactory.Register<TimeOfDayEditorControl, TimeOfDayEditor>();
        }
        
        public static readonly DependencyProperty NowButtonVisibleProperty =
            DependencyProperty.Register("NowButtonVisible", typeof(bool), typeof(DynamicSplitPanel), new UIPropertyMetadata(false));

        private bool _nowbuttonvisible = true;
        private DateTimeEdit Editor;

        private bool IsChanged;
        private Button LessButton;

        private Button MoreButton;
        //private TimeSpanEdit Editor = null;

        private Button NowButton;

        public TimeOfDayEditorControl()
        {
            MaxWidth = 260;
            HorizontalAlignment = HorizontalAlignment.Left;
        }

        public override void Configure()
        {
            NowButtonVisible = false;
        }

        public bool NowButtonVisible
        {
            get => _nowbuttonvisible;
            set
            {
                _nowbuttonvisible = value;
                if (NowButton != null)
                    NowButton.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
                if (LessButton != null)
                    LessButton.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
                if (MoreButton != null)
                    MoreButton.Visibility = value ? Visibility.Collapsed : Visibility.Visible;
            }
        }

        protected override FrameworkElement CreateEditor()
        {
            var Stack = new DockPanel
            {
                //Orientation = Orientation.Horizontal,
                HorizontalAlignment = HorizontalAlignment.Stretch
            };


            Editor = new DateTimeEdit
            {
                Pattern = DateTimePattern.CustomPattern,
                CustomPattern = "HH:mm", //DateTimeFormat.Custom,
                //FormatString = "HH:mm",
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                //TimeInterval = new TimeSpan(0, 15, 0),
                //ShowButtonSpinner = false
                DropDownView = DropDownViews.Clock,
                IsButtonPopUpEnabled = false
            };

            Editor.SetValue(DockPanel.DockProperty, Dock.Left);


            Editor.PreviewKeyDown += (o, e) =>
            {
                //Logger.Send(LogType.Information, "", String.Format("DurationEditor:PreviewKeyDown {0} {1} {2} {3}",
                //    e.Key.ToString()
                //    , (o as DateTimeEdit).SelectionStart
                //    , (o as DateTimeEdit).SelectionLength
                //    , (o as DateTimeEdit).SelectedText
                //));

                var separator = Editor.Text.IndexOf(":");

                if (e.Key == Key.OemPeriod)
                {
                    Editor.Select(separator + 1, 2);
                    e.Handled = true;
                }
                else if (e.Key == Key.Back)
                {
                    if (string.Equals(Editor.SelectedText, "00"))
                        Editor.Select(0, separator);
                    else
                        Editor.SelectedText = "00";
                    e.Handled = true;
                }
                else if (e.Key == Key.Tab)
                {
                    if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.LeftShift))
                    {
                        if (Editor.SelectionStart > separator)
                        {
                            Editor.Select(0, separator);
                            e.Handled = true;
                        }
                    }
                    else
                    {
                        if (Editor.SelectionLength != Editor.Text.Length && Editor.SelectionStart < separator)
                        {
                            Editor.Select(separator + 1, 2);
                            e.Handled = true;
                        }
                    }
                }
            };

            Editor.DateTimeChanged += (o, e) =>
            {
                IsChanged = true;
                CheckChanged();
            };

            Editor.LostFocus += (o, e) =>
            {
                if (IsChanged)
                    CheckChanged();
            };

            NowButton = new Button
            {
                Content = "Now",
                Width = 45,
                Margin = new Thickness(5, 0, 0, 0),
                Focusable = false,
                Visibility = _nowbuttonvisible ? Visibility.Visible : Visibility.Collapsed
            };
            NowButton.SetValue(DockPanel.DockProperty, Dock.Right);
            NowButton.Click += (o, e) =>
            {
                Editor.DateTime = DateTime.MinValue.Add(new TimeSpan(DateTime.Now.TimeOfDay.Hours, DateTime.Now.TimeOfDay.Minutes, 0));
                CheckChanged();
            };

            LessButton = new Button
            {
                Content = "<",
                Width = 23,
                Margin = new Thickness(2, 0, 0, 0),
                Focusable = false,
                Visibility = _nowbuttonvisible ? Visibility.Collapsed : Visibility.Visible
            };
            LessButton.SetValue(DockPanel.DockProperty, Dock.Right);

            LessButton.Click += (o, e) =>
            {
                Editor.DateTime = Editor.DateTime.HasValue && Editor.DateTime.Value.TimeOfDay >= new TimeSpan(0, 15, 0)
                    ? Editor.DateTime.Value.Subtract(new TimeSpan(0, 15, 0))
                    : Editor.DateTime;
                CheckChanged();
            };

            MoreButton = new Button
            {
                Content = ">",
                Width = 23,
                Margin = new Thickness(2, 0, 0, 0),
                Focusable = false,
                Visibility = _nowbuttonvisible ? Visibility.Collapsed : Visibility.Visible
            };
            MoreButton.SetValue(DockPanel.DockProperty, Dock.Right);

            MoreButton.Click += (o, e) =>
            {
                Editor.DateTime = Editor.DateTime.HasValue && Editor.DateTime.Value.TimeOfDay < new TimeSpan(23, 45, 0)
                    ? Editor.DateTime.Value.Add(new TimeSpan(0, 15, 0))
                    : Editor.DateTime;
                CheckChanged();
            };

            Stack.Children.Add(MoreButton);
            Stack.Children.Add(LessButton);
            Stack.Children.Add(NowButton);
            Stack.Children.Add(Editor);

            return Stack;
        }

        public override int DesiredHeight()
        {
            return 25;
        }

        public override int DesiredWidth()
        {
            return 150;
        }

        protected override TimeSpan RetrieveValue()
        {
            var result = new TimeSpan(0);

            if (Editor.DateTime.HasValue)
                result = Editor.DateTime.Value.TimeOfDay;

            result = new TimeSpan(result.Hours, result.Minutes, 0);

            return result;
        }

        protected override void UpdateValue(TimeSpan value)
        {
            if (value.Ticks > 0)
                Editor.DateTime = DateTime.MinValue.Add(value);
            else
                Editor.DateTime = null;
        }

        public override void SetFocus()
        {
            Editor.Focus();
        }

        public override void SetColor(Color color)
        {
            Editor.Background = new SolidColorBrush(color);
        }

        protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
        {
            base.OnRenderSizeChanged(sizeInfo);

            ResizeEditor(sizeInfo.NewSize.Width);

            //if (Editor.ActualWidth > 150.0F)
            //    Editor.Width = 150;
        }

        private void ResizeEditor(double width)
        {
            if (double.IsNaN(width) || width.Equals(0.0F) || Editor == null)
                return;

            var buttons = NowButton != null && LessButton != null && MoreButton != null
                ? (NowButton.Visibility == Visibility.Visible ? NowButton.ActualWidth + NowButton.Margin.Left + NowButton.Margin.Right : 0F) +
                  (LessButton.Visibility == Visibility.Visible ? LessButton.ActualWidth + LessButton.Margin.Left + LessButton.Margin.Right : 0F) +
                  (MoreButton.Visibility == Visibility.Visible ? MoreButton.ActualWidth + MoreButton.Margin.Left + MoreButton.Margin.Right : 0F)
                : 0.0F;
            Editor.Width = Math.Max(0, (HorizontalAlignment != HorizontalAlignment.Stretch ? Math.Min(width, MaxWidth) : width) - buttons);
        }
    }
}