using AduSkin.Controls.Metro;
using System;
using System.Collections.Generic;
using System.Diagnostics;
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;

namespace JianGongYun.TRTC.Windows
{
    /// <summary>
    /// SettingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SettingWindow : MetroWindow
    {
        /// <summary>
        /// 单例
        /// </summary>
        private static SettingWindow _Instance;
        public static SettingWindow GetInstance()
        {
            if (_Instance == null)
            {
                _Instance = new SettingWindow();
            }
            return _Instance;
        }

        protected override void OnClosed(EventArgs e)
        {
            _Instance = null;
            if (micTesting)//关闭麦克风测试
            {
                LiveClassroom.lTXDeviceManager.stopMicDeviceTest();
            }
            base.OnClosed(e);
        }
        ViewModels.SettingWindowViewModel mode = ViewModels.SettingWindowViewModel.GetInstance();
        private SettingWindow()
        {
            InitializeComponent();
            BorderBrush = new SolidColorBrush(color: Color.FromRgb(42, 43, 48));//窗口标题背景颜色
            this.DataContext = mode;
        }


        /// <summary>
        /// 测试麦克风
        /// </summary>
        bool micTesting = false;
        private void TestMic(object sender, RoutedEventArgs e)
        {
            if (micTesting)
            {
                LiveClassroom.lTXDeviceManager.stopMicDeviceTest();
                this.Dispatcher.Invoke(new Action(() =>
                {
                    this.MicTestBtn.Content = "检测麦克风";
                    this.MicTestBtn.Type = AduSkin.Controls.FlatButtonSkinEnum.info;
                    micTesting = false;
                }));
            }
            else
            {
                if (LiveClassroom.lTXDeviceManager.startMicDeviceTest(1000) == 0)
                {
                    this.Dispatcher.Invoke(new Action(() =>
                    {
                        this.MicTestBtn.Content = "停止检测";
                        this.MicTestBtn.Type = AduSkin.Controls.FlatButtonSkinEnum.error;
                        micTesting = true;
                    }));
                }
                else
                {
                    AduMessageBox.Show("测试失败", "提醒");
                }
            }
        }

        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            //滑动条颜色前台设置无效，手动调用才行
            var sliderColor = (SolidColorBrush)(new BrushConverter().ConvertFrom("#3e7fff"));
            Slider1.DecreaseColor = sliderColor;
            Slider2.DecreaseColor = sliderColor;
        }

        /// <summary>
        /// 打开文件浏览器
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OpenDir_Click(object sender, RoutedEventArgs e)
        {
            var process = new Process
            {
                StartInfo = new ProcessStartInfo { FileName = "explorer.exe", Arguments = mode.ScreenRecordingDir }
            };
            process.Start();
        }

        /// <summary>
        /// 修改路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChangeDir_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog openFileDialog = new System.Windows.Forms.FolderBrowserDialog();  //选择文件夹
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)//注意，此处一定要手动引入System.Window.Forms空间，否则你如果使用默认的DialogResult会发现没有OK属性
            {
                mode.ScreenRecordingDir = openFileDialog.SelectedPath;
            }
        }
    }
}
