using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Media;
using InABox.Core;
using Button = System.Windows.Controls.Button;
using HorizontalAlignment = System.Windows.HorizontalAlignment;
using TextBox = System.Windows.Controls.TextBox;

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

        public Environment.SpecialFolder InitialFolder { get; set; }

        public override int DesiredHeight()
        {
            return 25;
        }

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


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

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

        public override void Configure()
        {
            InitialFolder = EditorDefinition.InitialFolder;
        }

        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(50, GridUnitType.Pixel) });
            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50, GridUnitType.Pixel) });

            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),
                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),
                Content = "Clear",
                Focusable = false
            };
            Clear.SetValue(Grid.ColumnProperty, 2);
            Clear.SetValue(Grid.RowProperty, 0);
            Clear.Click += Clear_Click;
            Grid.Children.Add(Clear);

            return Grid;
        }

        private void Select_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new FolderBrowserDialog();
            dlg.SelectedPath = string.IsNullOrWhiteSpace(Editor.Text) ? Environment.GetFolderPath(InitialFolder) : Editor.Text;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Editor.Text = dlg.SelectedPath;
                CheckChanged();
            }
        }

        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;
        }
    }
}