using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;

namespace ExtendedWebBrowser2
{
    /// <summary>
    /// 
    /// </summary>
    public class Command
    {
        private ExtendedWebBrowser _Browser;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="host"></param>
        public Command(ExtendedWebBrowser host)
        {
            _Browser = host;
        }
        [DllImport("User32.DLL")]
        private static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
        [DllImport("User32.DLL")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent,
            IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        private readonly int IDM_VIEWSOURCE = 2139;
        private readonly uint WM_COMMAND = 0x0111;
        /// <summary>
        /// 查看源码
        /// </summary>
        public void ViewSource()
        {
            IntPtr vHandle = _Browser.Handle;
            vHandle = FindWindowEx(vHandle, IntPtr.Zero, "Shell Embedding", null);
            vHandle = FindWindowEx(vHandle, IntPtr.Zero, "Shell DocObject View", null);
            vHandle = FindWindowEx(vHandle, IntPtr.Zero, "Internet Explorer_Server", null);
            SendMessage(vHandle, WM_COMMAND, IDM_VIEWSOURCE, (int)_Browser.Handle);
        }
        private void ExecCommand(string command)
        {
            _Browser.Document.ExecCommand(command, false, null);
        }
        /// <summary>
        /// 复制当前选中区
        /// </summary>
        public void Copy()
        {
            ExecCommand("Copy");
        }
        /// <summary>
        /// 粘贴当前选中区
        /// </summary>
        public void Paste()
        {
            ExecCommand("Paste");
        }
        /// <summary>
        /// 剪切当前选中区
        /// </summary>
        public void Cut()
        {
            ExecCommand("Cut");
        }
        /// <summary>
        /// 全选
        /// </summary>
        public void SelectAll()
        {
            ExecCommand("SelectAll");
        }
        /// <summary>
        /// 清除当前选中区的选中状态。
        /// </summary>
        public void UnSelect()
        {
            ExecCommand("Unselect");
        }
        /// <summary>
        /// 从当前选中区中删除全部超级链接。
        /// </summary>
        public void UnLink()
        {
            ExecCommand("Unlink");
        }
        /// <summary>
        /// 从当前选中区中删除全部书签。
        /// </summary>
        public void UnBookmark()
        {
            ExecCommand("UnBookmark");
        }
        /// <summary>
        /// 删除当前选中区
        /// </summary>
        public void Delete()
        {
            ExecCommand("Delete");
        }

        /// <summary>
        /// 重做
        /// </summary>
        public void Redo()
        {
            ExecCommand("Redo");
        }
        /// <summary>
        /// 撤销
        /// </summary>
        public void Undo()
        {
            ExecCommand("Undo");
        }
        /// <summary>
        /// 从当前选中区中删除格式化标签。
        /// </summary>
        public void RemoveFormat()
        {
            ExecCommand("RemoveFormat");
        }
        /// <summary>
        /// 点击节点
        /// </summary>
        /// <param name="tagName"></param>
        /// <param name="attr"></param>
        /// <param name="attrValue"></param>
        public void ClickElement(string tagName, string attr, string attrValue)
        {
            HtmlElementCollection list = _Browser.Document.GetElementsByTagName(tagName);
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].GetAttribute(attr).ToLower() == attrValue.ToLower())
                {
                    list[i].InvokeMember("Click");
                    break;
                }
            }
        }
        /// <summary>
        /// 点击节点
        /// </summary>
        /// <param name="tagName"></param>
        /// <param name="attr"></param>
        /// <param name="attrValue"></param>
        /// <param name="waitMilliSecond"></param>
        public void ClickElement(string tagName, string attr, string attrValue, int waitMilliSecond)
        {
            HtmlElementCollection list = _Browser.Document.GetElementsByTagName(tagName);
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].GetAttribute(attr).ToLower() == attrValue.ToLower())
                {
                    list[i].InvokeMember("Click");
                    Wait(waitMilliSecond);
                    break;
                }
            }
        }
        /// <summary>
        /// 点击元素
        /// </summary>
        /// <param name="tagName">节点名</param>
        /// <param name="attr">属性</param>
        /// <param name="attrValue">属性值</param>
        /// <param name="waitMilliSecond">等待毫秒数</param>
        /// <param name="index">第几个符合的元素,从1开始</param>
        public void ClickElement(string tagName, string attr, string attrValue, int waitMilliSecond,int index)
        {
            HtmlElementCollection list = _Browser.Document.GetElementsByTagName(tagName);
            int _index = 0;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].GetAttribute(attr).ToLower() == attrValue.ToLower())
                {
                    if (_index == index)
                    {
                        // list[i].InvokeMember("Click");
                        list[i].InvokeMember("Click");
                        Wait(waitMilliSecond);
                        break;
                    }
                    _index++;
                }
            }
        }
        /// <summary>
        /// 等待多时毫秒
        /// </summary>
        /// <param name="Milliseconds"></param>
        public void Wait(int Milliseconds)
        {
            if (Milliseconds <= 0) { return; }
            DateTime startTime = DateTime.Now;
            while (true)
            {
                if (DateTime.Now >= startTime.AddMilliseconds(Milliseconds) && Milliseconds > 0)
                {
                    break;
                }
                System.Windows.Forms.Application.DoEvents();
            }
        }
    }
}
