using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using InABox.Core;
using Microsoft.Win32;

namespace InABox.DynamicGrid
{
    public class FileNameEditorControl : DynamicEditorControl<string, FileNameEditor>
    {
        static FileNameEditorControl()
        {
            //DynamicEditorControlFactory.Register<FileNameEditorControl, FileNameEditor>();
        }
        
        private TextBox Editor;

        public string Filter { get; set; }

        public bool AllowView { get; set; }

        public bool RequireExisting { get; set; }

        public override int DesiredHeight()
        {
            return 25;
        }

        public override int DesiredWidth()
        {
            return int.MaxValue;
        }

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

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


        public override void Configure()
        {
            Filter = EditorDefinition.FileMask;
            AllowView = EditorDefinition.AllowView;
            RequireExisting = EditorDefinition.RequireExisting;
        }

        protected override FrameworkElement CreateEditor()
        {
            var Grid = new Grid
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                HorizontalAlignment = HorizontalAlignment.Stretch
            };
            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });

            Editor = new TextBox
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(0, 0, 0, 0),
                IsEnabled = false
            };
            //Editor.LostFocus += (o, e) => CheckChanged();
            Editor.SetValue(Grid.ColumnProperty, 0);
            Editor.SetValue(Grid.RowProperty, 0);
            Grid.Children.Add(Editor);

            var Select = new Button
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(5, 1, 0, 1),
                Width = 45,
                Content = "Select",
                Focusable = false
            };
            Select.SetValue(Grid.ColumnProperty, 1);
            Select.SetValue(Grid.RowProperty, 0);
            Select.Click += Select_Click;
            Grid.Children.Add(Select);

            var Clear = new Button
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(5, 1, 0, 1),
                Width = 45,
                Content = "Clear",
                Focusable = false
            };
            Clear.SetValue(Grid.ColumnProperty, 2);
            Clear.SetValue(Grid.RowProperty, 0);
            Clear.Click += Clear_Click;
            Grid.Children.Add(Clear);

            if (AllowView)
            {
                var View = new Button
                {
                    VerticalAlignment = VerticalAlignment.Stretch,
                    VerticalContentAlignment = VerticalAlignment.Center,
                    HorizontalAlignment = HorizontalAlignment.Stretch,
                    Margin = new Thickness(5, 1, 0, 1),
                    Width = 45,
                    Content = "View",
                    Focusable = false
                };
                View.SetValue(Grid.ColumnProperty, 3);
                View.SetValue(Grid.RowProperty, 0);
                View.Click += View_Click;
                Grid.Children.Add(View);
            }

            return Grid;
        }

        private void Select_Click(object sender, RoutedEventArgs e)
        {
            if (RequireExisting)
            {
                var dlg = new OpenFileDialog();
                dlg.Filter = Filter;
                dlg.Title = "Open File";
                if (dlg.ShowDialog() == true)
                {
                    Editor.Text = dlg.FileName;
                    CheckChanged();
                }

                ;
            }
            else
            {
                var sfd = new SaveFileDialog();
                sfd.Filter = Filter;
                sfd.Title = "Open File";
                sfd.AddExtension = true;
                sfd.OverwritePrompt = false;
                if (sfd.ShowDialog() == true)
                {
                    Editor.Text = sfd.FileName;
                    CheckChanged();
                }
            }
        }

        private void View_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrWhiteSpace(Editor.Text))
            {
                MessageBox.Show("Please select a document first!");
                return;
            }

            var gsProcessInfo = new ProcessStartInfo();
            gsProcessInfo.Verb = "open";
            gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
            gsProcessInfo.FileName = Editor.Text;
            gsProcessInfo.UseShellExecute = true;

            Process.Start(gsProcessInfo);
        }

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

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

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