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;

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 string MusicPath = null;
        static int WarningTipID = 0;

        WMPLib.WindowsMediaPlayer axWindowsMediaPlayer1 = null;
        System.Windows.Threading.DispatcherTimer timer;
        public TimeSpan Duration = TimeSpan.MinValue;
        string message = "警告";
        public string Message
        { 
            get 
            { 
                return message;
            } 
            set 
            { 
                message = value;
                NotifyPropertyChanged("Message"); 
            }
        }
        string ok_content = "确定";
        public string OKContent
        {
            get
            {
                return ok_content;
            }
            set
            {
                ok_content = value;
                NotifyPropertyChanged("OKContent");
            }
        }
        public Window_WarningTip()
        {
            InitializeComponent();
            this.DataContext = this;

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

            timer.Tick += new EventHandler(timer1_Tick);


            axWindowsMediaPlayer1 = new WMPLib.WindowsMediaPlayer();
            axWindowsMediaPlayer1.StatusChange += axWindowsMediaPlayer1_StatusChange;
        }
        //static Window_WarningTip() 
        //{
        //    Current = new Window_WarningTip();
        //}
        void timer1_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            
            axWindowsMediaPlayer1.StatusChange -= axWindowsMediaPlayer1_StatusChange;
            axWindowsMediaPlayer1.controls.stop();
            axWindowsMediaPlayer1.close();

            this.Close();
            Current = null;
            if (StopEvent != null)
                StopEvent();
        }
        /// <summary>
        /// 关闭 
        /// </summary>
        public static void Stop() 
        {
            if(Current!=null)
                Current.timer1_Tick(null,null);
        }
        /// <summary>
        /// 关闭 
        /// </summary>
        /// <param name="id"></param>
        public static void Stop(int id) 
        {
            if (WarningTipID == id)
                Stop();
        }

        void Play(string musicpath)
        {

            if (MusicPath != musicpath)
            {
                MusicPath = musicpath;
                axWindowsMediaPlayer1.URL = MusicPath;
                axWindowsMediaPlayer1.controls.play();
            }
            else
            {
                axWindowsMediaPlayer1_StatusChange();

            }

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

            }
            else 
            {
                timer.Interval = Duration;
                timer.Start();
            }
        }
        void axWindowsMediaPlayer1_StatusChange()
        {
            WMPLib.WMPPlayState s = axWindowsMediaPlayer1.playState;
            switch(s)
            {
                case WMPLib.WMPPlayState.wmppsUndefined://未知状态
                case WMPLib.WMPPlayState.wmppsStopped://播放停止
                case WMPLib.WMPPlayState.wmppsPaused://播放暂停
                case WMPLib.WMPPlayState.wmppsMediaEnded://播放流已结束
                    axWindowsMediaPlayer1.URL = MusicPath;
                    axWindowsMediaPlayer1.controls.play();
                    break;
                case WMPLib.WMPPlayState.wmppsPlaying://正在播放
                case WMPLib.WMPPlayState.wmppsScanForward://向前搜索
                case WMPLib.WMPPlayState.wmppsScanReverse://向后搜索
                case WMPLib.WMPPlayState.wmppsBuffering://正在缓冲
                case WMPLib.WMPPlayState.wmppsWaiting://正在等待流开始
                case WMPLib.WMPPlayState.wmppsTransitioning://准备新的媒体文件
                case WMPLib.WMPPlayState.wmppsReady://播放准备就绪
                case WMPLib.WMPPlayState.wmppsReconnecting://尝试重新连接流媒体数据
                case WMPLib.WMPPlayState.wmppsLast://上一次状态,状态没有改变
                    break;
            }
        }

        /// <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, "确定", 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, Action stophandler, string okcontent, Action okhandler) 
        {
            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();
            if (OKEvent != null)
                OKEvent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Width = SystemParameters.PrimaryScreenWidth;
            this.Left = 0;
            this.Top = (SystemParameters.PrimaryScreenHeight - this.Height) / 2;
        }
        #region INotifyPropertyChanged 成员
        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion



    }
}
