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

namespace OVCS.ClientWinform
{
    public partial class SettingForm : Form
    {
        private IMultimediaManager multimediaManager = MultimediaManagerFactory.GetSingleton();
        private IList<CameraInformation> cameras;
        private IList<MicrophoneInformation> microphones;
        private IList<SpeakerInformation> speakers;
        private int defaultCameraIndex; 
        public SettingForm()
        {
            InitializeComponent();
            this.defaultCameraIndex = this.multimediaManager.CameraDeviceIndex;
            //获取摄像头列表
            cameras = Camera.GetCameras();
            this.comboBox1.DataSource = (System.Collections.IList)cameras;
            if (cameras.Count > 0)
            {
                foreach (CameraInformation cameraInformation in cameras)
                {
                    if (cameraInformation.Index == this.defaultCameraIndex)
                    {
                        this.comboBox1.SelectedIndex = cameras.IndexOf(cameraInformation);
                    }
                }
            }
            //获取麦克风列表
            microphones = SoundDevice.GetMicrophones();
            this.comboBox2.DataSource = (System.Collections.IList)microphones;
            if (microphones.Count > 0)
            {
                foreach (MicrophoneInformation microphoneInformation in microphones)
                {
                    if (microphoneInformation.Index == this.multimediaManager.MicrophoneDeviceIndex)
                    {
                        this.comboBox2.SelectedIndex = microphones.IndexOf(microphoneInformation);
                    }
                }
            }

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

            this.multimediaManager.DeviceErrorOccurred += new CbGeneric<MultimediaDeviceType,int, string>(multimediaManager_DeviceErrorOccurred);
            this.FormClosed += SettingForm_FormClosed;
        }

        private void SettingForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.comboBox1.Enabled = true;
            this.comboBox2.Enabled = true;
            this.comboBox3.Enabled = true;
        }

        void multimediaManager_DeviceErrorOccurred(MultimediaDeviceType deviceType,int deviceIndex, string error)
        {
            if (this.InvokeRequired)
            {
                this.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_start_Click(object sender, EventArgs e)
        {
            try
            {             
                int camera = this.comboBox1.SelectedIndex;
                int microphone = this.comboBox2.SelectedIndex;
                int speaker = this.comboBox3.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.button_start.Enabled = false;  
                this.comboBox1.Enabled = false;
                this.comboBox2.Enabled = false;
                this.comboBox3.Enabled = false; 
                if (this.defaultCameraIndex != this.multimediaManager.CameraDeviceIndex)
                {
                    Program.MainForm.SendCameraIndexChange(); 
                }
                this.Close();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
    }
}
