using CPF;
using CPF.Controls;
using CPF.Drawing;
using ESBasic;
using OMCS.Passive.MultiChat;
using System;
using static OVCS.ClientLinux.MultiVideoChatContainer;

namespace OVCS.ClientLinux.Tools
{
    [CPF.Design.DesignerLoadStyle("res://OVCS.ClientLinux/Resource/Stylesheet1.css")]//用于设计的时候加载样式

    //聊天面板控件
    public class AutoScaleContainer : Control
    {
        
        protected override void InitializeComponent()
        {
            CommandContext = this;
            DataContext = this;
            Background = CPF.Drawing.Color.White;
            Size = SizeField.Fill;
            Children.Add(new Grid
            {
                Size = SizeField.Fill,
                MarginTop = 0,
                MarginBottom = 0,
                MarginLeft = 0,
                MarginRight = 0,
                Children =
                {
                    new WrapPanel
                    {
                        Orientation = Orientation.Horizontal,
                        Width = 890,
                        Height = 548,
                        MarginLeft= 0,
                        MarginRight= 0,
                        MarginBottom= 0,
                        MarginTop= 0,
                        Visibility= Visibility.Visible,
                        Name="wrapPanel",
                        PresenterFor = this,
                        Children=
                        {

                        }
                    }
                }
            }) ;

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

        public void AutoScaleContainer_DesiredSizeChanged(object sender, EventArgs e)
        {
            this.SetStandardSize(new CPF.Drawing.Size(32, 19));
            if (this.ActualSize.Width > 0 && this.ActualSize.Height > 0)
            {
                UserControl_SizeChanged();
            }
        }

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

        private Size standardSize;
        private WrapPanel flowLayoutPanel;
        public WrapPanel FlowLayoutPanel
        {
            get { return flowLayoutPanel; }
        }
        public void SetComponent()
        {
            flowLayoutPanel = this.FindPresenterByName <WrapPanel>("wrapPanel");
        }
        protected override void OnInitialized()
        {
            base.OnInitialized();
        }
        /// <summary>
        /// 窗口尺寸修改后触发，参数：Size - 新的尺寸
        /// </summary>
        public event CbGeneric<Size> videoPanelSizeChange;
        //用户代码

        //设置等比缩放
        public void UserControl_SizeChanged()
        {

            if (this.flowLayoutPanel == null)
            {
                return;
            }

            float wCoef = (float)(this.ActualSize.Width / this.standardSize.Width);
            float hCoef = (float)(this.ActualSize.Height / this.standardSize.Height);
            if (wCoef == hCoef)
            {
                this.flowLayoutPanel.Width = this.ActualSize.Width;
                this.flowLayoutPanel.Height = this.ActualSize.Height;
                this.flowLayoutPanel.MarginLeft = 0;
                this.flowLayoutPanel.MarginRight = 0;
                this.flowLayoutPanel.MarginTop = 0;
                this.flowLayoutPanel.MarginBottom = 0;
                this.videoPanelSizeChange?.Invoke(new Size(this.flowLayoutPanel.Width.Value, this.flowLayoutPanel.Height.Value));
                return;
            }

            if (wCoef < hCoef)
            {
                int newHeight = (int)(wCoef * this.standardSize.Height);
                this.flowLayoutPanel.Width = this.ActualSize.Width;
                this.flowLayoutPanel.Height = newHeight;
                this.flowLayoutPanel.MarginLeft = 0;
                this.flowLayoutPanel.MarginRight = 0;
                this.flowLayoutPanel.MarginTop = (this.ActualSize.Height - newHeight) / 2;
                this.flowLayoutPanel.MarginBottom = (this.ActualSize.Height - newHeight) / 2;
                this.videoPanelSizeChange?.Invoke(new Size(this.flowLayoutPanel.Width.Value, this.flowLayoutPanel.Height.Value));
                return;
            }
            int newWidth = (int)(hCoef * this.standardSize.Width);
            if (newWidth < 0)
            {
                return;
            }
            this.flowLayoutPanel.Width = newWidth;
            this.flowLayoutPanel.Height = this.ActualSize.Height;
            this.flowLayoutPanel.MarginLeft = (this.ActualSize.Width - newWidth) / 2;
            this.flowLayoutPanel.MarginRight = (this.ActualSize.Width - newWidth) / 2;
            this.flowLayoutPanel.MarginTop = 0;
            this.flowLayoutPanel.MarginBottom = 0;
            this.videoPanelSizeChange?.Invoke(new Size(this.flowLayoutPanel.Width.Value, this.flowLayoutPanel.Height.Value));
        }

        /// <summary>
        /// 设置标准尺寸。
        /// </summary>        
        /// <param name="_standardSize">用于等比缩放进行参考的标准尺寸</param>
        public void SetStandardSize(Size _standardSize)
        {
            this.standardSize = _standardSize;
        }
//#endif
    }
}
