using ESBasic;
using OMCS;
using OMCS.Passive;
using OMCS.Passive.Audio;
using OMCS.Passive.MultiChat;
using OMCS.Passive.Video;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows;
using System.Windows.Controls;

namespace OVCS.ClientWpf
{
    /// <summary>
    /// 小窗口容器视频
    /// </summary>
    public partial class VideoPanel : UserControl
    {
        private IChatUnit chatUnit;
        public IChatUnit ChatUnit
        {
            get { return chatUnit; }
        }
        private bool isMySelf = false;
        
        //当前的用户id
        public string MemberID
        {
            get
            {
                if (this.chatUnit == null)
                {
                    return null;
                }
                return this.chatUnit.MemberID;
            }
        }

        //摄像头连接器
        public DynamicCameraConnector DynamicCameraConnector
        {
            get { return this.chatUnit.DynamicCameraConnector; }
        }
        /// <summary>
        /// 其他用户 摄像头开启或关闭
        /// </summary>
        public event CbGeneric<bool, IChatUnit> CameraStateChange;

        /// <summary>
        /// 状态改变触发 
        /// </summary>
        public event CbGeneric<string, string> ChangeState;

        /// <summary>
        /// 视频正常连接成功并输出显示时 触发该事件 
        /// </summary>
        public event CbGeneric<int, IChatUnit> VideoByCount;

        /// <summary>
        /// 视频连接成功触发
        /// </summary>
        public event CbGeneric<IChatUnit> VideoConnect;

        public VideoPanel()
        {
            InitializeComponent();         
        }       

        /// <summary>
        /// 初始化成员视频显示控件。
        /// </summary>    
public void Initialize(IChatUnit unit, bool myself)
{

    this.pictureBox_Mic.RenderSize = new System.Windows.Size(24, 24);
    this.chatUnit = unit;
    this.isMySelf = myself;
    this.toolStrip1.Text = chatUnit.MemberID;
    //初始化麦克风连接器            
    this.chatUnit.MicrophoneConnector.ConnectEnded += new CbGeneric<string, ConnectResult>(MicrophoneConnector_ConnectEnded);
    this.chatUnit.MicrophoneConnector.OwnerOutputChanged += new CbGeneric<string>(MicrophoneConnector_OwnerOutputChanged);
    
    if (!this.isMySelf)
    {
        this.chatUnit.MicrophoneConnector.BeginConnect(unit.MemberID);
    }
    if (this.isMySelf)
    {
        this.videoSizeShow.Visibility = Visibility.Collapsed;
    }
    //初始化摄像头连接器
    this.chatUnit.DynamicCameraConnector.SetViewer(this.cameraPanel1);
    this.chatUnit.DynamicCameraConnector.VideoDrawMode = VideoDrawMode.Scale;
    this.chatUnit.DynamicCameraConnector.ConnectEnded += new CbGeneric<string, ConnectResult>(DynamicCameraConnector_ConnectEnded);
    this.chatUnit.DynamicCameraConnector.OwnerOutputChanged += new CbGeneric<string>(DynamicCameraConnector_OwnerOutputChanged);
    this.chatUnit.DynamicCameraConnector.Disconnected += new CbGeneric<string, ConnectorDisconnectedType>(DynamicCameraConnector_Disconnected);
    this.chatUnit.DynamicCameraConnector.OwnerVideoSizeChanged += DynamicCameraConnector_OwnerVideoSizeChanged;
    this.chatUnit.DynamicCameraConnector.BeginConnect(unit.MemberID);
}

