using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using InABox.Clients;
using InABox.Core;
using InABox.WPF;

namespace InABox.DynamicGrid
{
    public class PINEditorControl : DynamicEditorControl<string, PINEditor>
    {
        
        static PINEditorControl()
        {
            //DynamicEditorControlFactory.Register<PINEditorControl, PINEditor>();
        }
        
        private Button ClearPIN;
        private Button CreatePIN;

        private TextBox Editor;

        public override void Configure()
        {
        }

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

            Editor = new TextBox
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalContentAlignment = HorizontalAlignment.Center,
                Focusable = false,
                IsReadOnly = true,
                Background = new SolidColorBrush(Colors.Gainsboro)
            };
            Editor.SetValue(DockPanel.DockProperty, Dock.Left);
            Editor.TextChanged += (o, e) => CheckChanged();

            CreatePIN = new Button
            {
                Content = "Create",
                Width = 50,
                Margin = new Thickness(5, 0, 0, 0),
                Focusable = true
            };
            CreatePIN.SetValue(DockPanel.DockProperty, Dock.Right);
            CreatePIN.Click += CreatePINClick;

            ClearPIN = new Button
            {
                Content = "Clear",
                Width = 50,
                Margin = new Thickness(5, 0, 0, 0),
                Focusable = true
            };
            ClearPIN.SetValue(DockPanel.DockProperty, Dock.Right);
            ClearPIN.Click += ClearPINClick;

            DockPanel.Children.Add(ClearPIN);
            DockPanel.Children.Add(CreatePIN);
            DockPanel.Children.Add(Editor);

            return DockPanel;
        }


        private void CreatePINClick(object sender, RoutedEventArgs e)
        {
            using (var cursor = new WaitCursor())
            {
                while (true)
                {
                    var random = new Random(DateTime.Now.Millisecond);
                    var pin = "";
                    for (int i = 0; i < ClientFactory.PINLength; i++)
                        pin += random.Next(10).ToString();
                    var ok = new Client<User>().Load(new Filter<User>(x => x.PIN).IsEqualTo(pin)).FirstOrDefault() == null;
                    if (ok)
                    {
                        Editor.Text = pin;
                        break;
                    }
                }
            }
        }

        private void ClearPINClick(object sender, RoutedEventArgs e)
        {
            Editor.Text = "";
        }

        public override int DesiredHeight()
        {
            return 25;
        }

        public override int DesiredWidth()
        {
            return 150 + (int)CreatePIN.Width + (int)ClearPIN.Width + 10;
            ;
        }

        protected override string RetrieveValue()
        {
            return Editor.Text;
        }

        protected override void UpdateValue(string value)
        {
            Editor.Text = value;
        }

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

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