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

namespace InABox.DynamicGrid
{
    public class PasswordEditorControl : DynamicEditorControl<string, PasswordEditor>
    {

        static PasswordEditorControl()
        {
            //DynamicEditorControlFactory.Register<PasswordEditorControl, PasswordEditor>();
        }
        
        private bool _viewbuttonvisible;
        private Button Button;
        private DockPanel DockPanel;
        private PasswordBox Editor;
        private TextBox Viewer;

        public bool ViewButtonVisible
        {
            get => _viewbuttonvisible;
            set
            {
                _viewbuttonvisible = value;
                if (Button != null)
                    Button.Visibility = value ? Visibility.Visible : Visibility.Collapsed;
            }
        }

        public override void Configure()
        {
            if (EditorDefinition is not PasswordEditor editor) return;

            ViewButtonVisible = editor.ViewButtonVisible;
        }

        protected override FrameworkElement CreateEditor()
        {
            if (EditorDefinition != null && EditorDefinition is PasswordEditor passwordEditor)
            {
                ViewButtonVisible = passwordEditor.ViewButtonVisible;
            }

            DockPanel = new DockPanel
            {
                HorizontalAlignment = HorizontalAlignment.Stretch
            };


            Editor = new PasswordBox
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center
            };
            Editor.SetValue(DockPanel.DockProperty, Dock.Left);
            Editor.PasswordChanged += (o, e) => CheckChanged();

            Viewer = new TextBox
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                IsReadOnly = true
                //Visibility = Visibility.Collapsed
            };
            Viewer.SetValue(DockPanel.DockProperty, Dock.Left);

            Button = new Button
            {
                Content = "..",
                Margin = new Thickness(5, 0, 0, 0),
                Visibility = _viewbuttonvisible ? Visibility.Visible : Visibility.Collapsed,
                Width = 30
            };
            Button.PreviewMouseDown += Button_MouseDown;
            Button.PreviewMouseUp += Button_MouseUp;
            Button.SetValue(DockPanel.DockProperty, Dock.Right);

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


            return DockPanel;
        }

        private void Button_MouseDown(object sender, MouseButtonEventArgs e)
        {
            Viewer.Text = Editor.Password;
            DockPanel.Children.Remove(Editor);
            DockPanel.Children.Add(Viewer);
            //Editor.Visibility = Visibility.Collapsed;
            //Viewer.Visibility = Visibility.Visible;
        }

        private void Button_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Viewer.Text = "";
            DockPanel.Children.Remove(Viewer);
            DockPanel.Children.Add(Editor);
            //Viewer.Visibility = Visibility.Collapsed;
            //Editor.Visibility = Visibility.Visible;
        }

        public override int DesiredHeight()
        {
            return 25;
        }

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

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

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

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

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