using System;
using System.Threading;
using CRVM.Entity;

namespace CRVM.CIDExcuter
{
    public class AlarmRing
    {
        /// <summary>
        /// 播放波形文件 
        /// </summary>
        /// <param name="Filename">文件路径</param>
        /// <param name="Mod">一般情况下为0</param>
        /// <param name="Flags"></param>
        /// <returns></returns>
        [System.Runtime.InteropServices.DllImport("winmm.dll")]
        public static extern bool PlaySound(String Filename, int Mod, int Flags);
        protected const int SND_SYNC = 0x0; //同步播放，线程阻塞
        protected const int SND_ASYNC = 0x1;//异步播放
        protected const int SND_NODEFAULT = 0x2;
        protected const int SND_MEMORY = 0x4;
        protected const int SND_LOOP = 0x8;
        protected const int SND_NOSTOP = 0x10;
        protected const int SND_NOWAIT = 0x2000;
        protected const int SND_ALIAS = 0x10000;
        protected const int SND_ALIAS_ID = 0x110000;
        protected const int SND_FILENAME = 0x20000;
        protected const int SND_RESOURCE = 0x40004;
        protected const int SND_PURGE = 0x40;
        protected const int SND_APPLICATION = 0x80;




        private Thread vp;
        /// <summary>
        /// 开启报警铃声
        /// </summary>
        public void Ring(bool low, bool mid, bool high)
        {
            if (vp != null && vp.IsAlive)
            {
                vp.Abort();
            }
            if (mid)
            {
                PlaySound("mid.wav", 0, SND_FILENAME | SND_ASYNC);
                //PlaySound("low.wav", 0, SND_FILENAME | SND_ASYNC);
            }
            else if (high)
            {
                PlaySound("high.wav", 0, SND_FILENAME | SND_ASYNC);
                //PlaySound("low.wav", 0, SND_FILENAME | SND_ASYNC);
            }
            else if (low)
            {
                PlaySound("low.wav", 0, SND_FILENAME | SND_ASYNC);
            }

        }

        public void Ring()
        {
            if (SysParam.Instance.ringType == 1)
            {
                PlaySound("mid.wav", 0, SND_FILENAME | SND_ASYNC);
                //PlaySound("low.wav", 0, SND_FILENAME | SND_ASYNC);
            }
            else if (SysParam.Instance.ringType == 2)
            {
                PlaySound("high.wav", 0, SND_FILENAME | SND_ASYNC);
                //PlaySound("low.wav", 0, SND_FILENAME | SND_ASYNC);
            }
            else if (SysParam.Instance.ringType == 0)
            {
                PlaySound("low.wav", 0, SND_FILENAME | SND_ASYNC);
            }

        }
        /// <summary>
        /// 根据报警标记播放报警信息
        /// </summary>
        /// <param name="alarmFlag">报警标记[机架号，类型]</param>
        public void AlarmVoice(bool[,] alarmFlag)
        {
            if (vp != null && vp.IsAlive)
            {
                vp.Abort();
            }
            vp = new Thread(() => AlarmVoicePlay(alarmFlag));
            vp.Start();
        }

        internal void DefectVoice(bool[,] tempDefectFlag, bool[,] alarmFlag)
        {
            if (vp != null && vp.IsAlive)
            {
                vp.Abort();
            }
            vp = new Thread(() => DefectVoicePlay(tempDefectFlag, alarmFlag));
            vp.Start();
            //Console.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff") + "  线程号：" + Thread.CurrentThread.ManagedThreadId + "  线程池：" + Thread.CurrentThread.IsThreadPoolThread);
        }

        private void AlarmVoicePlay(bool[,] alarmFlag)
        {
            for (int i = 5; i >= 1; i--)
            {
                for (int j = 3; j >= 1; j--)
                {
                    if (alarmFlag[i - 1, j - 1])
                    {
                        PlaySound(SysParam.Instance.appPath + @"\ring\alarm" + i + "-" + j + ".wav", 0, SND_FILENAME | SND_ASYNC);
                        Thread.Sleep(2000);
                    }
                }
            }
        }

        private void DefectVoicePlay(bool[,] defectFlag, bool[,] alarmFlag)
        {
            //for (int i = 5; i >= 1; i--)
            //{
            //    for (int j = 3; j >= 1; j--)
            //    {
            //        if (defectFlag[i - 1, j - 1])
            //        {
            //            PlaySound(SysParam.Instance.appPath+@"\ring\defect" + i + "-" + j + ".wav", 0, SND_FILENAME | SND_ASYNC);
            //            Thread.Sleep(2000);
            //        }
            //    }
            //}
            AlarmVoicePlay(alarmFlag);
        }
    }
}
