using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using InABox.Core;
using Xceed.Wpf.Toolkit;

namespace InABox.DynamicGrid
{
    public class DateTimeEditorControl : DynamicEditorControl<DateTime, DateTimeEditor>
    {
        
        static DateTimeEditorControl()
        {
            //DynamicEditorControlFactory.Register<DateTimeEditorControl, DateTimeEditor>();
        }
        
        private Button Button;
        private DateTimePicker Editor;

        public override void Configure()
        {
        }

        protected override FrameworkElement CreateEditor()
        {
            var DockPanel = new DockPanel
            {
                HorizontalAlignment = HorizontalAlignment.Stretch
            };

            Editor = new DateTimePicker
            {
                Format = DateTimeFormat.Custom,
                FormatString = "dd MMM yyyy HH:mm",
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                CalendarDisplayMode = CalendarMode.Month,
                ShowButtonSpinner = false,
                Width=150
            };

            Editor.GotFocus += (o, e) => { new Timer(s => { Dispatcher.Invoke(() => { Editor.SelectAll(); }); }, Editor, 100, Timeout.Infinite); };

            Editor.ValueChanged += (o, e) =>
            {
                if (Editor.Value.HasValue && Editor.Value.Value.Year < 100)
                    Editor.Value = Editor.Value.Value.AddYears(2000);
                CheckChanged();
            };
            Editor.SetValue(DockPanel.DockProperty, Dock.Left);

            Button = new Button
            {
                Content = "Now",
                Width = 45,
                Margin = new Thickness(5, 0, 0, 0),
                Focusable = false
            };
            Button.Click += (o, e) => Editor.Value = DateTime.Today.AddHours(DateTime.Now.TimeOfDay.Hours).AddMinutes(DateTime.Now.TimeOfDay.Minutes);
            Button.SetValue(DockPanel.DockProperty, Dock.Right);

            DockPanel.Children.Add(Button);
            DockPanel.Children.Add(Editor);

            return DockPanel;
        }

        public override int DesiredHeight()
        {
            return 25;
        }

        public override int DesiredWidth()
        {
            return 150 + (int)Button.Width + 5;
        }

        protected override DateTime RetrieveValue()
        {
            var result = DateTime.MinValue;
            if (Editor.Value.HasValue)
            {
                if (Editor.Value.Value.Year < 100)
                    result = Editor.Value.Value.AddYears(2000);
                else
                    result = Editor.Value.Value;
            }

            return result;
        }

        protected override void UpdateValue(DateTime value)
        {
            if (value != DateTime.MinValue)
                Editor.Value = value;
            else
                Editor.Value = null;
        }

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

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