using ESBasic;
using OMCS.Passive;
using OMCS.Passive.Audio;
using OMCS.Passive.MultiChat;
using OMCS.Passive.Video;
using OVCS.ClientWpf.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OVCS.ClientWpf
{
    /// <summary>
    /// videoHidePanel.xaml 的交互逻辑
    /// </summary>
    public partial class VideoHidePanel : UserControl
    {
        private IChatUnit unit;
       
        //当前的用户id

        public string MemberID
        {
            get 
            {
                if (this.unit == null)
                {
                    return null;
                }
                return this.unit.MemberID;
            }
        }
        public bool isMySelf = false;
        /// <summary>
        /// 其他用户 摄像头开启或关闭
        /// </summary>
        public event CbGeneric<bool, IChatUnit> CameraStateChange;

        public VideoHidePanel()
        {
            InitializeComponent();
        }
        public void Initialize(bool isVideoShow,IChatUnit _unit,bool _isMySelf)
        {
            this.isMySelf = _isMySelf;
            this.unit = _unit;
            this.pictureBox_Head.ImageSource = CommonHelper.GetUserHeadByID(this.unit.MemberID);
            this.pictureBox_Camera.Visibility = ExtenseMethod.GetVisibility(isVideoShow);
            this.toolStrip1.Text = this.unit.MemberID;
            this.unit.DynamicCameraConnector.OwnerOutputChanged += DynamicCameraConnector_OwnerOutputChanged;
            this.unit.DynamicCameraConnector.ConnectEnded += DynamicCameraConnector_ConnectEnded;
        }

        private void DynamicCameraConnector_ConnectEnded(string ownerID, ConnectResult res)
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.BeginInvoke(new CbGeneric<string, ConnectResult>(this.DynamicCameraConnector_ConnectEnded), ownerID, res);
            }
            else
            {
                this.cameraVisibilityChange(res == ConnectResult.Succeed && this.unit.DynamicCameraConnector.OwnerOutput);
            }
        }

        // 会议成员数超过9个时,设置摄像头状态
        public void cameraVisibilityChange(bool isShow) 
        {
            this.pictureBox_Camera.Visibility = ExtenseMethod.GetVisibility(isShow);
        }

        // 根据分贝值 渲染声音分贝律动条
        public void SpeakerDecibelDownChange(UserDecibel userdecibel)
        {
            this.decibelProgress.Value = userdecibel != null ? userdecibel.Decibel : 0;
        }

        /// <summary>
        /// 好友启用或禁用摄像头
        /// </summary>
        /// <param name="ownerID">主人id</param>
        void DynamicCameraConnector_OwnerOutputChanged(string ownerID)
        {
            if (!Dispatcher.CheckAccess())
            {
                Dispatcher.BeginInvoke(new CbGeneric<string>(this.DynamicCameraConnector_OwnerOutputChanged), ownerID);
            }
            else
            {
                this.CameraStateChange?.Invoke(this.unit.DynamicCameraConnector.OwnerOutput, this.unit);
            }
        }
    }
}
