using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.SessionState;

namespace CustomHandler
{
    public class CSSHandler : System.Web.IHttpHandler, IRequiresSessionState
    {

        public bool IsReusable
        {
            get { return true; }
        }
        public void ProcessRequest(System.Web.HttpContext context)
        {
            string File = context.Request.PhysicalPath;
            string Url = context.Request.RawUrl;
            HttpRequest req = context.Request;
            bool isnew = string.IsNullOrEmpty(context.Request.Headers["If-Modified-Since"]);
            DateTime cachedate = isnew ? DateTime.MinValue : DateTime.Parse(context.Request.Headers["If-Modified-Since"]).ToUniversalTime();
            string strlan = req.Headers["Accept-Language"];
            string[] lans = strlan.Split(',');
            string firstlan = lans[0];
            int ipos = firstlan.IndexOf(';');
            if (ipos != -1)
            {
                firstlan = firstlan.Substring(0, ipos).Trim();

            }
            string filename;
            if (firstlan == "zh-CN" || firstlan == "zh")
            {

                filename = File;

            }
            else
            {
                filename = File.Replace("App_Themes", "App_Themes_Other");
            }
            DateTime Lastmodified = new FileInfo(filename).LastWriteTimeUtc;
            if (Lastmodified <= cachedate)
            {
                context.Response.StatusCode = 304;
            }
            else
            {
                context.Response.ContentType = "text/css";
                context.Response.AddHeader("Last-Modified", Lastmodified.ToString("r"));
                context.Response.TransmitFile(filename);
            }
        }

    }

}
