using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Net;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Reflection;
using US.Browser.IE.BLL;
namespace US.Browser.IE
{
    /// <summary>
    /// 收藏夹操作区域
    /// </summary>
    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public class FVCBrower
    {
        public BrowserDAO _UBF;
        public static WebBrowser UAFB;//添加的收藏框
        public static WebBrowser UPFB;//显示的收藏夹框
        private string _UID = string.Empty;//用户当前登录账户ID

        #region 打开收藏夹

        /// <summary>
        /// 无参数构造打开收藏夹
        /// </summary>
        public FVCBrower(BrowserDAO _UFB)
        {
            Control.CheckForIllegalCrossThreadCalls = false; Form.CheckForIllegalCrossThreadCalls = false; _UBF = _UFB;
            UAFB = CreBrower("AddFVBrower", "\\html\\FavC.htm", new Size(342, 402));//添加的收藏夹
            UPFB = CreBrower("OutFVBrower", "\\html\\FavP.htm", new Size(342, 402));//显示的收藏夹
            UAFB.Document.LosingFocus += new HtmlElementEventHandler(Document_LosingFocusO);
            UPFB.Document.LosingFocus += new HtmlElementEventHandler(Document_LosingFocus);
            UAFB.GotFocus += new EventHandler(UPFB_GotFocus);
            UPFB.GotFocus += new EventHandler(UPFB_GotFocus);
        }

        /// <summary>
        /// 查看收藏夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Document_LosingFocus(object sender, HtmlElementEventArgs e)
        {
            if (e.ToElement == null) { UPFB.Hide(); }
        }

        /// <summary>
        /// 添加收藏隐藏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Document_LosingFocusO(object sender, HtmlElementEventArgs e)
        {
            if (e.ToElement == null) { UAFB.Hide(); }
        }

        /// <summary>
        /// 焦点获取使用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void UPFB_GotFocus(object sender, EventArgs e) { }

        /// <summary>
        /// 创建收藏夹
        /// </summary>
        /// <param name="UN"></param>
        /// <param name="UURL"></param>
        /// <param name="UXY"></param>
        /// <returns></returns>
        public WebBrowser CreBrower(string UN, string UURL, Size UXY)
        {
            WebBrowser _UWB = new WebBrowser();
            _UWB.ScriptErrorsSuppressed = _UWB.Visible = _UWB.ScrollBarsEnabled = false; _UWB.ObjectForScripting = this;
            _UWB.Size = UXY; _UWB.Name = UN; _UWB.Url = new System.Uri(Application.StartupPath + UURL); _UBF._UF.Controls.Add(_UWB); return _UWB;
        }

        /// <summary>
        /// 打开收藏夹
        /// </summary>
        public void OpenFV(WebBrowser UWB)
        {
            if (UPFB.Visible == false)
            {
                if (SelCookies(UWB) != "") //判断用户是否登录1473
                {
                    ChangeBrower(UPFB, new Point(_UBF._UF.Width - 355, _UBF._UWB.Height), new Size(352, _UBF._UF.Height - _UBF._UWB.Height), true);//修改收藏夹
                    UPFB.Document.InvokeScript("Loadhtml", new Object[] { "UF_PX", _UID }); UPFB.Visible = true; UPFB.Focus(); UPFB.Document.Focus();
                }
            }
            else { UPFB.Visible = false; }

        }

        /// <summary>
        /// 打开添加收藏夹窗体
        /// </summary>
        /// <param name="brw"></param>
        public void OpenAddFVC(object UWB)
        {
            if (UAFB.Visible == false && UWB != "")//显示收藏夹
            {
                WebBrowser _UBW = (WebBrowser)UWB;
                if (SelCookies(_UBW) != "")
                {
                    ChangeBrower(UAFB, new Point((int)((_UBF._UF.Width - 522)), _UBF._UWB.Height + 2), new Size(344, 404), true);
                    UAFB.Visible = true; UAFB.Document.InvokeScript("Loadhtml", new Object[] { "CFV_C", _UID });
                    UAFB.Document.InvokeScript("CUAT", new Object[] { _UBW.Url.ToString(), _UBW.DocumentTitle }); UAFB.Focus(); UAFB.Document.Focus();
                }
            }
            else { UAFB.Visible = false; }

        }

        /// <summary>
        /// 点击打开其中一个收藏夹 
        /// </summary>
        /// <param name="UURL"></param>
        public void OpenURL(string UURL)
        {
            _UBF._UWB.Document.InvokeScript("_addTag", new Object[] { UURL });
            VIS(UPFB);
        }

        /// <summary>
        /// 隐藏显示收藏夹或收藏显示框
        /// </summary>
        public bool VIS(WebBrowser _WB)
        {
            return _WB.Visible = _WB.Visible == true ? false : true;
        }

        #endregion

        #region 辅助函数

        /// <summary>
        /// 设置WebBrowser大小和位置
        /// </summary>
        /// <param name="UWB"></param>
        /// <param name="UXY">位置</param>
        /// <param name="UWH">长宽</param>
        public static void ChangeBrower(WebBrowser UWB, Point? UXY, Size? UWH, bool? TF)
        {
            if (UXY != null) { Point _UX = UWB.PointToScreen(UXY.Value); UWB.Location = UXY.Value; }//设置位置
            if (UWH != null) { UWB.Size = UWH.Value; }//设置长宽
            if (TF == true) { UWB.BringToFront(); }//最顶层
        }

        #region 获取cookie

        /// <summary>
        /// 获取1473的cookie
        /// </summary>
        /// <returns></returns>
        public string SelCookies(WebBrowser UWB)
        {
            string _UCO = string.Empty; UWB = UWB.Url.ToString().Contains("http://www.1473.cn") ? UWB : _UBF._UVB.GetWebBrowser("http://www.1473.cn");
            _UCO = ACookies(UWB); if (_UCO == "") { _UBF._UVB.OpenLook(new object[] { "login" }, UWB); } _UID = _UCO; return _UCO;
        }


        /// <summary>
        /// 获取对应的cookie
        /// </summary>
        /// <param name="UWB"></param>
        /// <returns></returns>
        public static string ACookies(WebBrowser UWB)
        {
            try
            {
                int i, j; string _UCOK = UWB.Document.Cookie, _UAST = "";
                string[] _UACO = _UCOK.Replace(" ", "").Split(';'), _UAS;
                for (i = 0; i < _UACO.Length; i++)
                {
                    _UAS = _UACO[i].Split('=');
                    if (_UAS[0] == "usestudiosso")
                    {
                        for (j = 1; j < _UAS.Length; j++)
                        {
                            _UAST += _UAS[j] + "=";
                        }
                    }
                }
                return GGUID(_UAST, "userid");
            }
            catch (Exception) { return ""; }
        }


        /// <summary>
        /// 根据cookie获取指定的值
        /// </summary>
        /// <param name="UCK"></param>
        /// <param name="UN"></param>
        /// <returns></returns>
        public static string GGUID(string UCK, string UN)
        {
            int i; string _UST = ""; string[] _UACO = UCK.Split('&'), _UAST;
            for (i = 0; i < _UACO.Length; i++)
            {
                _UAST = _UACO[i].Split('=');
                if (_UAST[0] == UN)
                {
                    _UST = _UAST[1];
                }
                else break;
            }
            return _UST;
        }

        #endregion

        #endregion

        #region 收藏夹到数据库

        /// <summary>
        /// 创建收藏文件夹
        /// </summary>
        /// <param name="UID"></param>
        /// <param name="CV"></param>
        /// <returns></returns>
        public string FVCadd(string UID, string CV, string UCB)
        {
            USClientCommunicate.PostPage("http://collect.1473.cn", "POST", delegate(string ust)
           {
               _UBF._UF.Invoke(new MethodInvoker(delegate()
               {
                   UAFB.Document.InvokeScript(UCB, new object[] { ust });
               }));
           }, "mode=", "UseBE_AddCFV", "{ UID:'" + UID + "',CateName:'" + CV + "'}");
            return "";
        }

        /// <summary>
        /// 收藏网页
        /// </summary>
        /// <param name="FVC"></param>
        /// <param name="FVH"></param>
        /// <param name="CID"></param>
        /// <param name="UID"></param>
        /// <returns></returns>
        public void AddFV(string FVC, string FVH, string CID, string UID)
        {
            USClientCommunicate.PostPage("http://collect.1473.cn", "POST", null, "mode=", "UseBE_AddFV", "{ UID:'" + UID + "',CateID:'" + CID + "',href:'" + FVH + "',herfc:'" + FVC + "'}");
        }

        /// <summary>
        /// 删除收藏文件夹
        /// </summary>
        /// <param name="CID"></param>
        /// <returns></returns>
        public string DelFVC(string CID, string UCB)
        {
            return USClientCommunicate.PostPage("http://collect.1473.cn", "POST", delegate(string ust)
            {
                _UBF._UF.Invoke(new MethodInvoker(delegate()
                {
                    UAFB.Document.InvokeScript(UCB, new object[] { ust });
                }));
            }, "mode=", "UseBE_DelCFV", "{ CID:'" + CID + "'}");
        }

        /// <summary>
        /// 删除收藏中的链接
        /// </summary>
        /// <param name="GID"></param>
        /// <returns></returns>
        public void DelFV(string GID)
        {
            USClientCommunicate.PostPage("http://collect.1473.cn", "POST", null, "mode=", "UseBE_DelFV", "{ GID:'" + GID + "'}");
        }

        /// <summary>
        /// 获取收藏夹文件夹数据
        /// </summary>
        /// <param name="UID"></param>
        /// <returns></returns>
        public string FVCSel(string UID, string id, string cb)
        {
            if (UID != "")
            {
                USClientCommunicate.PostPage("http://collect.1473.cn", "POST", delegate(string a)
                {
                    _UBF._UF.Invoke(new MethodInvoker(delegate()
                    {
                        (id == "CFV_C" ? UAFB : UPFB).Document.InvokeScript(cb, new object[] { a });
                    }));
                }, "mode=", "UseBE_SELC", "{ UID:'" + UID + "'}");
            }
            return "";
        }

        /// <summary>
        /// 获取指定文件夹中的收藏
        /// </summary>
        /// <param name="CID"></param>
        /// <returns></returns>
        public void FVSel(string CID, string UCB)
        {
            USClientCommunicate.PostPage("http://collect.1473.cn", "POST", delegate(string ust)
            {
                _UBF._UF.Invoke(new MethodInvoker(delegate()
            {
                UPFB.Document.InvokeScript(UCB, new object[] { ust });
            }));
            }, "mode=", "UseBE_SELFV", "{CID:'" + CID + "'}");
        }

        //窗体拖动
        [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 _Main_Drag()
        {
            ReleaseCapture();//该函数从当前线程中释放鼠标捕获事件，并恢复鼠标输入处理
            SendMessage(UAFB.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        #endregion
    }

}
