using OMCS.Passive;
using System;
using System.Configuration;
using System.Diagnostics;
using System.Windows.Forms;

namespace OVCS
{
    public partial class LoginForm : Form
    { 

        #region UserID
        private string userID;
        public string UserID
        {
            get { return userID; }
        }
        #endregion

        #region RoomID
        private string roomID;
        public string RoomID
        {
            get { return roomID; }
        }
        #endregion

        #region MultimediaManager
        private IMultimediaManager multimediaManager = MultimediaManagerFactory.GetSingleton();
        public IMultimediaManager MultimediaManager
        {
            get { return multimediaManager; }
        } 
        #endregion     

              
        private string password = "";
        public LoginForm()
        {
            InitializeComponent();           
        }
       

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                this.roomID = this.textBox_roomID.Text.Trim();
                if (this.textBox_userID.Text.Trim() == "")
                {
                    return;
                }
                this.userID = this.textBox_userID.Text;                 
                if (this.userID.Length > 11)
                {
                    MessageBox.Show("登录账号的长度不能超过11");
                    return;
                }

                this.password = this.textBox_password.Text.Trim();

                this.label_state.Visible = true;
                this.Cursor = Cursors.WaitCursor;
                this.Refresh();
                if (!this.Login())
                {
                    this.label_state.Visible = false;
                    this.Cursor = Cursors.Default;
                    this.Refresh();
                    return;
                }                
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
            catch (Exception ee)
            {
                this.label_state.Visible = false;
                this.Cursor = Cursors.Default;
                MessageBox.Show(ee.Message);
                Program.Logger.Log(ee, "LoginForm.button2_Click登录的处理", ESBasic.Loggers.ErrorLevel.High);
            }
        }

        private bool Login()
        {        
            //OMCS 语音视频 参数设置   
            multimediaManager.MicrophoneDeviceIndex = 0;
            multimediaManager.CameraDeviceIndex = 0;
            multimediaManager.MaxDesktopFrameRate = 15;
            multimediaManager.DesktopEncodeQuality = 18;
            multimediaManager.MaxCameraFrameRate= 15;
            multimediaManager.CameraEncodeQuality = 18;
            multimediaManager.Advanced.AudioMixedStrategy = AudioMixedStrategy.DecibelTop2;
            multimediaManager.Advanced.AutoAdjustCameraOutputSize = true;
            try
            {
                string[] cameraSizeStr = ConfigurationManager.AppSettings["CameraVideoSize"].Split(',');
                multimediaManager.CameraVideoSize = new System.Drawing.Size(int.Parse(cameraSizeStr[0]), int.Parse(cameraSizeStr[1]));
            }
            catch { }
               
            multimediaManager.Initialize(userID, "", ConfigurationManager.AppSettings["ServerIP"], int.Parse(ConfigurationManager.AppSettings["OmcsPort"]));
            multimediaManager.OutputAudio = true;          
           
            return true;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.cnblogs.com/justnow");
        }
    }
    
}
