using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Shapes;
using System.ComponentModel;
using NAudio.Wave;

namespace FLY.ControlLibrary
{
    /// <summary>
    /// Window_WarningTip.xaml 的交互逻辑
    /// </summary>
    public partial class Window_WarningTip : Window, INotifyPropertyChanged
    {
        static Window_WarningTip Current = null;
        static  Action StopEvent = null;
        static Action OKEvent = null;
        static int WarningTipID = 0;

        private WaveOutEvent outputDevice;
        private AudioFileReader audioFile;
        private bool isRunning;

        System.Windows.Threading.DispatcherTimer timer;
        /// <summary>
        /// 持续时间
        /// </summary>
        public TimeSpan Duration = TimeSpan.MinValue;

        /// <summary>
        /// 警告信息
        /// </summary>
        public string Message { get; set; } = "警告";

        /// <summary>
        /// 确认键 显示内容
        /// </summary>
        public string OKContent { get; set; } = "确定";

        
        public Window_WarningTip()
        {
            InitializeComponent();
            this.DataContext = this;

            timer = new System.Windows.Threading.DispatcherTimer();

            timer.Tick += new EventHandler(timer1_Tick);
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            timer.Stop();

            //到时间了，把音乐关闭
            stopMusic();

            //把界面关闭
            Close();
            Current = null;

            //触发事件
            StopEvent?.Invoke();
        }
        /// <summary>
        /// 关闭 
        /// </summary>
        public static void Stop() 
        {
            Current?.timer1_Tick(null,null);
        }
        /// <summary>
        /// 关闭 
        /// </summary>
        /// <param name="id"></param>
        public static void Stop(int id) 
        {
            if (WarningTipID == id)
                Stop();
        }

        void stopMusic() 
        {
            isRunning = false;
            outputDevice?.Stop();
            outputDevice?.Dispose();
            outputDevice = null;
            audioFile?.Dispose();
            audioFile = null;
        }
        void playMusic(string musicpath)
        {
            try
            {
                audioFile = new AudioFileReader(musicpath);
            }
            catch 
            {
                //音乐无法启动
                return;
            }
            outputDevice = new WaveOutEvent();


            outputDevice.Init(audioFile);
            outputDevice.PlaybackStopped += (s, e) =>
            {
                if (isRunning)
                {
                    //重新播放！！
                    if (audioFile == null)
                    {
                        stopMusic();
                        return;
                    }
                    audioFile.Position = 0;
                    outputDevice.Play();
                }
            };
            outputDevice.Play();
        }
        void Play(string musicpath)
        {
            if (musicpath == null)
            {
                //没有音乐，关闭
                stopMusic();
            }
            else if (audioFile == null)
            {
                playMusic(musicpath);
            }
            else if (audioFile.FileName != musicpath)
            {
                stopMusic();
                playMusic(musicpath);
            }
            isRunning = true;

            if ((Duration == TimeSpan.MaxValue) || (Duration == TimeSpan.MinValue))
            {
                //永远打开
                timer.Stop();

            }
            else 
            {
                timer.Interval = Duration;
                timer.Start();
            }
        }

        /// <summary>
        /// 显示报警提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        /// <returns>ID</returns>
        public static int Show(string title, string message) 
        {
            return Show(title, message, TimeSpan.MaxValue);
        }
        /// <summary>
        /// 显示报警提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        /// <param name="duration">显示的时间</param>
        /// <returns></returns>
        public static int Show(string title, string message, TimeSpan duration)
        {
            return Show(title, message, duration, null);
        }
        /// <summary>
        /// 显示报警提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        /// <param name="duration">显示的时间</param>
        /// <param name="musicpath">音乐路径</param>
        /// <returns>ID</returns>
        public static int Show(string title, string message, TimeSpan duration, string musicpath)
        {

            return Show(title, message, duration, musicpath, null);
        }
        /// <summary>
        /// 显示报警提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        /// <param name="duration">显示的时间</param>
        /// <param name="musicpath">音乐路径</param>
        /// <param name="stophandler">关闭事件 回调函数</param>
        /// <returns>ID</returns>
        public static int Show(string title, string message, TimeSpan duration, string musicpath, Action stophandler)
        {
            return Show(title, message, duration, musicpath, stophandler, "OK", null);
        }
        /// <summary>
        /// 显示报警提示
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="message">内容</param>
        /// <param name="duration">显示的时间</param>
        /// <param name="musicpath">音乐路径</param>
        /// <param name="stophandler">关闭事件 回调函数</param>
        /// <param name="okcontent">确定按键 显示字符串</param>
        /// <param name="okhandler">确定事件 回调函数</param>
        /// <returns>ID</returns>
        public static int Show(string title, string message, TimeSpan duration, string musicpath=null, Action stophandler=null, string okcontent="OK", Action okhandler=null) 
        {
            if (Current == null)
            {
                Current = new Window_WarningTip();
            }
            
            
            Current.Title = title;
            Current.Duration = duration;
            Current.Message = message;
            Current.OKContent = okcontent;

            StopEvent = stophandler;
            OKEvent = okhandler;
            Current.Owner = Application.Current.MainWindow;
            Current.Show();
            Current.Play(musicpath);

            WarningTipID++;
            if (WarningTipID >= int.MaxValue-1)
                WarningTipID = 0;

            return WarningTipID;
        }
        private void button_ok_Click(object sender, RoutedEventArgs e)
        {
            Stop();
            OKEvent?.Invoke();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Width = SystemParameters.PrimaryScreenWidth;
            this.Left = 0;
            this.Top = (SystemParameters.PrimaryScreenHeight - this.Height) / 2;
        }
        #region INotifyPropertyChanged 成员
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion



    }
}
