using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace CompanyServer
{
    public partial class Form1 : Form
    {
        List<Form> OpenForms = new List<Form>();

        public Form1()
        {
            InitializeComponent();
         
        }



        #region 公共方法
        bool ActiveChildForm(string formname)
        {
            bool result = false;
            foreach (Form f in OpenForms)
            {
                if (f.Name == formname)
                {
                    if (f.Visible == false)
                    {
                        f.Show();
                    }
                    else
                    {
                        f.BringToFront();
                        f.Activate();
                    }
                    (f.Tag as ToolStripMenuItem).Checked = true;
                    result = true;
                }
                else
                {
                    (f.Tag as ToolStripMenuItem).Checked = false;
                }
            }
            return result;
        }

        void AddChildForm(Form childform)
        {
            childform.FormClosed += new FormClosedEventHandler(childform_FormClosed);
            ToolStripMenuItem tsmi = new ToolStripMenuItem();
            tsmi.Click += new EventHandler(tsmi_Click);
            childform.Tag = tsmi;
            OpenForms.Add(childform);
            窗口.DropDownItems.Add(tsmi);

            for (int i = 0; i < OpenForms.Count; i++)
            {
                (OpenForms[i].Tag as ToolStripMenuItem).Text = (i + 1).ToString() + ". " + OpenForms[i].Name;
            }

            ActiveChildForm(childform.Name);

            窗口菜单分割条.Visible = true;
        }

        void tsmi_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < OpenForms.Count; i++)
            {
                if ((OpenForms[i].Tag as ToolStripMenuItem).Equals(sender as ToolStripMenuItem))
                {
                    ActiveChildForm(OpenForms[i].Name);
                    break;
                }
            }
        }

        void childform_FormClosed(object sender, FormClosedEventArgs e)
        {
            DelteChildForm(sender as Form);



        }

        void DelteChildForm(Form childform)
        {
            窗口.DropDownItems.Remove(childform.Tag as ToolStripMenuItem);
            OpenForms.Remove(childform);

            for (int i = 0; i < OpenForms.Count; i++)
            {
                (OpenForms[i].Tag as ToolStripMenuItem).Text = (i + 1).ToString() + ". " + OpenForms[i].Name;
            }

            if (OpenForms.Count > 0)
            {
                ActiveChildForm(OpenForms[0].Name);
            }
            else
            {
                窗口菜单分割条.Visible = false;
            }
        }

        void Item_click(HNode hn)
        {
            if (hn.FormObject == null)
            {
                hn.CreatForm();
            }

            Form _f = hn.FormObject;

            if (ActiveChildForm(_f.Name) == false)
            {
                if (_f == null)
                {
                    MessageBox.Show("未找到窗体[" + hn.FormCation + "]的定义!");
                    return;
                }

                _f.MdiParent = this;
                _f.WindowState = FormWindowState.Maximized;
                AddChildForm(_f);
            }
        }

        #endregion

        /// <summary>
        /// 禁止关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //e.Cancel = true;
        }

        private void 基础设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
           

            HNode hn = new HNode();
            hn.ClassName = "CompanyServer.基础配置";
            hn.DllName = "Application";
            hn.FormCation = "基础配置";

            Item_click(hn);
        }


        private void 上海直报ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            HNode hn = new HNode();
            hn.ClassName = "CompanyServer.上海直报";
            hn.DllName = "Application";
            hn.FormCation = "上海直报";

            Item_click(hn);
        }
    }
}
