using System;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace XHWK.WKTool
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;

    /// <summary>
    /// 录屏等待 CountdownWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CountdownWindow
    {
        private readonly List<BitmapImage> _images = new List<BitmapImage>();

        public CountdownWindow()
        {
            InitializeComponent();
            foreach (var imgName in _imgNames)
            {
                _images.Add(new BitmapImage(new Uri("pack://application:,,/Images/" + imgName)));
            }
        }

        /// <summary>
        /// 计时器
        /// </summary>
        private int _imgNum = 22;

        public void Initialize(bool showlblShortcut = false, int changeTime = 3000)
        {
            imgLoding.Opacity = 0.1;
            imgLoding.Source = _images.Last();
            Init(showlblShortcut, changeTime);
        }

        private System.Timers.Timer _timer;

        private async void Init(bool showlblShortcut, int changeTime)
        {
            if (showlblShortcut)
            {
                lblShortcut.Visibility = Visibility.Visible;
                borShortcut.Visibility = Visibility.Visible;
            }
            else
            {
                lblShortcut.Visibility = Visibility.Hidden;
                borShortcut.Visibility = Visibility.Hidden;
            }
            _imgNum = 22;
            _timer = new System.Timers.Timer();
            _timer.Enabled = true;
            _timer.Interval = 1.0 * changeTime / 22;
            _timer.Elapsed += Timer_Elapsed;
            _timer.Start();
            await Task.Delay(300);
            imgLoding.Opacity = 1;
        }

        private void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (_imgNum >= 0)
            {
                Dispatcher.Invoke
                (
                    () =>
                    {
                        // 执行操作
                        imgLoding.Source = _images[_imgNum];
                    },
                    DispatcherPriority.Send
                );
                _imgNum--;
            }
            else
            {
                _timer.Stop();
                Dispatcher.Invoke
                (
                    () =>
                    {
                        // 执行操作
                        imgLoding.Source = _images.Last();
                        Hide();
                    },
                    DispatcherPriority.Send
                );
            }
        }

        readonly string[] _imgNames =
        {
            "countdown1_7.png",
            "countdown1_6.png",
            "countdown1_5.png",
            "countdown1_4.png",
            "countdown1_3.png",
            "countdown1_2.png",
            "countdown1_1.png",
            "countdown2_7.png",
            "countdown2_6.png",
            "countdown2_5.png",
            "countdown2_4.png",
            "countdown2_3.png",
            "countdown2_2.png",
            "countdown2_1.png",
            "countdown3_8.png",
            "countdown3_7.png",
            "countdown3_6.png",
            "countdown3_5.png",
            "countdown3_4.png",
            "countdown3_3.png",
            "countdown3_2.png",
            "countdown3_1.png",
            "countdown3_1.png",
        };
    }
}
