using MediaViewerLib.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
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.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HistoryDLL
{
    /// <summary>
    /// ThumbnailContainer.xaml 的互動邏輯
    /// </summary>
    public partial class ThumbnailContainer : UserControl
    {
        private Storyboard sbShowStart;
        private Storyboard sbShowStop;
        private List<string> imgPath;
        private int imgWidth = 211;
        private int imgHeight = 158;
        private int topBottomDiff = 10;

        public List<string> ImgPath
        {
            get { return imgPath; }
            set
            {
                imgPath = value;
                LoadThumbImage();
            }
        }

        public ThumbnailContainer()
        {
            InitializeComponent();
            if (GlobalFunction.isSolutionUsing4K)
            {
                imgWidth *= 2;
                imgHeight *= 2;
                //topBottomDiff *= 2;
            }

            sbShowStart = FindResource("sbShowStart") as Storyboard;
            sbShowStart.Completed += new EventHandler(sbShowStart_Completed);
            sbShowStop = FindResource("sbShowStop") as Storyboard;
            sbShowStop.Completed += new EventHandler(sbShowStop_Completed);
        }

        private void sbShowStop_Completed(object sender, EventArgs e)
        {
            uxStackPanel.Opacity = 0.0;
        }

        private void sbShowStart_Completed(object sender, EventArgs e)
        {
            uxStackPanel.Opacity = 1.0;
        }

        public void StartOpacityAnimation()
        {
            if (sbShowStart != null)
            {
                sbShowStart.Begin();
            }
        }

        public void StopOpacityAnimation()
        {
            if (sbShowStop != null)
            {
                sbShowStop.Begin();
            }
        }

        private void LoadThumbImage()
        {
            uxStackPanel.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;
            SolidColorBrush brush = new SolidColorBrush(ConfigSettingClass.EventCollect.BackgroundColor);

            if (imgPath.Count == 0)
            {
                Image img = new Image();

                if (File.Exists(GlobalFunction.thumbPath + ConfigSettingClass.MainBackCollect.ImgLogo))
                {
                    Grid gd = new Grid();
                    Rectangle rectange = new Rectangle();
                    //ImageBrush imagebrush = new ImageBrush(new BitmapImage(new Uri(@"pack://application:,,,/" + GlobalFunction.projectname + @";component/Data/Thumb/shadow_big.png", UriKind.RelativeOrAbsolute)));
                    ImageBrush imagebrush = null;

                    gd.Width = imgWidth + (GlobalFunction.isSolutionUsing4K ? 8 : 5);
                    gd.Height = imgHeight + (GlobalFunction.isSolutionUsing4K ? 8 : 5);
                    //imagebrush.Stretch = Stretch.Fill;
                    //gd.Background = imagebrush;
                    gd.Background =Brushes.Gray;
                    gd.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    gd.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    rectange.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    rectange.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    img.Width = rectange.Width = imgWidth;
                    img.Height = rectange.Height = imgHeight;
                    //img.Margin = new Thickness(-(GlobalFunction.isSolutionUsing4K ? 10 : 5), -(GlobalFunction.isSolutionUsing4K ? 10 : 5), 0, 0);
                    img.Margin = rectange.Margin = new Thickness(3, 2, 0, 0);
                    img.Source = new BitmapImage(new Uri(GlobalFunction.thumbPath + ConfigSettingClass.MainBackCollect.ImgLogo));
                    rectange.Fill = brush;
                    gd.Children.Add(rectange);
                    gd.Children.Add(img);
                    gd.Margin = GlobalFunction.isSolutionUsing4K ? new Thickness(0, 0, 0, topBottomDiff) : new Thickness(0, 0, 0, 0);
                    uxStackPanel.Children.Add(gd);
                }
            }
            else
            {
                for (int i = 0; i < imgPath.Count; i++)
                {
                    BitmapSource src = MediaUtilties.LoadThumbnailImage(imgPath[i], ThumbnailSize.Large);
                    Grid gd = new Grid();
                    //ImageBrush imagebrush = new ImageBrush(new BitmapImage(new Uri(@"pack://application:,,,/" + GlobalFunction.projectname + @";component/Data/Thumb/shadow_big.png", UriKind.RelativeOrAbsolute)));
                    ImageBrush imagebrush = new ImageBrush(new BitmapImage(new Uri(System.IO.Path.Combine(GlobalFunction.thumbPath,"shadow_big.png"), UriKind.RelativeOrAbsolute)));
                    Image img = new Image();
                    Rectangle rectange = new Rectangle();

                    gd.Width = imgWidth + (GlobalFunction.isSolutionUsing4K ? 8 : 5);
                    gd.Height = imgHeight + (GlobalFunction.isSolutionUsing4K ? 8 : 5);
                    //gd.Background = new SolidColorBrush(Colors.Red);
                    imagebrush.Stretch = Stretch.Fill;
                    gd.Background = imagebrush;

                    gd.VerticalAlignment = System.Windows.VerticalAlignment.Center;
                    gd.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                    img.VerticalAlignment = rectange.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                    img.HorizontalAlignment = rectange.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                    img.Width = rectange.Width = imgWidth;
                    img.Height = rectange.Height = imgHeight;
                    img.Stretch = Stretch.UniformToFill;
                    img.Source = src;
                    rectange.Fill = brush;
                    img.Margin = rectange.Margin = new Thickness(3, 2, 0, 0);
                    //gd.Margin = new Thickness(0, topBottomDiff, 0, 0);
                    gd.Children.Add(rectange);
                    gd.Children.Add(img);
                    gd.Margin = GlobalFunction.isSolutionUsing4K ? new Thickness(0, 0, 0, topBottomDiff) : new Thickness(0, 0, 0, 0);
                    uxStackPanel.Children.Add(gd);
                }
            }
        }
    }
}