using InABox.Core;
using InABox.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace InABox.DynamicGrid
{
    public class DFSignatureControl : DynamicFormFieldControl<DFLayoutSignaturePad, DFLayoutSignaturePadProperties, byte[], byte[]?>
    {
        private Grid Grid = null!; // Late-initialised
        private Image Image = null!; // Late-initialised
        private bool _isEmpty = true;

        protected override FrameworkElement Create()
        {
            Grid = new Grid();
            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
            Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });

            Grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
            Grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });

            Image = new Image();
            Image.StretchDirection = StretchDirection.DownOnly;
            Image.Source = EmbeddedImageUtilities.CreateEmptySignature();

            var clearButton = new Button
            {
                Content = "Clear",
                Margin = new Thickness(0, 5, 5, 0),
                Width = 60,
                Height = 35
            };
            clearButton.Tag = Image;
            clearButton.Click += SignatureClear_Click;

            var editButton = new Button
            {
                Content = "Edit",
                Margin = new Thickness(0, 5, 0, 0),
                Width = 60,
                Height = 35
            };
            editButton.Tag = Image;
            editButton.Click += SignatureEdit_Click;

            Image.SetGridPosition(0, 0, 1, 3);
            clearButton.SetGridPosition(1, 1, 1, 1);
            editButton.SetGridPosition(1, 2, 1, 1);
            Grid.Children.Add(Image);
            Grid.Children.Add(clearButton);
            Grid.Children.Add(editButton);

            return Grid;
        }

        public override byte[]? GetSerializedValue()
        {
            return EmbeddedImageUtilities.SaveImageToBytes(Image, _isEmpty, new PngBitmapEncoder());
        }

        public override void SetSerializedValue(byte[]? value)
        {
            SetValue(value);
        }

        private void SignatureClear_Click(object sender, RoutedEventArgs e)
        {
            if (!_isEmpty)
            {
                Image.Source = EmbeddedImageUtilities.CreateEmptySignature();
                _isEmpty = true;
                ChangeField();
            }
        }

        private void SignatureEdit_Click(object sender, RoutedEventArgs e)
        {
            var window = new SignaturePadWindow(_isEmpty ? null : Image.Source);

            if (window.ShowDialog() == true)
            {
                Image.Source = window.Image ?? EmbeddedImageUtilities.CreateEmptySignature();
                _isEmpty = window.Image == null;
                ChangeField();
            }
        }

        public override byte[] GetValue()
        {
            return EmbeddedImageUtilities.SaveImageToBytes(Image, _isEmpty, new PngBitmapEncoder()) ?? Array.Empty<byte>();
        }

        public override void SetValue(byte[]? value)
        {
            _isEmpty = !EmbeddedImageUtilities.LoadImageFromData(Image, value, EmbeddedImageUtilities.CreateEmptySignature);
        }

        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == IsEnabledProperty)
            {
                Grid.RowDefinitions[1].Height = (bool)e.NewValue
                    ? GridLength.Auto
                    : new GridLength(0);
            }
        }
        protected override bool IsEmpty() => _isEmpty;
    }
}
