using CoreAudio;
using ryCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace RyLine
{
    class RyCode
    {
        /// <summary>
        /// 运行代码
        /// </summary>
        /// <param name="line"></param>
        public static void RunCodeLine(string line)
        {
            #region 单参数识别
            string line_low = line.ToLower();
            string title = "";
            switch (line_low)
            {
                case "shutdown":
                    title = "关机";
                    break;
                case "reboot":
                    title = "重启电脑";
                    break;
                case "logoff":
                    title = "注销电脑";
                    break;
            }
            if (title != "")
            {
                RyLine.FrmConfirm frm = new FrmConfirm();
                frm.LblAction.Text = title;
                if (frm.ShowDialog() != DialogResult.OK) { return; }
            }
            switch (line_low)
            {
                case "shutdown":
                    shoutdown.DoExitWin(shoutdown.EWX_SHUTDOWN | shoutdown.EWX_FORCE);
                    break;
                case "reboot":
                    shoutdown.DoExitWin(shoutdown.EWX_REBOOT | shoutdown.EWX_FORCE);
                    break;
                case "logoff":
                    shoutdown.DoExitWin(shoutdown.EWX_LOGOFF | shoutdown.EWX_FORCE);
                    break;
                case "mute_on"://静音
                    try
                    {
                        MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
                        var device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
                        device.AudioEndpointVolume.Mute = true;
                    }
                    catch {
                    }
                    break;
                case "mute_off"://取消静音
                    try
                    {
                        MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
                        var device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
                        device.AudioEndpointVolume.Mute = false;
                    }
                    catch { }
                    break;
                case "mute_onoff"://静音/取消静音
                    try
                    {
                        MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
                        var device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
                        device.AudioEndpointVolume.Mute = !device.AudioEndpointVolume.Mute;
                    }
                    catch { }
                    break;
                case "timesync"://时间同步
                    TimeClock.API.TimeSync.Sync();
                    break;
                default:
                    if (line_low.IndexOf("openfile") == 0) //打开文件
                    {
                        #region 打开文件
                        string file = line.GetStr("\"", "\"", 0, out int pos, "");
                        string pram = "";
                        if (file != "")
                        { pram = line.GetStr(" \"", "\"", pos, out pos, ""); }
                        if (file != "")
                        {
                            RyFiles.RunFile(file, pram);
                        }
                        #endregion
                    }
                    else if (line_low.IndexOf("sleep(") == 0 || line_low.IndexOf("wait(") == 0) //等待几毫秒
                    {
                        int mill_second= line.GetStr("(", ")", 0, out int pos, "0").ToInt();
                        Thread.Sleep(mill_second);
                    }
                    else if (line_low.IndexOf("vol=")==0) //设置音量
                    {
                        #region 设置音量
                        double vol = line.GetStr("=", "", 0, out int pos, "0").ToDouble();
                        try
                        {
                            MMDeviceEnumerator DevEnum = new MMDeviceEnumerator();
                            var device = DevEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia);
                            device.AudioEndpointVolume.MasterVolumeLevelScalar = (float)vol;
                            if(device.AudioEndpointVolume.Mute)
                            {
                                device.AudioEndpointVolume.Mute = false;
                            }
                        }
                        catch { } 
                        #endregion
                    }
                    break;
            }
            #endregion
        }
    }
}
