using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;

namespace US.Browser.IE
{
    [ComVisible(true)]
    public partial class News : Form
    {

        WebBrowser _UWB; RespondWeb _UPW;

        bool _UTF = true;

        #region

        /// <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);
        }

        #endregion

        /// <summary>
        /// 初始化窗体
        /// </summary>
        public News(BrowserDAO UBE)
        {
            _UPW = new RespondWeb(null, UBE); InitializeComponent(); InitBrower();
        }

        /// <summary>
        /// 初始化窗体
        /// </summary>
        void InitBrower()
        {
            this.Ne.Url = new System.Uri(Application.StartupPath + "\\html\\News.htm"); this.Ne.IsWebBrowserContextMenuEnabled = false;
            this.Ne.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(Ne_DocumentCompleted);
            this.Ne.ObjectForScripting = this; Ne.ScriptErrorsSuppressed = true;
        }

        /// <summary>
        /// 初始化成功
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Ne_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (this.Ne.ReadyState == WebBrowserReadyState.Complete && e.Url == this.Ne.Url) { string _id = GetId(); USMain.id = (_id == "" || _id == null) ? "11111111-1111-1111-1111-111111111111" : _id; }
        }

        /// <summary>
        /// 这里是传参交互
        /// </summary>
        /// <param name="Json"></param>
        public void TFP(string Json, WebBrowser UWB, RespondWeb UPW)
        {
            if ((bool)this.Ne.Document.InvokeScript("Init", new object[] { Json }))
            {
                if (StaticClass.UCON != "Teacher") { init(false); }
                _UWB = UWB; _UPW = UPW;
            }
        }

        /// <summary>
        /// 消息消失
        /// </summary>
        public void remove()
        {
            animate(true, 0, this);
        }

        /// <summary>
        /// 显示窗体出现
        /// </summary>
        /// <param name="UJE"></param>
        public void init(bool UTF)
        {
            if (this.Visible == false)
            {
                Point _UPE = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width - 20, Screen.PrimaryScreen.WorkingArea.Height - this.Height - 1);
                this.PointToScreen(_UPE); this.Location = _UPE; this.Opacity = 0; this.Visible = true; if (!UTF) { this.SendToBack(); } else { this.BringToFront(); } animate(false, 1, this);
            }
            else if (UTF) { this.BringToFront(); }
        }

        /// <summary>
        /// 动画函数
        /// </summary>
        /// <param name="tf"></param>
        /// <param name="c"></param>
        /// <param name="uf"></param>
        public static void animate(bool tf, int c, Form uf)
        {
            int i = 0; System.Windows.Forms.Timer _tt = new System.Windows.Forms.Timer(); double k = c == 0 ? -0.2 : 0.2;
            _tt.Tick += new EventHandler(delegate(object sender, EventArgs g)
            {
                uf.Opacity += k;
                if (i++ == 10) { _tt.Stop(); _tt.Dispose(); _tt = null; if (tf) { uf.Hide(); } };
            });
            _tt.Interval = 100; _tt.Start();
        }

        /// <summary>
        /// 查看html内容
        /// </summary>
        /// <param name="tf"></param>
        /// <param name="ue"></param>
        public void Look(string tf, string ue)
        {
            _UPW.OpenLook(new object[] { tf, ue }, null);
        }

        /// <summary>
        /// 获取ID
        /// </summary>
        /// <returns></returns>
        public string GetId()
        {
            return (string)this.Ne.Document.InvokeScript("GetID");
        }

        /// <summary>
        /// 设置最新Id
        /// </summary>
        public void SetId(string id)
        {
            USMain.id = id;
        }

        /// <summary>
        /// 拖动函数
        /// </summary>
        public void Drag()
        {
            BLL.WindowHWND._Main_Drag(this);
        }
    }
}
