using CPF;
using CPF.Controls;
using ESBasic;
using OMCS;
using OMCS.Linux;
using OMCS.Passive;
using OMCS.Tools;
using System.Collections.Generic;

namespace OVCS.ClientLinux
{
    public class SettingWindow : Window
    {
        private IMultimediaManager multimediaManager = MultimediaManagerFactory.GetSingleton();
        private ComboBox ComboBox_camera;
        private ComboBox ComboBox_mic;
        private ComboBox ComboBox_speaker;
        private int defaultCameraIndex; 
        protected override void InitializeComponent()
        {
            LoadStyleFile("res://OVCS.ClientLinux/Stylesheet1.css");//加载样式文件，文件需要设置为内嵌资源
            
            Title = "设备选择";
            MinHeight = 220;
            MinWidth = 430;
            MaxHeight = 220;
            MaxWidth = 430;
            Height = 220;
            Width = 430;
            Background = null;
            Children.Add(new WindowFrame(this, new Panel
            {
                Size = SizeField.Fill,
                Children =
                {
                     Children.Add(new Grid
                    {
                        Size = SizeField.Fill,
                        ClipToBounds = true,
                        MarginTop = 10,
                        Children =
                        {
                             new StackPanel
                             {
                                 Size= SizeField.Fill,
                                 Orientation = Orientation.Vertical,
                                 MarginTop = 10,
                                 MarginBottom= 10,
                                 MarginLeft = 10,
                                 MarginRight= 10,
                                 Children=
                                 {
                                     new StackPanel
                                     {
                                         Size= SizeField.Fill,
                                         Orientation = Orientation.Horizontal,
                                         MarginTop = 5,
                                         MarginBottom= 5,
                                         MarginLeft= 10,
                                         MarginRight= 10,
                                         Children=
                                         {
                                             new TextBlock
                                             {
                                                 Text = "摄像头",
                                                 MarginLeft= 10,
                                                 MarginRight= 10,
                                             },
                                             new ComboBox
                                             {
                                                 Name="ComboBox_camera",
                                                 PresenterFor=this,
                                                 SelectedValuePath="Index", 
                                                 Width = 320,
                                                 Height = 23,
                                                 //BorderThickness = "1",
                                                 //BorderType = BorderType.BorderThickness,
                                                 //Items =
                                                 //{
                                                 //    "0",
                                                 //    "1",
                                                 //    "2"
                                                 //}
                                             }
                                         }
                                     },
                                     new StackPanel
                                     {
                                         
                                         Size= SizeField.Fill,
                                         Orientation = Orientation.Horizontal,
                                         MarginTop = 5,
                                         MarginBottom= 5,
                                         MarginLeft= 10,
                                         MarginRight= 10,
                                         Children=
                                         {
                                             new TextBlock
                                             {
                                                 Text = "麦克风",
                                                 
                                                 MarginLeft= 10,
                                                 MarginRight= 10,
                                             },
                                             new ComboBox
                                             {
                                                 Name="ComboBox_mic",
                                                 PresenterFor=this,
                                                 SelectedValuePath="Index",
                                                 Width = 320,
                                                 Height = 23,
                                                 //BorderThickness = "1",
                                                 //BorderType = BorderType.BorderThickness,
                                                 //Items =
                                                 //{
                                                 //    "0",
                                                 //    "1",
                                                 //    "2"
                                                 //}
                                             }
                                         }
                                     },
                                     new StackPanel
                                     {
                                         Size= SizeField.Fill,
                                         Orientation = Orientation.Horizontal,
                                         MarginTop = 5,
                                         MarginBottom= 5,
                                         MarginLeft= 10,
                                         MarginRight= 10,
                                         Children=
                                         {
                                             new TextBlock
                                             {
                                                 Text = "扬声器",
                                                 MarginLeft = 10,
                                                 MarginRight= 10,
                                             },
                                             new ComboBox
                                             {
                                                 Name="ComboBox_speaker",
                                                 PresenterFor=this,
                                                 SelectedValuePath = "Index",
                                                 Width = 320,
                                                 Height = 23,
                                                 //BorderThickness = "1",
                                                 //BorderType = BorderType.BorderThickness,
                                                 //Items =
                                                 //{
                                                 //    "0",
                                                 //    "1",
                                                 //    "2"
                                                 //}
                                             }
                                         }
                                     },
                                 }
                             },
                             new Button
                             {
                                  Content="确定",
                                  Commands =
                                  {
                                      {
                                          nameof(Button.MouseDown),

                                          (s,e) =>
                                          {
                                              this.button_setClick();
                                          }
                                      }
                                  },
                                  
                                  Height = 30,
                                  Width = 60,
                                  MarginTop = 120,
                                  MarginRight= 20,
                             }
                        }
                    })
                }
            })) ;

            if (!DesignMode)//设计模式下不执行，也可以用#if !DesignMode
            {
                SetComponent();
            }
        }



        private void SetComponent()
        {
            ComboBox_camera = this.FindPresenterByName<ComboBox>("ComboBox_camera");
            ComboBox_mic = this.FindPresenterByName<ComboBox>("ComboBox_mic");
            ComboBox_speaker = this.FindPresenterByName<ComboBox>("ComboBox_speaker");

            this.defaultCameraIndex = this.multimediaManager.CameraDeviceIndex;
            cameras = Camera.GetCameras();

            this.ComboBox_camera.Items = (System.Collections.IList)cameras;
            if (cameras.Count > 0)
            {
                foreach (CameraInformation cameraInformation in cameras)
                {
                    if (cameraInformation.Index == this.defaultCameraIndex)
                    {
                        this.ComboBox_camera.SelectedIndex = cameras.IndexOf(cameraInformation);
                    }
                }
            }
            //获取麦克风列表
            microphones = SoundDevice.GetMicrophones();
            this.ComboBox_mic.Items = (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.Items = (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);
                    }
                }
            }
        }
//#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错


        public SettingWindow()
        {
            //获取摄像头列表
           
            this.Closing += LoginWindow_Closing;
        }

        private void LoginWindow_Closing(object sender, ClosingEventArgs e)
        {
            
        }
        private IList<CameraInformation> cameras;
        private IList<MicrophoneInformation> microphones;
        private IList<SpeakerInformation> speakers;
        protected override void OnInitialized()
        {
            base.OnInitialized();
            
        }


        //用户代码 0 2 4
        private void button_setClick()
        {
            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)
            {
                Program.MainWindow.SendCameraIndexChange();
            }
            this.Close();
        }
//#endif
    }
}
