using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Bellwether.UIComponent
{
    /// <summary>
    /// Interaction logic for BufferedMediaLoad.xaml
    /// </summary>
    public partial class BufferedMediaLoad : UserControl
    {
        //圖片規格
        private static string[] imgDimList = { ".jpg", ".png", ".bmp", ".jpeg" };

        private delegate void AddImageEvent(ImageSource source);
        private AddImageEvent AddPreviewHandler;
        private AddImageEvent AddImageHandler;

        private string filePath;
        private bool isImageSource;
        private int imageSourceHeight;
        private int imageSourceWidth;
        private double imageSourceRatio
        {
            get
            {
                return imageSourceWidth / (imageSourceHeight == 0 ? 1 : imageSourceHeight);
            }
        }

        public BufferedMediaLoad(string filePath)
        {
            InitializeComponent();

            AddPreviewHandler += AddPreview;
            AddImageHandler += AddImage;

            this.filePath = System.IO.Path.GetFullPath(filePath);
            string ext = System.IO.Path.GetExtension(filePath).ToLower();
            if (imgDimList.Contains(ext))
            {
                isImageSource = true;
            }
            else
            {
                throw new Exception("<BufferedMediaLoad>:File not supportted");
            }

            //uxMainGrid.Children.Add(new TextBlock() { Text = "Loading...", VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 48, Foreground = new SolidColorBrush(Colors.White), Background = new SolidColorBrush(Colors.Black) });
            Thread shellLoad = new Thread(shellLoader);
            shellLoad.Start();
            this.Loaded += BufferedMediaLoad_Loaded;
        }

        private void BufferedMediaLoad_Loaded(object sender, RoutedEventArgs e)
        {
            Thread ImageLoad = new Thread(ImageLoader);
            ImageLoad.Start();
        }

        private void shellLoader()
        {
            //BitmapSource source = null;
            ImageSource source = null; ;
            try
            {
                //ShellFile shellFile = ShellFile.FromFilePath(filePath);
                //if (isImageSource)
                //{
                //    //var test = shellFile.Properties.System.Image.HorizontalSize.Value;
                //    imageSourceHeight = (int)shellFile.Properties.System.Image.HorizontalSize.Value;
                //    imageSourceWidth = (int)shellFile.Properties.System.Image.VerticalSize.Value;
                //}
                ////let bitmap can correctly send to uithread
                //if (shellFile != null && shellFile.Thumbnail != null && shellFile.Thumbnail.BitmapSource != null)
                //    source = shellFile.Thumbnail.BitmapSource;
                //source.Freeze();
                source = Utility.GlobalFunction.GetBitMap(filePath, 800, 600);
                source.Freeze();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            if (source != null)
                this.Dispatcher.Invoke(AddPreviewHandler, source);
        }

        private void AddPreview(ImageSource s)
        {
            Image preview = new Image();
            preview.Source = s;
            uxPreviewContainer.Children.Add(preview);
        }

        private void ImageLoader()
        {
            //ImageSource s = GetBitMap(filePath);
            ImageSource s = Utility.GlobalFunction.GetBitMap(filePath, 1920, 1080);
            if (s != null)
                this.Dispatcher.Invoke(AddImageHandler, s);
        }

        private void AddImage(ImageSource s)
        {
            Image image = new Image();
            image.Source = s;
            RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.HighQuality);
            uxImageContainer.Children.Add(image);
        }
    }
}
