using InABox.Core;
using InABox.WPF;
using Microsoft.Win32;
using Syncfusion.XPS;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace InABox.DynamicGrid
{
    public class DFVideoControl : DynamicFormFieldControl<DFLayoutVideoField, DFLayoutVideoFieldProperties, byte[]>
    {
        Button PlayButton;
        Button PauseButton;
        Button DownloadButton;
        MediaElement Player;
        Grid grid;
        Point center;
        int rotation = 90;
        bool firstPlay = true;

        byte[]? Data;

        protected override FrameworkElement Create()
        {
            grid = new Grid();

            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
            grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });

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

            PlayButton = new Button
            {
                Width = 25,
                Height = 25,
                Margin = new Thickness(5)
            };

            PlayButton.Content = new Image
            {
                Source = Wpf.Resources.play_button.AsBitmapImage(),
            };
            PlayButton.Click += PlayButton_Click;

            PauseButton = new Button
            {
                Width = 25,
                Height = 25,
                Margin = new Thickness(5)
            };
            PauseButton.Content = new Image
            {
                Source = Wpf.Resources.pause_button.AsBitmapImage(),
            };
            PauseButton.Click += PauseButton_Click;

            DownloadButton = new Button
            {
                Width = 25,
                Height = 25,
                Margin = new Thickness(5)
            };
            DownloadButton.Content = new Image
            {
                Source = Wpf.Resources.download.AsBitmapImage(),
            };
            DownloadButton.Click += DownloadButton_Click;

            StackPanel panel = new StackPanel();
            panel.Orientation = Orientation.Horizontal;
            panel.Children.Add(PlayButton);
            panel.Children.Add(PauseButton);
            panel.Children.Add(DownloadButton);
            panel.HorizontalAlignment = HorizontalAlignment.Center;
            panel.SetGridPosition(1, 0, 1, 2);

            Player = new MediaElement();
            Player.Stretch = System.Windows.Media.Stretch.Fill;
            Player.Visibility = Visibility.Visible;
            Player.VerticalAlignment = VerticalAlignment.Center;
            Player.HorizontalAlignment = HorizontalAlignment.Center;
            Player.SetGridPosition(0, 0, 1, 2);
            Player.LoadedBehavior = MediaState.Manual;

            grid.Children.Add(panel);
            grid.Children.Add(Player);

            return grid;
        }

        private void DownloadButton_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new SaveFileDialog();
            dlg.AddExtension = true;
            dlg.DefaultExt = ".mp4";
            dlg.ShowDialog();
            if (!string.IsNullOrWhiteSpace(dlg.FileName))
            {
                System.IO.File.WriteAllBytes(dlg.FileName, GetValue());
                MessageBox.Show("Success - file saved to: "
                    + System.Environment.NewLine
                    + dlg.FileName);

                var dir = System.IO.Path.GetDirectoryName(dlg.FileName);

                ProcessStartInfo startInfo = new ProcessStartInfo
                {
                    Arguments = dir,
                    FileName = "explorer.exe"
                };
                Process.Start(startInfo);
            }
        }

        private void PauseButton_Click(object sender, RoutedEventArgs e)
        {
            Player.Pause();
        }

        private void PlayButton_Click(object sender, RoutedEventArgs e)
        {
            var path = System.IO.Path.GetTempFileName();
            path = path.Substring(0, path.Length - 4) + ".mp4";

            System.IO.File.WriteAllBytes(path, GetValue());

            Player.Source = new Uri(path);

            Player.Play();
        }


        public override byte[] GetValue()
        {
            return Data ?? Array.Empty<byte>();
        }

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

        protected override bool IsEmpty() => Data is null || Data.Length == 0;
    }
}
