using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Reflection;
using System.Windows.Forms;

namespace Mesnac.Gui.Edit.Common
{
    /// <summary>
    /// 删除气力输送带有流动方向的控件
    /// </summary>
    public class DelIntakeControl
    {
        public static bool DelControl(Host.HostSurfaceManager _hostSurfaceManager, int comm)
        {
            try
            {
                List<string> lstContorl = new List<string>();
                List<Component> lst = new List<Component>();
                IDesignerHost host = _hostSurfaceManager.ActiveDesignSurface.GetService(typeof(IDesignerHost)) as IDesignerHost;
                if (host != null)
                {
                    foreach (Component cmp in host.Container.Components)
                    {
                        if (cmp.GetType().BaseType.FullName == "Mesnac.Controls.Intake.IntakeControl")
                        {
                            lst.Add(cmp);
                        }
                    }
                }
                #region 删除控件源
                System.ComponentModel.Design.ISelectionService ss = host.GetService(typeof(System.ComponentModel.Design.ISelectionService)) as System.ComponentModel.Design.ISelectionService;
                if (ss != null)
                {
                    System.Collections.ICollection ic = ss.GetSelectedComponents();
                    object[] selectedcomp = new object[ic.Count];
                    ic.CopyTo(selectedcomp, 0);
                    if (selectedcomp.Length == 1)
                    {
                        if (selectedcomp[0].GetType().BaseType.FullName != "Mesnac.Controls.Intake.IntakeControl")
                        {
                            return true;
                        }
                        Control intakeControl = selectedcomp[0] as Control;
                        foreach (Component cmp in lst)
                        {
                            PropertyInfo[] pi = cmp.GetType().GetProperties();
                            for (int i = 0; i < pi.Length; i++)
                            {
                                if (pi[i].GetCustomAttributesData().Count > 0)
                                {
                                    if (pi[i].GetCustomAttributesData()[0].ConstructorArguments.Count > 0)
                                    {
                                        if (pi[i].GetCustomAttributesData()[0].ConstructorArguments[0].ToString() == "typeof(Mesnac.Controls.Intake.Base.LinkControlEditor)")
                                        {
                                            if (pi[i].GetValue(cmp, null) != null)
                                                lstContorl.Add(pi[i].GetValue(cmp, null) == null ? "" : pi[i].GetValue(cmp, null).ToString());
                                        }
                                    }
                                }
                            }
                        }
                        foreach (string item in lstContorl)
                        {
                            if (item.IndexOf(intakeControl.Name) >= 0 && comm == 17)
                            {
                                DialogResult dlg = MessageBox.Show("系统检测到该控件有被动连接,删除该控件,其被动连接关系将一同删除,确认?", "提示", MessageBoxButtons.OKCancel);
                                if (dlg == DialogResult.OK)
                                {
                                    foreach (Component cmp in lst)
                                    {
                                        PropertyInfo[] pi = cmp.GetType().GetProperties();
                                        for (int i = 0; i < pi.Length; i++)
                                        {
                                            if (pi[i].GetCustomAttributesData().Count > 0)
                                            {
                                                if (pi[i].GetCustomAttributesData()[0].ConstructorArguments.Count > 0)
                                                {
                                                    if (pi[i].GetCustomAttributesData()[0].ConstructorArguments[0].ToString() == "typeof(Mesnac.Controls.Intake.Base.LinkControlEditor)")
                                                    {
                                                        if ((pi[i].GetValue(cmp, null) == null ? "" : pi[i].GetValue(cmp, null).ToString()) == item)
                                                            pi[i].SetValue(cmp, "", null);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    return true;
                                }
                                else
                                {
                                    return false;
                                }
                            }
                        }
                    }
                }
                #endregion
                return true;
            }
            catch (Exception)
            {
                return true;
            }
        }
    }
}