namespace InABox.DynamicGrid;
#if USEIMAGES
    public class FileEditorControl<T> : DynamicEditorControl<DataFile> where T : DataFile, new()
    {

        private TextBox Editor = null;

        T _value = null;

        protected override FrameworkElement CreateEditor()
        {

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

            Editor = new TextBox()
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Stretch,
            };
            Editor.TextChanged += (o, e) => Changed = true;
            Editor.SetValue(Grid.ColumnProperty, 0);
            Editor.SetValue(Grid.RowProperty, 0);
            Grid.Children.Add(Editor);

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

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

            Button View = new Button()
            {
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(5, 0, 0, 0),
                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 View_Click(object sender, RoutedEventArgs e)
        {
            if (String.IsNullOrEmpty(_value.FileName) || (_value.Data == null) || (_value.Data.Length == 0))
            {
                System.Windows.MessageBox.Show("No File Selected!");
                return;
            }
            String ext = System.IO.Path.GetExtension(_value.FileName);
            String filename = System.IO.Path.ChangeExtension(System.IO.Path.GetTempFileName(), ext);
            File.WriteAllBytes(filename, _value.Data);

            ProcessStartInfo gsProcessInfo = new ProcessStartInfo();
            gsProcessInfo.Verb = "open";
            gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
            gsProcessInfo.FileName = filename;
            gsProcessInfo.UseShellExecute = true;
            Process.Start(gsProcessInfo);
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            _value.FileName = "";
            _value.Data = new byte[] { };
            Editor.Text = "";
        }

        private void Select_Click(object sender, RoutedEventArgs e)
        {
            VistaOpenFileDialog dlg = new VistaOpenFileDialog();
            dlg.Filter = _value.Filter();
            if (dlg.ShowDialog() == true)
            {
                _value.FileName = System.IO.Path.GetFileName(dlg.FileName);
                _value.Data = File.ReadAllBytes(dlg.FileName);
                Editor.Text = _value.FileName;
            }
        }

        protected override int DesiredHeight()
        {
            return 25;
        }

        protected override DataFile GetValue()
        {
            return _value;
        }

        protected override void SetValue(DataFile value)
        {
            T file = (T)value;

            T newfile = new T();  //(DataFile)Activator.CreateInstance(file != null ? value.GetType() : typeof(MiscellaneousDataFile));
            newfile.FileName = file != null ? file.FileName : "";
            newfile.Data = file != null ? file.Data : new byte[] { };
            Editor.Text = file != null ? file.FileName : "";
            _value = newfile;
        }

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

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

    public class ImageFileEditorControl : FileEditorControl<ImageDataFile> { }

    public class PDFFileEditorControl : FileEditorControl<PDFDataFile> { }

    public class MiscellaneousFileEditorControl : FileEditorControl<MiscellaneousDataFile> { }
#endif