using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using InABox.Core;
using InABox.WPF;
using Microsoft.Win32;
using Color = System.Windows.Media.Color;
using Image = System.Drawing.Image;

namespace InABox.DynamicGrid
{
    public class EmbeddedImageEditorControl : DynamicEditorControl<byte[]?, EmbeddedImageEditor>
    {
        
        static EmbeddedImageEditorControl()
        {
            //DynamicEditorControlFactory.Register<EmbeddedImageEditorControl, EmbeddedImageEditor>();
        }
        
        private byte[]? data;

        private Button Select = null!;

        public EmbeddedImageEditorControl()
        {
            MaximumHeight = int.MaxValue;
            MaximumWidth = int.MaxValue;
            MaximumFileSize = int.MaxValue;
        }

        public int MaximumHeight { get; set; }

        public int MaximumWidth { get; set; }

        public int MaximumFileSize { get; set; }

        public override void Configure()
        {
            MaximumHeight = EditorDefinition.MaximumHeight;
            MaximumWidth = EditorDefinition.MaximumWidth;
            MaximumFileSize = EditorDefinition.MaximumFileSize;
        }

        protected override FrameworkElement CreateEditor()
        {
            var grid = new Grid();
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(30, GridUnitType.Pixel) });

            Select = new Button
            {
                Content = "Select",
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center
            };
            Select.SetValue(Grid.ColumnProperty, 0);
            Select.Click += Select_Click;
            grid.Children.Add(Select);

            var View = new Button
            {
                Content = "..",
                HorizontalAlignment = HorizontalAlignment.Stretch,
                VerticalAlignment = VerticalAlignment.Stretch,
                VerticalContentAlignment = VerticalAlignment.Center,
                Margin = new Thickness(5, 0, 0, 0)
            };
            View.SetValue(Grid.ColumnProperty, 1);
            View.Click += View_Click;
            grid.Children.Add(View);

            return grid;
        }


        private void Select_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new OpenFileDialog();
            dlg.Filter = "Image Files (*.jpg;*.png;*.bmp;*.gif)|*.png;*.jpg;*.jpeg;*.bmp;*.gif";
            if (dlg.ShowDialog() == true)
            {
                if (new FileInfo(dlg.FileName).Length > 32000)
                    MessageBox.Show("File must be less than 32kb");
                var image = Image.FromFile(dlg.FileName);
                var size = image.Size.Adjust(MaximumWidth, MaximumHeight);
                image = new Bitmap(image, size);
                data = new ImageConverter().ConvertTo(image, typeof(byte[])) as byte[];
                CheckChanged();
            }
        }

        private void View_Click(object sender, RoutedEventArgs e)
        {
            if (data == null || data.Length == 0)
                return;
            var type = ImageUtils.GetImageType(data);
            var file = Path.ChangeExtension(Path.GetTempFileName(), type.ToString());
            File.WriteAllBytes(file, data);

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


        public override int DesiredHeight()
        {
            return 25;
        }

        public override int DesiredWidth()
        {
            return 150;
        }

        protected override byte[]? RetrieveValue()
        {
            return data;
        }

        protected override void UpdateValue(byte[]? value)
        {
            data = value;
        }

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

        public override void SetColor(Color color)
        {
        }
    }
}