        private void DynamicCameraConnector_OwnerVideoSizeChanged(string ownerID, System.Drawing.Size size)
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.BeginInvoke(new CbGeneric<string, System.Drawing.Size>(this.DynamicCameraConnector_OwnerVideoSizeChanged), ownerID, size);
            }
            else
            {
                if (ownerID == this.chatUnit.MemberID)
                {
                    this.videoSizeShow.Text = string.Format("{0}*{1}", size.Width, size.Height);
                }
            }
        }

        void DynamicCameraConnector_Disconnected(string ownerID, ConnectorDisconnectedType disconnectedType)
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.BeginInvoke(new CbGeneric<string, ConnectorDisconnectedType>(this.DynamicCameraConnector_Disconnected), ownerID, disconnectedType);
            }
            else
            {
                this.ChangeState?.Invoke(ownerID, "摄像头连接断开");
                this.VideoByCount?.Invoke(-1,this.chatUnit);
                this.disConnectReason.Visibility = Visibility.Visible;
                this.disConnectReason.Text = disconnectedType.ToString();
            }
        }

        //好友启用或禁用摄像头
        void DynamicCameraConnector_OwnerOutputChanged(string ownerID)
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.BeginInvoke(new CbGeneric<string>(this.DynamicCameraConnector_OwnerOutputChanged), ownerID);
            }
            else
            {
                this.ShowCameraState(ownerID,this.chatUnit.DynamicCameraConnector.OwnerOutput);
                this.CameraStateChange?.Invoke(this.chatUnit.DynamicCameraConnector.OwnerOutput, this.chatUnit);
                this.VideoByCount?.Invoke(this.chatUnit.DynamicCameraConnector.OwnerOutput ? 1 : -1, this.chatUnit);
            }
        }

        private ConnectResult cameraConnectResult;
        public ConnectResult CameraConnectResult
        {
            get { return cameraConnectResult; }
        }
        //摄像头连接器尝试连接的结果
        void DynamicCameraConnector_ConnectEnded(string ownerID, ConnectResult res)
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.BeginInvoke(new CbGeneric<string, ConnectResult>(this.DynamicCameraConnector_ConnectEnded), ownerID, res);
            }
            else
            {
                if (res == ConnectResult.Succeed)
                {
                    if (this.chatUnit.DynamicCameraConnector.OwnerMachineType == MachineType.Android || this.chatUnit.DynamicCameraConnector.OwnerMachineType == OMCS.Passive.Video.MachineType.IOS)
                    {
                        this.chatUnit.DynamicCameraConnector.VideoDrawMode = VideoDrawMode.Scale;
                    }
                    this.videoSizeShow.Text = string.Format("{0}*{1}", this.chatUnit.DynamicCameraConnector.VideoSize.Width, this.chatUnit.DynamicCameraConnector.VideoSize.Height);
                    this.VideoByCount?.Invoke(this.chatUnit.DynamicCameraConnector.OwnerOutput ? 1 : -1, this.chatUnit);
                    this.VideoConnect?.Invoke(this.chatUnit);
                }
                else
                {
                    this.VideoByCount?.Invoke(-1, this.chatUnit);
                }
                this.cameraConnectResult = res;
                this.ShowCameraState(ownerID,this.chatUnit.DynamicCameraConnector.OwnerOutput);
            }
        }

        /// <summary>
        /// 综合显示摄像头的状态。
        /// </summary>
        private void ShowCameraState(string ownerID,bool isCameraOutput)
        {
            if (!isMySelf)
            {
                this.ChangeState?.Invoke(ownerID, isCameraOutput ? "开启摄像头" : "关闭摄像头");
                if (this.chatUnit.Tag !=null)
                {
                    ((VideoAllPanel)this.chatUnit.Tag).videoHidePanel.cameraVisibilityChange(isCameraOutput);
                }
            }
            if (isCameraOutput)
            {
                this.disConnectReason.Visibility = Visibility.Collapsed;
            }
            if (!this.chatUnit.DynamicCameraConnector.OwnerOutput)
            {
                return;
            }
        }

        //好友启用或禁用麦克风
        void MicrophoneConnector_OwnerOutputChanged(string ownerID)
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.BeginInvoke(new CbGeneric<string>(this.MicrophoneConnector_OwnerOutputChanged) ,ownerID);
            }
            else
            {
                this.ShowMicState(ownerID,this.chatUnit.MicrophoneConnector.OwnerOutput);
                if (!this.chatUnit.MicrophoneConnector.OwnerOutput)
                {
                    this.SpeakerDecibelChange(null);
                    ((VideoAllPanel)this.chatUnit.Tag).videoHidePanel.SpeakerDecibelDownChange(null);
                }
            }
        }

        private ConnectResult connectMicResult;
        //麦克风连接器尝试连接的结果
        void MicrophoneConnector_ConnectEnded(string ownerID, ConnectResult res)
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.BeginInvoke(new CbGeneric<string, ConnectResult>(this.MicrophoneConnector_ConnectEnded), ownerID, res);
            }
            else
            {
                this.connectMicResult = res;
                this.ShowMicState(ownerID,this.chatUnit.MicrophoneConnector.OwnerOutput);
            }
        }

        /// <summary>
        /// 综合显示麦克风的状态。
        /// </summary>
        public void ShowMicState(string ownerID , bool isMicOutput)
        {
            if (this.connectMicResult != ConnectResult.Succeed)
            {
                this.pictureBox_Mic.Visibility = Visibility.Visible;
                this.pictureBox_Mic.ToolTip = "主人麦克风无效";
                this.pictureBox_Mic.Source = ResourceManager.Singleton.MicUselessImage;
                if (this.chatUnit.Tag != null)
                {
                    ((VideoAllPanel)this.chatUnit.Tag).videoHidePanel.pictureBox_Mic.Visibility = Visibility.Visible;
                    ((VideoAllPanel)this.chatUnit.Tag).videoHidePanel.pictureBox_Mic.ToolTip = "主人麦克风无效";
                    ((VideoAllPanel)this.chatUnit.Tag).videoHidePanel.pictureBox_Mic.Source = ResourceManager.Singleton.MicUselessImage;
                }
            }
            else
            {
                this.pictureBox_Mic.ToolTip = isMicOutput ? "主人麦克风开启" : "主人麦克风关闭";
                this.pictureBox_Mic.Source = isMicOutput ? ResourceManager.Singleton.MicEnableImage : ResourceManager.Singleton.MicDisableImage;
                if (this.chatUnit.Tag != null)
                {
                    ((VideoAllPanel)this.chatUnit.Tag).videoHidePanel.pictureBox_Mic.ToolTip = isMicOutput ? "主人麦克风开启" : "主人麦克风关闭";
                    ((VideoAllPanel)this.chatUnit.Tag).videoHidePanel.pictureBox_Mic.Source = isMicOutput ? ResourceManager.Singleton.MicEnableImage : ResourceManager.Singleton.MicDisableImage;
                }
                if (!isMicOutput&& this.isMySelf) 
                { 
                    this.SpeakerDecibelChange(null);
                    if (this.chatUnit.Tag != null)
                    {
                        ((VideoAllPanel)this.chatUnit.Tag).videoHidePanel.SpeakerDecibelDownChange(null);
                    }
                }
                if (!this.isMySelf) 
                {
                    this.ChangeState?.Invoke(ownerID, isMicOutput ? "开启麦克风" : "关闭麦克风");
                }
            }
        }
        // 根据分贝值 渲染声音分贝律动条
        public void SpeakerDecibelChange(UserDecibel userdecibel)
        {
            this.decibelProgress.Value = userdecibel != null? userdecibel.Decibel:0;
        }

        #region 摄像头索引改变时，重新调用BeginConnect

        internal void CameraIndexChange()
        {
            if (this.cameraConnectResult != ConnectResult.Succeed)
            {
                this.chatUnit.DynamicCameraConnector.BeginConnect(this.chatUnit.MemberID);
            }
        }

        #endregion
    }
}
