using CommonLang.Property;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace CommonFroms
{
    public static class FormUtils
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern void SwitchToThisWindow(IntPtr hWnd, bool turnOn);

        /// <summary>
        /// 切换到当前窗口
        /// </summary>
        /// <param name="form"></param>
        /// <param name="turnOn"></param>
        static public void SwithToThisForm(Form form, bool turnOn)
        {
            SwitchToThisWindow(form.Handle, turnOn);
        }

        /// <summary>
        /// 判断ListView里面的所有Tag是否和list一致,通常用于数据层刷新
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="view"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public static bool ListViewItemTagEquals<T>(ListView view, ICollection<T> list) where T : class
        {
            if (view.Items.Count != list.Count)
            {
                return false;
            }
            foreach (ListViewItem item in view.Items)
            {
                T it = item.Tag as T;
                if (!list.Contains(it))
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 判断ListView里面的所有Tag是否和list一致,通常用于数据层刷新
        /// </summary>
        /// <param name="view"></param>
        /// <param name="list"></param>
        /// <param name="compare">参数1表示list中的单位,参数2表示ListView里的Tag</param>
        /// <returns></returns>
        public static bool ListViewItemTagEquals(ListView view, IList list, IComparer compare)
        {
            if (view.Items.Count != list.Count)
            {
                return false;
            }
            foreach (ListViewItem item in view.Items)
            {
                bool finded = false;
                foreach (object tag in list)
                {
                    if (compare.Compare(tag, item.Tag) == 0)
                    {
                        finded = true;
                        break;
                    }
                }
                if (!finded)
                {
                    return false;
                }
            }
            return true;
        }

        public static void GetAllTreeNodes(TreeNodeCollection nodes, List<TreeNode> allnodes)
        {
            foreach (TreeNode node in nodes)
            {
                allnodes.Add(node);
                GetAllTreeNodes(node.Nodes, allnodes);
            }
        }

        public static TreeNode FindTreeNodeByText(TreeNodeCollection nodes, string text, TreeNode start = null, bool include = false)
        {
            List<TreeNode> allnodes = new List<TreeNode>();
            GetAllTreeNodes(nodes, allnodes);
            int begin = allnodes.IndexOf(start);
            if (begin < 0)
            {
                begin = 0;
            }
            else if (!include)
            {
                begin += 1;
            }
            for (int i = begin; i < allnodes.Count; i++)
            {
                TreeNode tn = allnodes[i];
                if (tn.Text.Contains(text))
                {
                    return tn;
                }
            }
            return null;
        }
        public static TreeNode FindLastTreeNodeByText(TreeNodeCollection nodes, string text, TreeNode start = null, bool include = false)
        {
            List<TreeNode> allnodes = new List<TreeNode>();
            GetAllTreeNodes(nodes, allnodes);
            int end = allnodes.IndexOf(start);
            if (end < 0)
            {
                end = allnodes.Count - 1;
            }
            else if (!include)
            {
                end -= 1;
            }
            for (int i = end; i >= 0; i--)
            {
                TreeNode tn = allnodes[i];
                if (tn.Text.Contains(text))
                {
                    return tn;
                }
            }
            return null;
        }

    }
    
}