using Common;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MeterPlugInLibrary
{
    public class VoicePlay
    {
        private string m_szRunPath = System.Environment.CurrentDirectory;

        private VoiceMCI vmciClass = new VoiceMCI();
        /// <summary>
        /// 语音播放
        /// </summary>
        /// <param name="strvoiceName"></param>
        /// <param name="strPntName">车牌号</param>
        public void GetVoicePlay(string strvoiceName, string strCarNo)
        {
            try
            {
                WriteLog("车牌号：" + strCarNo + " 当前准备播放：" + strvoiceName);
                //判断是否存在语音，存在则语音播报
                bool isHaveVoice = false;
                foreach (string str in PbCache.voiceInfo)
                {
                    if (strvoiceName == str)
                    {
                        isHaveVoice = true;
                        break;
                    }
                }

                if (!isHaveVoice) return; //不存在语音则不进行后面播放操作

                if (!PbCache.voiceOver) return;//语音未播完
                PbCache.voiceOver = false;

                WriteLog("车牌号：" + strCarNo + " 语音播放：" + strvoiceName);
                strvoiceName = strvoiceName + ".wav";
                string path = m_szRunPath + "\\Sound\\" + strvoiceName;
                int intVmci = vmciClass.VoicePlay(path);
                WriteLog("车牌号：" + strCarNo + " 语音播放结果（ -1语音播放失败 0无音频设备 1语音播放成功 2语音播放结束）：" + intVmci);
            }
            catch (Exception exp)
            {
                WriteLog("语音播放异常！" + strvoiceName + "  " + exp.Message);
            }
        }
        
        private void WriteLog(string str)
        {
            string m_szRunPath;
            m_szRunPath = System.Environment.CurrentDirectory;
            if (System.IO.Directory.Exists(m_szRunPath + "\\log") == false)
            {
                System.IO.Directory.CreateDirectory(m_szRunPath + "\\log");
            }
            string strDate = System.DateTime.Now.ToString("yyyyMMdd");
            string strPathFile = m_szRunPath + "\\log\\" + strDate;
            if (!Directory.Exists(strPathFile))//如果不存在就创建file文件夹
            {
                Directory.CreateDirectory(strPathFile);
            }
            System.IO.TextWriter tw = new System.IO.StreamWriter(strPathFile + "\\语音播放_" + strDate + ".log", true);

            tw.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            tw.WriteLine(str);
            tw.WriteLine("\r\n");

            tw.Close();
        }
    }
}
