using NFine.Data.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

namespace NFine.Web
{
    /// <summary>
    /// ChulingHandler 的摘要说明
    /// </summary>
    public class ChulingHandler : IHttpHandler
    {
        /// <summary>
        /// 打印数据参数:服务器的URL+打印的文件名,转化为Base64编码
        /// </summary>
        protected string strPrintData;
        /// <summary>
        /// 标识是否安装了控件
        /// </summary>
        protected bool bIsInstallPrintControl = true;
        /// <summary>
        /// 打印控件的Cookie值
        /// </summary>
        protected string strPrintControlCookie = "";

        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request.Params["action"];
            switch (action)
            {
                case "GetUrlPath":
                    GetUrlPath(context);
                    break;

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private string GetUrlPath(HttpContext context)
        {
            string strUrl = context.Request.Url.ToString();
            int iEnd = strUrl.LastIndexOf("/");
            strUrl = strUrl.Substring(0, iEnd + 1);
            return strUrl;
        }

        /// <summary>
        /// 设置控件调用的Cookie值,判断是否安装了打印控件
        /// </summary>
        /// <param name="pJson"></param>
        private void SetCookieAndURL(HttpContext context, PrintJson pJson)
        {
            //bIsInstallPrintControl = false;
            //strPrintControlCookie = "";
            //HttpCookie pCookieInstall = context.Request.Cookies["InstallPrintControl"];
            //if (pCookieInstall != null)
            //{ //Cookie存在
            //    strPrintControlCookie = pCookieInstall.Value.ToString();

            //    //以Cookie值查找在数据表中是否存在
            //    string strSql = @"Select * From CheckInstall Where Cookie = @Cookie";
            //    SqlParameter[] pmcCookie = { new SqlParameter("Cookie", strPrintControlCookie) };
            //    using (SqlDataReader drCookie = SqlHelper.ExecuteReader(DbHelper.ConnectionString, CommandType.Text, strSql, pmcCookie))
            //    {
            //        if (drCookie.Read())
            //        { //标识为已经安装
            //            bIsInstallPrintControl = true;
            //        }
            //        drCookie.Close();
            //    }
            //    //更新Cookie的保存时间
            //    pCookieInstall.Expires = DateTime.Now.AddYears(10);
            //    context.Response.SetCookie(pCookieInstall);
            //}
            //else
            //{//Cookie不存在,则新建Cookie
            //    strPrintControlCookie = System.Guid.NewGuid().ToString();
            //    pCookieInstall = new HttpCookie("InstallPrintControl", strPrintControlCookie);
            //    pCookieInstall.Expires = DateTime.Now.AddYears(10);
            //    context.Response.Cookies.Add(pCookieInstall);
            //}

            //string strUrl = GetUrlPath(context) + "PrintCookie.aspx";

            //pJson.SetCookieAndURL(strPrintControlCookie, strUrl);
        }


    }
}