using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;

using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;
using System.Threading;
using US.Browser.IE.BLL;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
namespace US.Browser.IE
{
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Popup : skinform
    {
        WebBrowser _UWB; USMain _UM; RespondWeb _UPW;
        // string URL = "http://ie.1473.cn/index.htm";
        string URL = "http://seo.1473.cn/Client/Popup.html";
        //string URL = "http://zx.1473.cn";
        System.Windows.Forms.Timer _UTM;
        int[] _UST = new int[] { 10, 15, 20 };
        int _UTI, _UTT = 120000; bool _UTF = true;

        /// <summary>
        /// 设置聚焦
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == BLL.WindowHWND.WM_MOUSEACTIVATE)//鼠标事件
            {
                m.Result = new IntPtr(BLL.WindowHWND.MA_NOACTIVATE);
                _UTF = false; return;
            }
            else if (m.Msg == BLL.WindowHWND.WM_NCACTIVATE) //聚焦
            {
                if (((int)m.WParam & 0xFFFF) != BLL.WindowHWND.WA_INACTIVE)
                {
                    if (_UTF) { BLL.WindowHWND.SetActiveWindow(m.LParam); } _UTF = true;
                }
            }
            base.WndProc(ref m);
        }

        #region 初始化

        /// <summary>
        /// 初始化小窗口
        /// </summary>
        /// <param name="um"></param>
        public Popup(USMain UM)
        {
            InitializeComponent(); this._UM = UM;
            Control.CheckForIllegalCrossThreadCalls = false;
            _UPW = new RespondWeb(null, _UM._USY); SetP(true); TSP();
        }

        /// <summary>
        /// 初始化页面
        /// </summary>
        private void SetP(bool UTF)
        {
            int _UW = Screen.PrimaryScreen.WorkingArea.Width, _UH = Screen.PrimaryScreen.WorkingArea.Height;
            Point _UP = new Point((_UW - this.Width) / 2, (_UH - this.Height) / 2);
            this.PointToScreen(_UP); this.Location = _UP; if (UTF) { NWB(); this.Visible = false; }
        }

        #region 创建窗体

        /// <summary>
        /// 新建窗体
        /// </summary>
        /// <returns></returns>
        WebBrowser NWB()
        {
            _UWB = new WebBrowser();
            _UWB.ScriptErrorsSuppressed = _UWB.AllowNavigation = true;
            _UWB.ObjectForScripting = this; _UWB.ScrollBarsEnabled = false;
            _UWB.Dock = DockStyle.Fill; _UWB.Url = new Uri(URL);
            SHDocVw.WebBrowser _UW = (SHDocVw.WebBrowser)_UWB.ActiveXInstance;
            _UW.NavigateError += new SHDocVw.DWebBrowserEvents2_NavigateErrorEventHandler(_UW_NavigateError);
            _UW.NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(_UW_NewWindow3);
            this.Controls.Add(_UWB);
            return _UWB;
        }

        /// <summary>
        /// 连接发生错误
        /// </summary>
        /// <param name="pDisp"></param>
        /// <param name="URL"></param>
        /// <param name="Frame"></param>
        /// <param name="StatusCode"></param>
        /// <param name="Cancel"></param>
        void _UW_NavigateError(object pDisp, ref object URL, ref object Frame, ref object StatusCode, ref bool Cancel)
        {
            this.CloseX.Show();
        }

        /// <summary>
        /// 新建窗口处理
        /// </summary>
        /// <param name="ppDisp"></param>
        /// <param name="Cancel"></param>
        /// <param name="dwFlags"></param>
        /// <param name="bstrUrlContext"></param>
        /// <param name="bstrUrl"></param>
        void _UW_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)
        {
            Cancel = true; _UM._USY._UWB.Document.InvokeScript("_addTag", new object[] { bstrUrl });
        }

        #endregion

        #region 功能区域

        /// <summary>
        /// 广告时间查看
        /// </summary>
        void TSP()
        {
            if (StaticClass.UCON != "Teacher") //使用者机器为教师机器不弹出
            {
                _UTM = new System.Windows.Forms.Timer();
                _UTM.Tick += new EventHandler(_UTM_Tick);
                _UTM.Interval = _UTT; _UTM.Start();
            }
        }

        /// <summary>
        /// 时间变化使用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void _UTM_Tick(object sender, EventArgs e)
        {
            int _UDT = DateTime.Now.Hour;
            if (this._UTI == 0 || (_UDT != this._UTI && Array.IndexOf<int>(_UST, _UDT) != -1) && BLL.USIE.isnet()) { this._UTI = _UDT; if (!this.Visible) { ZJTC(false); } }
        }


        /// <summary>
        /// 弹出框
        /// </summary>
        public void ZJTC(bool UTF)
        {
            if (BLL.USIE.isnet())
            {
                if (_UTM != null) { _UTM.Stop(); _UTM.Dispose(); _UTM = null; }
                if (this.Visible == false) { this._UWB.Document.InvokeScript("init", new object[] { UTF }); }
                showf(UTF);
            }
        }

        /// <summary>
        /// 弹出
        /// </summary>
        /// <param name="UTF"></param>
        public void showf(bool UTF)
        {
            if (!UTF) { this.SendToBack(); } else { this.BringToFront(); } SetP(false); this.Visible = true;
        }

        /// <summary>
        /// 关闭
        /// </summary>
        void CloseX_Click(object sender, EventArgs e)
        {
            fgClose();
        }

        /// <summary>
        /// 统一关闭
        /// </summary>
        public void fgClose()
        {
            this.Hide(); _UTT = 1200000; TSP();
        }

        //窗体拖动
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();//API函数
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwdn, int wMsg, int mParam, int lParam);//接受发送来的消息 并处理
        public const int WM_SYSCOMMAND = 0x0112;//该变量表示将向Windows发送消息的类型
        public const int SC_MOVE = 0xF010;//该变量表示发送消息的附加消息
        public const int HTCAPTION = 0x0002;

        /// <summary>
        /// 拖动
        /// </summary>
        public void _P_move()
        {
            ReleaseCapture();//该函数从当前线程中释放鼠标捕获事件，并恢复鼠标输入处理
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        /// <summary>
        /// 获取网页字符中指定标签的值
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="title">标签</param>
        /// <returns>值</returns>
        private static string GetTitleContent(string str, string title)//获取<title>之间内容  
        {
            return Regex.Match(str, string.Format("<{0}[^>]*?>(?<Text>[^<]*)</{1}>", title, title), RegexOptions.IgnoreCase).Groups["Text"].Value;
        }

        /// <summary>
        /// 跳到1473查看
        /// </summary>
        /// <param name="_WZID"></param>
        public void _tpNewWindow(string UTP, string UDE)
        {
            _UPW.OpenLook(new object[] { UTP, UDE }, null);
        }

        #endregion


        #endregion


    }
}
