using ESBasic;
using OMCS.Passive;
using System;
using System.Configuration;
using System.Windows;

namespace OVCS.ClientWpf
{
    /// <summary>
    /// 登录页面渲染
    /// </summary>
    public partial class LoginWindow : Window
    { 
        //多媒体设备管理器
        private IMultimediaManager multimediaManager;
        //登录者账号ID
        private  string currentUserID;
        // 房间号
        private string roomID; 
        //登录密码
        private string password;
         

        /// <summary>
        /// 登录成功时，触发此事件。参数：房间号RoomId
        /// </summary>
        public event CbGeneric<string> loginSuccess;
        public LoginWindow(IMultimediaManager _multimediaManager)
        {
            InitializeComponent(); 
            this.multimediaManager = _multimediaManager;

            this.Closing += LoginWindow_Closing;
        }
        #region 登录参数设置
        private void Login()
        {
            // OMCS初始化 

            #region OMCS参数设置          
            //OMCS.GlobalUtil.SetMaxLengthOfUserID(20);
            multimediaManager.CameraVideoSize = new System.Drawing.Size(1280, 720);
            multimediaManager.CameraDeviceIndex = 0;
            multimediaManager.MicrophoneDeviceIndex = 0;
            multimediaManager.SpeakerIndex = 0;
            multimediaManager.DesktopEncodeQuality = 18;
            multimediaManager.MaxDesktopFrameRate = 15;
            multimediaManager.CameraEncodeQuality = 18;
            multimediaManager.MaxCameraFrameRate = 15;
            multimediaManager.Advanced.AudioMixedStrategy = AudioMixedStrategy.DecibelTop2;
            multimediaManager.Advanced.AutoAdjustCameraOutputSize = true;
            #endregion
            multimediaManager.Initialize(this.currentUserID, this.password, ConfigurationManager.AppSettings["ServerIP"], int.Parse(ConfigurationManager.AppSettings["OmcsPort"]));
         
            this.Hide();
            this.loginSuccess?.Invoke(this.roomID);
        }
        #endregion

        #region 点击登录
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.roomID = this.roomNumber.Text.Trim();
                this.currentUserID = this.loginId.Text.Trim();
                if (this.roomNumber.Text.Trim() == "")
                {
                    return;
                }
                if (this.currentUserID.Length > 21)
                {
                    MessageBox.Show("登录账号的长度不能超过21");
                    return;
                }

                this.password = this.loginPassword.Password.Trim();
                this.Login();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message +"_"+ee.StackTrace);                
            }

        }
        #endregion

        #region 关闭页面 退出程序
        private void LoginWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.multimediaManager.Dispose(); 
        }
        #endregion
    }
}
