using CPF;
using CPF.Controls;
using CPF.Drawing;
using ESBasic;
using OMCS;
using OMCS.Cpf;
using OMCS.Passive;
using OMCS.Tools;
using OVCS.ClientLinux.Tools;
using System;
using System.Configuration;

namespace OVCS.ClientLinux
{
    public class LoginWindow : Window
    {
        private ElTextBox roomNumber;
        private ElTextBox loginId;
        private ElTextBox loginPassword; 
        //多媒体设备管理器
        private IMultimediaManager multimediaManager;
        private CustomizeHandler customizeHandler;

        /// <summary>
        /// 登录成功时，触发此事件。参数：房间号RoomId
        /// </summary>
        public event CbGeneric<string> loginSuccess;
        protected override void InitializeComponent()
        {
            LoadStyleFile("res://OVCS.ClientLinux/Stylesheet1.css");//加载样式文件，文件需要设置为内嵌资源
            
            Title = "OVCS视频会议系统2023";
            MinHeight = 240;
            MinWidth = 350;
            MaxHeight = 240;
            MaxWidth = 350;
            Background = null;
            Children.Add(new WindowFrame(this, new Panel
            {
                Size = SizeField.Fill,
                Children =
                {
                     Children.Add(new Grid
                    {
                        Size = SizeField.Fill,
                        ClipToBounds = true,
                        MarginTop = 30,
                        RowDefinitions =
                        {
                            new RowDefinition
                            {
                                Height = GridLength.Auto
                            },
                            new RowDefinition
                            {
                                Height = GridLength.Auto
                            },
                            new RowDefinition
                            {
                                Height = GridLength.Auto,
                            },
                            new RowDefinition
                            {
                                Height = GridLength.Star,
                            },
                        },
                        Children =
                        {
                            new StackPanel()
                            {
                                Orientation = Orientation.Horizontal,
                                PresenterFor=this,
                                Children =
                                {
                                    new Label()
                                    {
                                        PresenterFor=this,
                                        Text="房间号："  ,
                                        Width = 75,
                                        FontSize = 15,

                                    },
                                     
                                     new ElTextBox
                                     {
                                        Height = 25,
                                        FontSize = 15,
                                        Name = "roomNumber",
                                        PresenterFor=this,
                                        Text = "R001",
                                        Width = 120,
                                        AcceptsReturn = false,
                                        Background = Color.White,
                                        BorderFill = Color.LightGray,
                                        BorderThickness = "1",
                                        BorderType = BorderType.BorderThickness,
                                     }
                                },
                                Attacheds=
                                {
                                    {
                                       Grid.RowIndex,
                                       0
                                    }
                                }
                            },
                            new StackPanel()
                            {
                                MarginTop = 10,
                                Orientation = Orientation.Horizontal,
                                Children =
                                {
                                    new Label()
                                    {
                                        PresenterFor=this,
                                        Text = "登录账号：",
                                        Width = 75,
                                        FontSize = 15,

                                    },
                                    new ElTextBox
                                    {
                                        Height = 25,
                                        PresenterFor = this,
                                        Name ="loginId",
                                        Text = "aa03",
                                        Width = 120,
                                        FontSize = 15,
                                        AcceptsReturn = false,
                                        Background = Color.White,
                                        BorderFill = Color.LightGray,
                                        BorderThickness = "1",
                                        BorderType = BorderType.BorderThickness,
                                    }
                                },
                                Attacheds=
                                {
                                    {
                                        Grid.RowIndex,
                                        1
                                    }
                                }
                            },
                            new StackPanel()
                            {
                                MarginTop = 10,
                                Orientation = Orientation.Horizontal,
                                Children =
                                {

                                    new Label()
                                    {
                                        PresenterFor = this,
                                        Text ="登录密码：",
                                        Width = 75,
                                        FontSize = 15,
                                    },
                                    new ElTextBox
                                    {
                                        PresenterFor=this,
                                        Name = "loginPassword",
                                        Width = 120,
                                        FontSize = 15,
                                        PasswordChar= '*',
                                        AcceptsReturn = false,
                                        Background = Color.White,
                                        BorderFill = Color.LightGray,
                                        BorderThickness = "1",
                                        BorderType = BorderType.BorderThickness,
                                    }
                                },
                                Attacheds=
                                {
                                    {
                                       Grid.RowIndex,
                                       2
                                    }
                                }
                            },
                            new Button()
                            {
                                Content = "确定",
                                Width = 65,
                                Height = 30,
                                MarginRight = 70,
                                MarginBottom = 60,
                                Commands =
                                {
                                    {
                                        nameof(Button.MouseDown),

                                        (s,e) =>
                                        {
                                            this.Login_BtnClick();
                                        }
                                    }
                                },
                                Attacheds=
                                {
                                    {
                                       Grid.RowIndex,
                                       3
                                    }
                                }
                            }
                        }
                    })
                }
            })) ;

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



//#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错

        
        private void SetComponent()
        {
            roomNumber = this.FindPresenterByName<ElTextBox>("roomNumber");
            loginId = this.FindPresenterByName<ElTextBox>("loginId");
            loginPassword = this.FindPresenterByName<ElTextBox>("loginPassword");
        }
        public LoginWindow(CustomizeHandler _customizeHandler, IMultimediaManager _multimediaManager)
        {
            GlobalUtil.SetPaintHelper(CpfPaintHelper.Singleton);
            this.customizeHandler = _customizeHandler; 
            this.multimediaManager = _multimediaManager;

            this.Closing += LoginWindow_Closing;
        }

        private void LoginWindow_Closing(object sender, ClosingEventArgs e)
        {
            this.multimediaManager.Dispose(); 
        }
        #region 登录参数设置
        private void Login()
        {
            // OMCS初始化
            #region OMCS参数设置          
            //OMCS.GlobalUtil.SetMaxLengthOfUserID(20);
            IMultimediaManager multimediaManager1 = Program.MultimediaManager;
            multimediaManager.CameraVideoSize = new System.Drawing.Size(640, 480);
            multimediaManager.CameraDeviceIndex = 0;
            multimediaManager.MicrophoneDeviceIndex = SoundDevice.GetFirstAvailableMicrophoneIndex();
            multimediaManager.SpeakerIndex = SoundDevice.GetFirstAvailableSpeakerIndex();
            multimediaManager.MaxDesktopFrameRate = 15;
            multimediaManager.DesktopEncodeQuality = 18;
            multimediaManager.MaxCameraFrameRate = 15;
            multimediaManager.CameraEncodeQuality = 18;
            multimediaManager.Advanced.AudioMixedStrategy = AudioMixedStrategy.DecibelTop2;
            multimediaManager.Advanced.AutoAdjustCameraOutputSize = true;
            #endregion
            multimediaManager.Initialize(this.loginId.Text, this.loginPassword.Text, ConfigurationManager.AppSettings["ServerIP"], int.Parse(ConfigurationManager.AppSettings["OmcsPort"]));
            Program.TestLogger.LogWithTime("multimediaManager初始化成功");
            //ESFramework 初始化 
            this.Hide();
            this.loginSuccess?.Invoke(this.roomNumber.Text);
        }
        #endregion
        protected override void OnInitialized()
        {
            base.OnInitialized();
        }


        //用户代码
        private  void Login_BtnClick()
        {
            try
            {
                if (this.roomNumber.Text.Trim() == "")
                {
                    return;
                }
                if (this.loginId.Text.Trim().Length > 21)
                {
                    MessageBox.Show("登录账号的长度不能超过21");
                    return;
                }
                this.Login();
            }
            catch (System.Exception ee)
            {
                MessageBox.Show(ee.Message + " - " + ee.StackTrace);
                Console.WriteLine(ee.Message + " - " + ee.StackTrace);
            }
        }

//#endif
    }
}
