using ESBasic;
using OMCS;
using OMCS.Passive;
using OMCS.Windows;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace OVCS.ClientWpf
{
    /// <summary>
    /// SettingWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SettingWindow : Window
    {
        private int defaultCameraIndex;
        private IList<CameraInformation> cameras;
        private IList<MicrophoneInformation> microphones;
        private IList<SpeakerInformation> speakers;
        private IMultimediaManager multimediaManager; 
        public SettingWindow(IMultimediaManager _multimediaManager)
        {
            InitializeComponent();
            this.multimediaManager = _multimediaManager;

            this.defaultCameraIndex = this.multimediaManager.CameraDeviceIndex;
            //获取摄像头列表
            cameras = Camera.GetCameras();
            this.ComboBox_camera.ItemsSource = (System.Collections.IList)cameras;
            if (cameras.Count > 0)
            {
                foreach (CameraInformation cameraInformation in cameras)
                {
                    if (cameraInformation.Index == this.multimediaManager.CameraDeviceIndex)
                    {
                        this.ComboBox_camera.SelectedIndex = cameras.IndexOf(cameraInformation);
                    }
                }
            }
            //获取麦克风列表
            microphones = SoundDevice.GetMicrophones();
            this.ComboBox_mic.ItemsSource = (System.Collections.IList)microphones;
            if (microphones.Count > 0)
            {
                foreach (MicrophoneInformation microphoneInformation in microphones)
                {
                    if (microphoneInformation.Index == this.multimediaManager.MicrophoneDeviceIndex)
                    {
                        this.ComboBox_mic.SelectedIndex = microphones.IndexOf(microphoneInformation);
                    }
                }
            }

            //获取扬声器列表
            speakers = SoundDevice.GetSpeakers();
            this.ComboBox_speaker.ItemsSource = (System.Collections.IList)speakers;
            if (speakers.Count > 0)
            {
                foreach (SpeakerInformation speakerInformation in speakers)
                {
                    if (speakerInformation.Index == this.multimediaManager.SpeakerIndex)
                    {
                        this.ComboBox_speaker.SelectedIndex = speakers.IndexOf(speakerInformation);
                    }
                }
            }
            this.Closed += SettingWindow_Closed;
        }

        private void SettingWindow_Closed(object sender, EventArgs e)
        {
            this.button_set_Click(null, null);
        }

        void multimediaManager_DeviceErrorOccurred(MultimediaDeviceType deviceType, int deviceIndex, string error)
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.BeginInvoke(new CbGeneric<MultimediaDeviceType, int, string>(this.multimediaManager_DeviceErrorOccurred), deviceType, deviceIndex, error);
            }
            else
            {
                MessageBox.Show(string.Format("Error : {0} - {1}", deviceType, error));
            }
        } 

        private void button_set_Click(object sender, RoutedEventArgs e)
        {
            int camera = this.ComboBox_camera.SelectedIndex;
            int microphone = this.ComboBox_mic.SelectedIndex;
            int speaker = this.ComboBox_speaker.SelectedIndex;
            this.multimediaManager.CameraDeviceIndex = camera == -1 ? -1 : cameras[camera].Index ;
            this.multimediaManager.MicrophoneDeviceIndex = microphone == -1 ? -1 :microphones[microphone].Index;
            this.multimediaManager.SpeakerIndex = speaker == -1 ? -1 : speakers[speaker].Index;


            this.ComboBox_camera.IsEnabled = false;
            this.ComboBox_mic.IsEnabled = false;
            this.ComboBox_speaker.IsEnabled = false;

            if (this.defaultCameraIndex != this.multimediaManager.CameraDeviceIndex)
            {
                App.MainWindow.SendCameraIndexChange();
            }
            this.Close();
        }
    }
}
