using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PushPlatform.Common.Util
{
    public static class PageUtil
    {
        public static string PageUrl(int recordCount, int index, int pageSize)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (var key in System.Web.HttpContext.Current.Request.QueryString.AllKeys)
            {
                dic.Add(key.ToLower(), System.Web.HttpContext.Current.Request.QueryString[key]);
            }
            //先计算总页数
            int pageCount = (int)Math.Ceiling((double)recordCount / pageSize);
            if (index > pageCount)
            {
                index = pageCount;
            }
            else if (index < 1)
            {
                index = 1;
            }
            if (pageCount <= 1)
            {
                return "";
            }
            int startIndex = 1;
            if (pageCount > 9)
            {
                int count = 8;
                int start = index;
                int end = index;
                while (count > 0)
                {
                    var flag1 = false;
                    var flag2 = false;
                    if (start > 1)
                    {
                        flag1 = true;
                        start--;
                    }
                    if (end < pageCount)
                    {
                        flag2 = true;
                        end++;
                    }
                    if (flag1)
                    {
                        count--;
                    }
                    if (flag2)
                    {
                        count--;
                    }
                    if (!flag1 && !flag2)
                    {
                        count--;
                    }
                }
                startIndex = start;
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<!-- Pagination: Aligned center -->");
            sb.AppendLine("<div class=\"pagination pagination-large pagination-centered margin-none\">");
            sb.AppendLine("<ul class=\"pagination pagination-centered margin-none\">");
            int preIndex = index <= 1 ? 1 : index - 1;
            if (1 < index)
            {
                sb.AppendLine("<li><a href=\"" + GetPageUrl(preIndex, dic) + "\" myPageIndex=\"" + preIndex + "\" >上一页</a></li>");
            }
            else
            {
                sb.AppendLine("<li class=\"disabled\"><a href=\"#\">上一页</a></li>");
            }
            int endIndex = startIndex + 9;
            for (int i = startIndex; i <= pageCount && i < endIndex; i++)
            {
                string cls = "";
                if (i == index)
                {
                    cls = "class=\"active\"";
                }
                if (index <= 5 || i != startIndex)
                {
                    sb.AppendLine("<li " + cls + "><a href=\"" + GetPageUrl(i, dic) + " \"  myPageIndex=\"" + i + "\" >" + i + "</a></li>");
                }
            }
            int next = index < pageCount ? index + 1 : index;

            if (index < pageCount)
            {
                sb.AppendLine("<li><a href=\"" + GetPageUrl(next, dic) + "\"   myPageIndex=\"" + next + "\" >下一页</a></li>");
            }
            else
            {
                sb.AppendLine("<li class=\"disabled\"><a href=\"#\">下一页</a></li>");
            }
            sb.AppendLine("</ul>");
            sb.AppendLine("</div>");
            sb.AppendLine("<!-- // Pagination: Aligned center END -->");
            return sb.ToString();
        }

        public static string PageUrl_Web(int recordCount, int index, int pageSize)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (var key in System.Web.HttpContext.Current.Request.QueryString.AllKeys)
            {
                dic.Add(key.ToLower(), System.Web.HttpContext.Current.Request.QueryString[key]);
            }
            //先计算总页数
            int pageCount = (int)Math.Ceiling((double)recordCount / pageSize);
            if (index > pageCount)
            {
                index = pageCount;
            }
            else if (index < 1)
            {
                index = 1;
            }
            if (pageCount <= 1)
            {
                //return "";
            }
            int startIndex = 1;
            if (pageCount > 9)
            {
                int count = 8;
                int start = index;
                int end = index;
                while (count > 0)
                {
                    var flag1 = false;
                    var flag2 = false;
                    if (start > 1)
                    {
                        flag1 = true;
                        start--;
                    }
                    if (end < pageCount)
                    {
                        flag2 = true;
                        end++;
                    }
                    if (flag1)
                    {
                        count--;
                    }
                    if (flag2)
                    {
                        count--;
                    }
                    if (!flag1 && !flag2)
                    {
                        count--;
                    }
                }
                startIndex = start;
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<ul class=\"pagination pagination-split pull-right nm_pages\">");
            int preIndex = index <= 1 ? 1 : index - 1;
            int pre10Index = index <= 10 ? 1 : index - 10;
            if (1 < index)
            {
                if (10 < index)
                {
                    sb.AppendLine("<li><a href=\"" + GetPageUrl(pre10Index, dic) + "\" myPageIndex=\"" + pre10Index + "\" ><i class=\"fa fa-angle-left\"></i><i class=\"fa fa-angle-left\"></i></a></li>");
                }
                else
                {
                    sb.AppendLine("<li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-left\"></i><i class=\"fa fa-angle-left\"></i></a></li>");
                }

                sb.AppendLine("<li><a href=\"" + GetPageUrl(preIndex, dic) + "\" myPageIndex=\"" + preIndex + "\" ><i class=\"fa fa-angle-left\"></i></a></li>");
            }
            else
            {
                sb.AppendLine("<li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-left\"></i><i class=\"fa fa-angle-left\"></i></a></li><li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-left\"></i></a></li>");
            }
            int endIndex = startIndex + 9;
            for (int i = startIndex; i <= pageCount && i < endIndex; i++)
            {
                string cls = "";
                if (i == index)
                {
                    cls = "class=\"active\"";
                }
                if (index <= 5 || i != startIndex)
                {
                    sb.AppendLine("<li " + cls + "><a href=\"" + GetPageUrl(i, dic) + " \"  myPageIndex=\"" + i + "\" >" + i + "</a></li>");
                }
            }
            int next = index < pageCount ? index + 1 : index;
            int next10 = index < pageCount - 10 ? index + 10 : pageCount;
            if (index < pageCount)
            {
                sb.AppendLine("<li><a href=\"" + GetPageUrl(next, dic) + "\"   myPageIndex=\"" + next + "\" ><i class=\"fa fa-angle-right\"></i></a></li>");
                if (index < pageCount - 10)
                {
                    sb.AppendLine("<li><a href=\"" + GetPageUrl(next10, dic) + "\"   myPageIndex=\"" + next10 + "\" ><i class=\"fa fa-angle-right\"></i><i class=\"fa fa-angle-right\"></i></a></li>");
                }
                else
                {
                    sb.AppendLine("<li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-right\"></i><i class=\"fa fa-angle-right\"></i></a></li>");
                }
            }
            else
            {
                sb.AppendLine("<li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-right\"></i></a></li><li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-right\"></i><i class=\"fa fa-angle-right\"></i></a></li>");
            }
            sb.AppendLine("</ul>");
            sb.AppendLine("<span style=\"color:#797979;\" class=\"pull-right\">共" + pageCount + "页，" + recordCount + "条数据</span>");            
            return sb.ToString();
        }


        public static string PageUrl_Web_InputPage(int recordCount, int index, int pageSize)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (var key in System.Web.HttpContext.Current.Request.QueryString.AllKeys)
            {
                dic.Add(key.ToLower(), System.Web.HttpContext.Current.Request.QueryString[key]);
            }
            //先计算总页数
            int pageCount = (int)Math.Ceiling((double)recordCount / pageSize);
            if (index > pageCount)
            {
                index = pageCount;
            }
            else if (index < 1)
            {
                index = 1;
            }
            if (pageCount <= 1)
            {
                //return "";
            }
            int startIndex = 1;
            if (pageCount > 9)
            {
                int count = 8;
                int start = index;
                int end = index;
                while (count > 0)
                {
                    var flag1 = false;
                    var flag2 = false;
                    if (start > 1)
                    {
                        flag1 = true;
                        start--;
                    }
                    if (end < pageCount)
                    {
                        flag2 = true;
                        end++;
                    }
                    if (flag1)
                    {
                        count--;
                    }
                    if (flag2)
                    {
                        count--;
                    }
                    if (!flag1 && !flag2)
                    {
                        count--;
                    }
                }
                startIndex = start;
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<ul class=\"pagination pagination-split pull-right nm_pages\">");
            int preIndex = index <= 1 ? 1 : index - 1;
            int pre10Index = index <= 10 ? 1 : index - 10;
            if (1 < index)
            {
                if (10 < index)
                {
                    sb.AppendLine("<li><a href=\"" + GetPageUrl(pre10Index, dic) + "\" myPageIndex=\"" + pre10Index + "\" ><i class=\"fa fa-angle-left\"></i><i class=\"fa fa-angle-left\"></i></a></li>");
                }
                else
                {
                    sb.AppendLine("<li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-left\"></i><i class=\"fa fa-angle-left\"></i></a></li>");
                }

                sb.AppendLine("<li><a href=\"" + GetPageUrl(preIndex, dic) + "\" myPageIndex=\"" + preIndex + "\" ><i class=\"fa fa-angle-left\"></i></a></li>");
            }
            else
            {
                sb.AppendLine("<li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-left\"></i><i class=\"fa fa-angle-left\"></i></a></li><li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-left\"></i></a></li>");
            }
            int endIndex = startIndex + 9;
            for (int i = startIndex; i <= pageCount && i < endIndex; i++)
            {
                string cls = "";
                if (i == index)
                {
                    cls = "class=\"active\"";
                }
                if (index <= 5 || i != startIndex)
                {
                    sb.AppendLine("<li " + cls + "><a href=\"" + GetPageUrl(i, dic) + " \"  myPageIndex=\"" + i + "\" >" + i + "</a></li>");
                }
            }
            int next = index < pageCount ? index + 1 : index;
            int next10 = index < pageCount - 10 ? index + 10 : pageCount;
            if (index < pageCount)
            {
                sb.AppendLine("<li><a href=\"" + GetPageUrl(next, dic) + "\"   myPageIndex=\"" + next + "\" ><i class=\"fa fa-angle-right\"></i></a></li>");
                if (index < pageCount - 10)
                {
                    sb.AppendLine("<li><a href=\"" + GetPageUrl(next10, dic) + "\"   myPageIndex=\"" + next10 + "\" ><i class=\"fa fa-angle-right\"></i><i class=\"fa fa-angle-right\"></i></a></li>");
                }
                else
                {
                    sb.AppendLine("<li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-right\"></i><i class=\"fa fa-angle-right\"></i></a></li>");
                }
            }
            else
            {
                sb.AppendLine("<li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-right\"></i></a></li><li class=\"disabled\"><a href=\"#\"><i class=\"fa fa-angle-right\"></i><i class=\"fa fa-angle-right\"></i></a></li>");
            }
            sb.AppendLine("</ul>");
            sb.AppendLine("<span style=\"color:#797979;\" class=\"pull-right\">共" + pageCount + "页，" + recordCount + "条数据</span>");
            sb.AppendLine("<div class=\"pull-right skipPage\">");
            sb.AppendLine("<div class=\"form-inline\">");
            sb.AppendLine("<div class=\"form-group\">");
            sb.AppendLine("<span class=\"form-control\">跳转到第</span>");
            sb.AppendLine("</div><div class=\"form-group\"><input type=\"text\" id=\"inputPage\" class=\"form-control\" /></div><div class=\"form-group\"><span class=\"form-control\">页</span></div><div class=\"form-group\">");
            sb.AppendLine("<button type=\"button\" locationPath=\"" + GetPageUrl(1, dic) + "\" class=\"btn btn-white\">确定</button>");
            sb.AppendLine("</div></div></div> ");
            return sb.ToString();
        }

        public static string PageUrl_JS(int recordCount, int index, int pageSize)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (var key in System.Web.HttpContext.Current.Request.QueryString.AllKeys)
            {
                dic.Add(key.ToLower(), System.Web.HttpContext.Current.Request.QueryString[key]);
            }
            //先计算总页数
            int pageCount = (int)Math.Ceiling((double)recordCount / pageSize);
            if (index > pageCount)
            {
                index = pageCount;
            }
            else if (index < 1)
            {
                index = 1;
            }
            if (pageCount <= 1)
            {
                return "";
            }
            int startIndex = 1;
            if (pageCount > 9)
            {
                int count = 8;
                int start = index;
                int end = index;
                while (count > 0)
                {
                    var flag1 = false;
                    var flag2 = false;
                    if (start > 1)
                    {
                        flag1 = true;
                        start--;
                    }
                    if (end < pageCount)
                    {
                        flag2 = true;
                        end++;
                    }
                    if (flag1)
                    {
                        count--;
                    }
                    if (flag2)
                    {
                        count--;
                    }
                    if (!flag1 && !flag2)
                    {
                        count--;
                    }
                }
                startIndex = start;
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<!-- Pagination: Aligned center -->");
            sb.AppendLine("<div class=\"pagination pagination-large pagination-centered margin-none\">");
            sb.AppendLine("<ul class=\"pagination pagination-centered margin-none\">");
            int preIndex = index <= 1 ? 1 : index - 1;
            if (1 < index)
            {
                sb.AppendLine("<li><a href=\"javascript:void(0);\" onclick=\"Paging(" + (index - 1) + ")\"  myPageIndex=\"" + preIndex + "\" >上一页</a></li>");
            }
            else
            {
                sb.AppendLine("<li class=\"disabled\"><a href=\"javascript:void(0);\">上一页</a></li>");
            }
            int endIndex = startIndex + 9;
            for (int i = startIndex; i <= pageCount && i < endIndex; i++)
            {
                string cls = "";
                if (i == index)
                {
                    cls = "class=\"active\"";
                }
                if (index <= 5 || i != startIndex)
                {
                    sb.AppendLine("<li " + cls + "><a href=\"javascript:void(0);\" onclick=\"Paging(" + i + ")\"  myPageIndex=\"" + i + "\" >" + i + "</a></li>");
                }
            }
            int next = index < pageCount ? index + 1 : index;

            if (index < pageCount)
            {
                sb.AppendLine("<li><a href=\"javascript:void(0);\" onclick=\"Paging(" + (index + 1) + ")\"  myPageIndex=\"" + next + "\" >下一页</a></li>");
            }
            else
            {
                sb.AppendLine("<li class=\"disabled\"><a href=\"javascript:void(0);\">下一页</a></li>");
            }
            sb.AppendLine("</ul>");
            sb.AppendLine("</div>");
            sb.AppendLine("<!-- // Pagination: Aligned center END -->");
            return sb.ToString();
        }

        public static string PageUrl_JS(int recordCount, int index, int pageSize, string searchUrl, string formID, string modalID)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            foreach (var key in System.Web.HttpContext.Current.Request.QueryString.AllKeys)
            {
                dic.Add(key.ToLower(), System.Web.HttpContext.Current.Request.QueryString[key]);
            }
            //先计算总页数
            int pageCount = (int)Math.Ceiling((double)recordCount / pageSize);
            if (index > pageCount)
            {
                index = pageCount;
            }
            else if (index < 1)
            {
                index = 1;
            }
            if (pageCount <= 1)
            {
                return "";
            }
            int startIndex = 1;
            if (pageCount > 9)
            {
                int count = 8;
                int start = index;
                int end = index;
                while (count > 0)
                {
                    var flag1 = false;
                    var flag2 = false;
                    if (start > 1)
                    {
                        flag1 = true;
                        start--;
                    }
                    if (end < pageCount)
                    {
                        flag2 = true;
                        end++;
                    }
                    if (flag1)
                    {
                        count--;
                    }
                    if (flag2)
                    {
                        count--;
                    }
                    if (!flag1 && !flag2)
                    {
                        count--;
                    }
                }
                startIndex = start;
            }
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<!-- Pagination: Aligned center -->");
            sb.AppendLine("<div class=\"pagination pagination-large pagination-centered margin-none\">");
            sb.AppendLine("<ul class=\"pagination pagination-centered margin-none\">");
            int preIndex = index <= 1 ? 1 : index - 1;
            if (1 < index)
            {
                sb.AppendLine("<li><a href=\"javascript:void(0);\" onclick=\"Paging(" + (index - 1) + ",'" + searchUrl + "','" + formID + "','" + modalID + "')\"  myPageIndex=\"" + preIndex + "\" >上一页</a></li>");
            }
            else
            {
                sb.AppendLine("<li class=\"disabled\"><a href=\"javascript:void(0);\">上一页</a></li>");
            }
            int endIndex = startIndex + 9;
            for (int i = startIndex; i <= pageCount && i < endIndex; i++)
            {
                string cls = "";
                if (i == index)
                {
                    cls = "class=\"active\"";
                }
                if (index <= 5 || i != startIndex)
                {
                    sb.AppendLine("<li " + cls + "><a href=\"javascript:void(0);\" onclick=\"Paging(" + i + ",'" + searchUrl + "','" + formID + "','" + modalID + "')\"  myPageIndex=\"" + i + "\" >" + i + "</a></li>");
                }
            }
            int next = index < pageCount ? index + 1 : index;

            if (index < pageCount)
            {
                sb.AppendLine("<li><a href=\"javascript:void(0);\" onclick=\"Paging(" + (index + 1) + ",'" + searchUrl + "','" + formID + "','" + modalID + "')\"  myPageIndex=\"" + next + "\" >下一页</a></li>");
            }
            else
            {
                sb.AppendLine("<li class=\"disabled\"><a href=\"javascript:void(0);\">下一页</a></li>");
            }
            sb.AppendLine("</ul>");
            sb.AppendLine("</div>");
            sb.AppendLine("<!-- // Pagination: Aligned center END -->");
            return sb.ToString();
        }

        private static string GetPageUrl(int pageIndex, Dictionary<string, string> dic)
        {

            if (dic.ContainsKey("pageindex"))
            {
                dic["pageindex"] = pageIndex.ToString();
            }
            else
            {
                dic.Add("pageindex", pageIndex.ToString());
            }

            string url = System.Web.HttpContext.Current.Request.Path;
            if (dic.Count > 0)
            {
                url += "?";
                foreach (var key in dic.Keys)
                {
                    url += key + "=" + dic[key] + "&";
                }
                url = url.TrimEnd('&');
            }
            return url;
        }
    }
}
