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 DurationEditorControl : DynamicEditorControl<TimeSpan, DurationEditor>
    {
        
        static DurationEditorControl()
        {
            //DynamicEditorControlFactory.Register<DurationEditorControl, DurationEditor>();
        }
        
        //private DateTimeEdit Editor = null;
        private TimeSpanEdit Editor;

        private bool IsChanged;

        private Button LessButton;
        private Button MoreButton;
        public override void Configure()
        {
        }

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

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


            Editor = new TimeSpanEdit
            {
                Format = "h:mm",
                MinValue = new TimeSpan(),
                MaxValue = TimeSpan.MaxValue,
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                ShowArrowButtons = 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 TimeSpanEdit).SelectionStart
                //    , (o as TimeSpanEdit).SelectionLength
                //    , (o as TimeSpanEdit).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.ValueChanged += (o, e) =>
            {
                IsChanged = true;
                CheckChanged();
            };

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

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

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

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

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

            Stack.Children.Add(MoreButton);
            Stack.Children.Add(LessButton);
            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.Value.HasValue)
                result = Editor.Value.Value;

            return result;
        }

        protected override void UpdateValue(TimeSpan value)
        {
            Editor.Value = value;
        }

        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))
                return;

            var buttons = LessButton != null && MoreButton != null
                ? (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 = (HorizontalAlignment != HorizontalAlignment.Stretch ? Math.Min(width, MaxWidth) : width) - buttons;
        }
    }
}