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

namespace InABox.DynamicGrid
{
    public class TimestampEditorControl : DynamicEditorControl<DateTime, TimestampEditor>
    {
        
        static TimestampEditorControl()
        {
            //DynamicEditorControlFactory.Register<TimestampEditorControl, TimestampEditor>();
        }
        
        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:ss",
                HorizontalAlignment = HorizontalAlignment.Left,
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                ShowButtonSpinner = false,
                //ShowDropDownButton = false,
                Focusable = false,
                IsReadOnly = true,
                Background = new SolidColorBrush(Colors.Gainsboro),
                Width = 150
            };
            Editor.SetValue(DockPanel.DockProperty, Dock.Left);
            
            Button = new Button
            {
                Content = "Set",
                Width = 45,
                Margin = new Thickness(5, 0, 0, 0),
                Focusable = true
            };
            Button.SetValue(DockPanel.DockProperty, Dock.Right);
            Button.Click += ButtonClick;

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

            return DockPanel;
        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            if (((string)Button.Content).Equals("Set"))
                Editor.Value = DateTime.Now;
            else
                Editor.Value = null;
            Button.Content = Editor.Value.HasValue ? "Clear" : "Set";
            CheckChanged();
        }

        public override int DesiredHeight()
        {
            return 25;
        }

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

        protected override DateTime RetrieveValue()
        {
            if (Editor.Value.HasValue)
                return Editor.Value.Value;
            return DateTime.MinValue;
        }

        protected override void UpdateValue(DateTime value)
        {
            if (value != DateTime.MinValue)
            {
                Editor.Value = value;
                Button.Content = "Clear"; // Editor.Value.Equals(DateTime.MinValue) ? "Set" : "Clear";
            }
            else
            {
                Editor.Value = null;
                Button.Content = "Set";
            }
        }

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


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