using MGdu.WinFormUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace RySkins
{
    public partial class SkinMdiForm : GMForm
    {
        /// <summary>
        /// 顶部栏
        /// </summary>
        [Description("顶部栏")]
        public Panel TopPanel { get; set; } = null;
        public SkinMdiForm()
        {
            InitializeComponent();
            InitMdiSkin();
        }
        #region 加载皮肤
        /// <summary>
        /// 加载皮肤
        /// </summary>
        private void InitMdiSkin()
        {
            _mdiClient = FindMdiClient();
            base.SizeChanged += new EventHandler((object sender, EventArgs e) =>
            {
                if (base.WindowState == FormWindowState.Maximized)
                {
                    base.ControlBoxOffset = new Point(4, base.BorderWidth);
                }
                else
                {
                    base.ControlBoxOffset = new Point(4, 1);
                }
                SetFormItemLocation(TopPanel);
            });
            base.Load += new EventHandler((object sender, EventArgs e) =>
            {
                base.XTheme = new ThemeForm();
                base.XTheme.Mdi_TabMinWidth = 70;
                base.XTheme.Mdi_TabNormalWidth = 100;
                base.XTheme.Mdi_TabMaxWidth = 145;
                base.XTheme.Mdi_ShowNewTabBtn = false;
                base.XTheme.Mdi_AlwaysShowListAllBtn = false;
                base.XTheme.Mdi_BarBottomRegionHeight = 0;
                base.XTheme.Mdi_ShowTabCloseIcon = false;
                base.XTheme.Mdi_TabShowBorder = false;
                base.XTheme.Mdi_BarMargin = new Padding(6, 0, 100, 0);
                base.XTheme.Mdi_TabActiveBackColorTop = Color.White;
                base.XTheme.Mdi_TabActiveBackColorBottom = Color.FromArgb(248, 248, 248);
                base.XTheme.Mdi_TabDeactiveBackColorTop = Color.FromArgb(230, 230, 230);
                base.XTheme.Mdi_TabDeactiveBackColorBottom = Color.FromArgb(230, 230, 230);
                //base.XTheme.Mdi_UseMsgToActivateChild
                base.MdiNewTabButtonClick += new EventHandler(NewFormClick);
                base.MdiTabCloseButtonClick += new EventHandler(ChildFormClose);
                //base.XTheme.Mdi_TabTopSpace += 80;
                if (_mdiClient != null)
                {
                    _mdiClient.Dock = DockStyle.None;
                    _mdiClient.BackColor = Color.White;
                }
            });
            base.MdiBarCreated += new EventHandler((object sender, EventArgs e) =>
            {
                SetFormItemLocation(TopPanel);
                Application.DoEvents();
                Thread th = new Thread(Start);
                th.Start();
                void Start()
                {
                    Thread.Sleep(10);
                    this.Invoke(new Action(() =>
                    {
                        this.SuspendLayout();
                        for (int i = 0; i < MdiForms.Count; i++)
                        {
                            CreateNewChild(MdiForms[i]);
                        }
                        this.ResumeLayout(false);
                        //CreateNewChild(typeof(DbOp.FrmCaijiView));
                        //Application.DoEvents();
                        if (this.MdiChildren.Length > 0)
                        {
                            this.ActivateMdiChildForm(this.MdiChildren[0]);
                        }
                    }));
                }
            });
        }
        /// <summary>
        /// 要初始化加载的Form列表
        /// </summary>
        public List<Form> MdiForms { get; set; } = new List<Form>();
        /// <summary>
        /// 创建子窗体
        /// </summary>
        /// <param name="form"></param>
        public void CreateNewChild(Form form)
        {
            //form.SuspendLayout();
            form.FormClosed += new FormClosedEventHandler(base.MdiChildClosed);
            form.VisibleChanged += new EventHandler(base.MdiChildVisibleChange);
            form.MdiParent = this;
            //f.BackColor = Color.White;
            form.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            form.SizeGripStyle = SizeGripStyle.Show;
            form.WindowState = FormWindowState.Maximized;
            form.Show();
           // form.ResumeLayout(false);
            //f.Focus();
        }
        MdiClient _mdiClient;
        private int PanelTopHeight { get; set; } = 43;

        private void NewFormClick(object sender, EventArgs e)
        {
            //  CreateNewChild();
        }
        private void ChildFormClose(object sender, EventArgs e)
        {
            if (sender is Form f)
                f.Close();
        }
        private MdiClient FindMdiClient()
        {
            MdiClient mdi = null;
            foreach (Control c in Controls)
            {
                if ((mdi = c as MdiClient) != null)
                {
                    break;
                }
            }
            return mdi;
        }
        private void SetMdiBarLocation()
        {
            Padding newMargin = Padding.Empty;
            //if (WindowState == FormWindowState.Maximized)
            //    newMargin = new Padding(BorderWidth, BorderWidth, BorderWidth, 0);
            //else
            //PnlMenu.Top = BorderWidth + base.XTheme.CaptionHeight;
            newMargin = new Padding(BorderWidth, BorderWidth + base.XTheme.CaptionHeight + 0, BorderWidth, 0);
            if (base.XTheme.Mdi_BarMargin != newMargin)
            {
                base.XTheme.Mdi_BarMargin = newMargin;
                Invalidate();
            }
        }
        private void SetPanelTopLocation(Panel PnlTop)
        {
            Point newLocation = new Point(base.MdiBarBounds.Left, base.MdiBarBounds.Bottom);
            //if (panelTop.Location == newLocation)
            //    return;
            //var size = new Size(base.MdiBarBounds.Width, PanelTopHeight);
            //if (PnlTop != null)
            //{
            //    PnlTop.Anchor = AnchorStyles.None;
            //    PnlTop.Location = newLocation;
            //    PnlTop.Size = size;
            //    PnlTop.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            //}
        }
        private void SetFormItemLocation(Panel PnlTop)
        {
            SetMdiBarLocation();
            //SetPanelTopLocation(PnlTop);
            SetMdiClientLocation(PnlTop);
        }
        private void SetMdiClientLocation(Panel PnlTop)
        {
            if (_mdiClient == null)
                return;
            Point newLocation = Point.Empty;
            var size = ClientRectangle.Size;
            if (PnlTop != null)
            {
                newLocation = new Point(PnlTop.Left, PnlTop.Bottom);
                if (_mdiClient.Location == newLocation)
                    return;

                int height = base.ClientSize.Height - newLocation.Y -
                    (base.ClientSize.Width - PnlTop.Width) / 2;

                size = new Size(PnlTop.Width, height);
            }
            _mdiClient.Anchor = AnchorStyles.None;
            _mdiClient.Location = newLocation;
            _mdiClient.Size = size;
            _mdiClient.Anchor =
                AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
        }
        #endregion
    }
}
