using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.IO;

namespace CompanyServer
{
    public class HNode
    {
        string formcaption;
        string dllname;
        string classname;
        int icoindex = 0;
        Form formobject;

        public string FormCation
        {
            get
            {
                return formcaption;
            }
            set
            {
                formcaption = value;
            }
        }

        public string DllName
        {
            get
            {
                return dllname;
            }
            set
            {
                dllname = value;
            }
        }

        public int IcoIndex
        {
            get
            {
                return icoindex;
            }
            set
            {
                icoindex = value;
            }
        }

        public string ClassName
        {
            get
            {
                return classname;
            }
            set
            {
                classname = value;
            }
        }

        public Form FormObject
        {
            get
            {
                return formobject;
            }
            set
            {
                formobject = value;
            }
        }

        public Form CreatForm()
        {
            if (formobject == null)
            {
                Assembly ass = GetAssembly(this.DllName);
                if (ass == null)
                {
                    return null;
                }
                try
                {
                    Type tp = ass.GetType(this.ClassName);
                    if (tp == null)
                    {
                        return null;
                    }

                    Form _f = CreateForm(tp);
                    if (_f == null)
                    {
                        MessageBox.Show("未找到窗体[" + this.FormCation + "]的定义!");
                        return null;
                    }
                    formobject = _f;
                    _f.FormClosing += new FormClosingEventHandler(_f_FormClosing);
                    return formobject;
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return null;
                }

            }
            else
            {
                return formobject;
            }
        }

        void _f_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.formobject = null;
        }

        private static Assembly GetAssembly(string dll)
        {
            Assembly ass = Assembly.GetEntryAssembly();
            if (dll.Trim() != "" && dll.Trim() != "Application")
            {
                string dllName = dll;
                if (!dllName.EndsWith(".dll", StringComparison.CurrentCultureIgnoreCase))
                {
                    dllName = dllName + ".dll";
                }

                string filePath = Application.StartupPath + '\\' + dllName;
                if (File.Exists(filePath))
                {
                    Assembly assembly = Assembly.LoadFile(filePath);
                    return assembly;
                }
            }
            return ass;
        }

        private static Form CreateForm(Type type)
        {
            if (type == null) return null;
            ConstructorInfo ci = type.GetConstructor(System.Type.EmptyTypes);
            if (ci == null)
            {
                ci = type.GetConstructor(Type.EmptyTypes);
                if (ci == null) return null;
                return (Form)ci.Invoke(new object[0]);
            }
            else
            {
                return (Form)ci.Invoke(new object[] { });
            }
        }

    }

}
