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

namespace OVCS.ClientLinux
{
    [CPF.Design.DesignerLoadStyle("res://OVCS.ClientLinux/Resource/Stylesheet1.css")]//用于设计的时候加载样式
    public class VideoHidePanel : Control
    {
        private bool isInit;
        public VideoHidePanel()
        {
            this.InitializeComponent();
        }
        protected override void InitializeComponent()
        {
             if (isInit)
             {
                 return;
             }
             isInit = true;
             MarginLeft = 2;
             MarginRight = 2;
             MarginBottom = 2;
             Height = 48;
             Width = 190;
             Background = "#404040";
             Children.Add(new Grid
             {
                MarginLeft = 0,
                Background = "#404040",
                ColumnDefinitions =
                {
                    new ColumnDefinition
                    {
                        Width = GridLength.Auto
                    },
                    new ColumnDefinition
                    {
                        Width = GridLength.Star
                    }
                },
                Children =
                {
                    new Border
                    {
                        MarginLeft = 5,
                        MarginRight = 5,
                        Size = SizeField.Fill,
                        BorderFill = Color.Transparent,
                        Child = new Picture
                        {
                           Name="pictureBox_Head",
                           PresenterFor = this,
                           Height = 45,
                           Width = 45,
                           Stretch = Stretch.UniformToFill,
                           Source = CommonHelper.ResourcesCatalog + "head.png",
                        },
                        Attacheds =
                        {
                            {
                                Grid.ColumnIndex,
                                0
                            }
                        }
                    },        
                    new Grid
                    {
                       MarginTop= 0,
                       MarginLeft = 5,
                       RowDefinitions =
                       {
                           new RowDefinition
                           {
                               Height = GridLength.Star
                           },
                           new RowDefinition
                           {
                               Height = GridLength.Star
                           }
                       },
                       Children =
                       {
                           new Label
                           {
                                MarginLeft = 5,
                                MarginTop = 0,
                                Text = "aa02",
                                Foreground = Color.White,
                                FontSize = 15,
                                Name = "toolStrip1",
                                PresenterFor = this,
                                Attacheds =
                                {
                                    {
                                        Grid.RowIndex,
                                        0
                                    }
                                }
                           },
                           new StackPanel
                           {
                               MarginTop= 3,
                               MarginLeft = 5,
                               Orientation = Orientation.Horizontal,
                               Children =
                               {
                                   new Picture
                                   {
                                       Name="pictureBox_Camera",
                                       PresenterFor= this,
                                       ToolTip="摄像头已打开",
                                       MarginRight = 5,
                                       MarginLeft = 0,
                                       Visibility = Visibility.Collapsed,
                                       Height = 20,
                                       Width = 20,
                                       Stretch = Stretch.UniformToFill,
                                       Source = CommonHelper.ResourcesCatalog + "cameraNor.png"
                                   },
                                   new Picture
                                   {
                                       Name = "pictureBox_Mic",
                                       PresenterFor = this,
                                       ToolTip="麦克风已打开",
                                       MarginLeft = 0,
                                       MarginRight= 5,
                                       Source = CommonHelper.ResourcesCatalog + "micNor.png",
                                       Stretch = Stretch.UniformToFill,
                                       Height = 20,
                                       Width = 20,
                                   },
                                   new ProgressBar
                                   {
                                       Name="decibelProgress",
                                       PresenterFor = this,
                                       CornerRadius = "0",
                                       MarginLeft = 0,
                                       MarginRight= 0,
                                       Height = 7,
                                       Width= 70,
                                       Maximum = 100
                                   }
                               },
                               Attacheds =
                               {
                                    {
                                        Grid.RowIndex,
                                        1
                                    }
                                }
                           }
                       },
                       Attacheds =
                        {
                            {
                                Grid.ColumnIndex,
                                1
                            }
                        }
                   }
                },
            }) ;

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



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

        private Picture pictureBox_Mic;
        public Picture PictureBox_Mic
        {
            get { return pictureBox_Mic; }
        }
        private ProgressBar decibelProgress;
        private Label toolStrip1;
        private Picture pictureBox_Head;
        private Picture pictureBox_Camera;

        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;

        private void SetComponent()
        {
            pictureBox_Mic = this.FindPresenterByName<Picture>("pictureBox_Mic");
            toolStrip1 = this.FindPresenterByName<Label>("toolStrip1");
            decibelProgress = this.FindPresenterByName<ProgressBar>("decibelProgress");
            pictureBox_Head = this.FindPresenterByName<Picture>("pictureBox_Head");
            pictureBox_Camera = this.FindPresenterByName<Picture>("pictureBox_Camera");
        }
        protected override void OnInitialized()
        {
            base.OnInitialized();
        }

        //用户代码


        /// <summary>
        /// 初始化成员视频显示控件。
        /// </summary>    
        public void Initialize(bool isVideoShow, IChatUnit _unit, bool _isMySelf)
        {
           
            try
            {
                this.isMySelf = _isMySelf;
                this.unit = _unit;
                this.pictureBox_Head.Source = CommonHelper.GetUserHead4ID(this.unit.MemberID);
                this.pictureBox_Camera.Visibility = isVideoShow ? Visibility.Visible : Visibility.Collapsed;
                this.toolStrip1.Text = this.unit.MemberID;
                this.unit.DynamicCameraConnector.OwnerOutputChanged += DynamicCameraConnector_OwnerOutputChanged;
                this.unit.DynamicCameraConnector.ConnectEnded += DynamicCameraConnector_ConnectEnded;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void DynamicCameraConnector_ConnectEnded(string ownerID, ConnectResult res)
        {
            UiSafeInvoker.ActionOnUI(delegate
            {
                this.cameraVisibilityChange(res == ConnectResult.Succeed && this.unit.DynamicCameraConnector.OwnerOutput);
            });
        }
        
        // 会议成员数超过9个时,设置摄像头状态
        public void cameraVisibilityChange(bool isShow)
        {
            this.pictureBox_Camera.Visibility = isShow?Visibility.Visible:Visibility.Collapsed;
        }

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

        //好友启用或禁用摄像头
        void DynamicCameraConnector_OwnerOutputChanged(string ownerID)
        {
            UiSafeInvoker.ActionOnUI(delegate
            {
                this.CameraStateChange?.Invoke(this.unit.DynamicCameraConnector.OwnerOutput, this.unit);
            });
        }
//#endif
    }
}
