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;
        /// <summary>
        /// 左侧栏
        /// </summary>
        [Description("左侧栏")]
        public Panel LeftPanel { get; set; } = null;
        public SkinMdiForm()
        {
            InitializeComponent();
            InitMdiSkin();
        }
        public SkinMdiForm(string[] args)
        {
            InitializeComponent();
            InitMdiSkin();
        }
        #region 加载皮肤
        /// <summary>
        /// 加载皮肤
        /// </summary>
        public 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 RySkins.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 = true;
                base.XTheme.Mdi_TabShowBorder = false;
                base.XTheme.Mdi_BarMargin = new Padding(6, 0, 100, 0);
                base.XTheme.Mdi_TabActiveBackColorTop = Color.White;
                base.XTheme.Mdi_ShowTabIcon = false;
                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;
                }
                this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - Width) / 2, (Screen.PrimaryScreen.WorkingArea.Height - Height) / 2);
            });
            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();
                        //CreateNewChild(typeof(DYun.OTools.FrmGZ));
                        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();
        }
        private void CreateNewChild(Type form)
        {
            Form f = (Form)Activator.CreateInstance(form);
            f.FormClosed += new FormClosedEventHandler(base.MdiChildClosed);
            f.VisibleChanged += new EventHandler(base.MdiChildVisibleChange);
            f.MdiParent = this;
            //f.BackColor = Color.White;
            f.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            f.SizeGripStyle = SizeGripStyle.Show;
            f.WindowState = FormWindowState.Maximized;
            f.Show();
            //f.Focus();
        }

        MdiClient _mdiClient;
        private int PanelTopHeight
        {
            get
            {
                return TopPanel == null ? 0 : TopPanel.Height;
            }

        }

        private void NewFormClick(object sender, EventArgs e)
        {
            //  CreateNewChild();
        }
        /// <summary>
        /// 至少保留一个子窗体
        /// </summary>
        public bool AleastOneChild { get; set; } = true;
        private void ChildFormClose(object sender, EventArgs e)
        {
            if (sender is Form f)
            {
                if (AleastOneChild)
                {
                    if (this.GetCurrentMdiChildren().Count == 1) { return; }
                }
                f.Close();
            }
            this.Invalidate();
        }
        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;
            int left_bar_width = 0;
            if (LeftPanel != null) { left_bar_width = LeftPanel.Width; }
            newMargin = new Padding(BorderWidth + left_bar_width, BorderWidth + base.XTheme.CaptionHeight + PanelTopHeight, 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.XTheme.CaptionHeight + BorderWidth);
            //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)
        {
            if (DesignMode) { return; }
            SetMdiBarLocation();
            SetPanelTopLocation(PnlTop);
            SetMdiClientLocation(PnlTop);
        }
        private void SetMdiClientLocation(Panel PnlTop)
        {
            if (_mdiClient == null)
                return;
            int left_bar_width = 0;
            if (LeftPanel != null) { left_bar_width = LeftPanel.Width; }
            Point newLocation = new Point(BorderWidth, base.XTheme.CaptionHeight + base.XTheme.Mdi_TabHeight);
            var size = new Size(ClientRectangle.Width - BorderWidth - BorderWidth, ClientRectangle.Height - base.XTheme.CaptionHeight - base.XTheme.Mdi_TabHeight - BorderWidth);
            if (PnlTop != null)
            {
                newLocation = new Point(PnlTop.Left + left_bar_width, PnlTop.Bottom + base.XTheme.Mdi_TabHeight);
                if (_mdiClient.Location == newLocation)
                    return;

                int height = base.Height - newLocation.Y - BorderWidth - BorderWidth;

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

        private void SkinMdiForm_Load(object sender, EventArgs e)
        {

        }
    }
}
