using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using TopWinCMS.BLL.PageFactory;

namespace TopWinCMS.WebUI.Admin.ContentRelease
{

    public class AjaxPostBack : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.HeaderEncoding = System.Text.Encoding.UTF8;


            List<string> UrlList = new List<string>();
            string Action = context.Request["Action"];

            int ModelID=-1;
            if (context.Request["ModelID"] != null)
                ModelID = int.Parse(context.Request["ModelID"]);

            MakingUrlList MUL = new MakingUrlList();
            switch (Action)
            {
                case "ContentPage":
                    #region
                    if (context.Request["Top"] != null)
                    {
                        UrlList = MUL.MakeContentPage(int.Parse(context.Request["Top"]), ModelID);
                    }
                    else if (context.Request["MinID"] != null)
                    {
                        string strWhere = string.Format(" ID>={0} and ID<={1} ", context.Request["MinID"], context.Request["MaxID"]);
                        UrlList = MUL.MakeContentPage(strWhere, ModelID);
                    }
                    else
                    {
                        string strWhere = string.Format(" UpdateTime between #{0}# and #{1}# ", context.Request["MinDate"], context.Request["MaxDate"]);
                        UrlList = MUL.MakeContentPage(strWhere, ModelID);
                    }
                    #endregion
                    break;

                case "ClassPage":
                    if (context.Request["Where"] != null)
                    {
                        UrlList = MUL.MakeClassPage(context.Request["Where"], ModelID);
                    }
                    break;

                case "SpecialPage":
                    if (context.Request["Where"] != null)
                    {
                        UrlList = MUL.MakeSpecialPage(context.Request["Where"], ModelID);
                    }
                    break;
                case "ChannelIndexPage":
                    bool ALL = Convert.ToBoolean(context.Request["ALL"]);
                    UrlList = MUL.ChannelIndexPage(ModelID, ALL);
                    break;
                case "IndexPage":
                    UrlList.Add(MUL.IndexPage());
                    break;
                case "ALL":
                    UrlList.AddRange(MUL.MakeContentPage(int.MaxValue, ModelID));
                    UrlList.AddRange(MUL.MakeClassPage(" ModelID=" + ModelID, ModelID));
                    UrlList.AddRange(MUL.MakeClassPage(" ModelID=" + ModelID, ModelID));
                    UrlList.AddRange(MUL.ChannelIndexPage(ModelID, true));
                    UrlList.Add(MUL.IndexPage());
                    break;

                default:
                    throw new Exception("参数错误");

            }


            string temp = string.Join("|", UrlList.ToArray());
            temp = temp.Replace("\\","/");

            context.Response.Write(temp);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
