using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Spaces;
using Spaces.Blog;
using System.Collections.Generic;
public partial class TagTopList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Entries.ItemCreated += new RepeaterItemEventHandler(PostCreated);
        AspNetPager1.PageChanged += new Xiaoxiao.WebUI.PageChangedEventHandler(AspNetPager1_PageChanged);
        if (!IsPostBack)
        {
            BindPost();
        }
    }

    void AspNetPager1_PageChanged(object src, Xiaoxiao.WebUI.PageChangedEventArgs e)
    {
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        BindPost();
    }

    private void BindPost()
    {
        string s = Request.QueryString["TagName"];
        if (s != null)
        {
            PagedTag pt = new PagedTag(0, s.TrimStart(' ').TrimEnd(' '), AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize);
            int count;
            List<DayBook> list = Blogs.GetPagedPostsByTag(pt, out count);
            AspNetPager1.RecordCount = count;
            Entries.DataSource = list;
            Entries.DataBind();
            txtKey.Text = s;
        }
    }
    const string postdescWithComments = "{0} 阅读({5}) ┆ <a href=\"{1}#FeedBack\" Title = \"comments, pingbacks, trackbacks\">评论 ({2})</a> ┆{3}{4}";
    const string postdescWithNoComments = "  {0}┆{1}┆{2}┆{3}";
    private string strFavoriteLink = "";//=@"&nbsp;<a href=""{0}AddToFavorite.aspx?id={1}"">收藏</a>";
    private string strEditLink = "";
    public void PostCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DayBook entry = e.Item.DataItem as DayBook;
            if (entry != null)
            {
                HyperLink hl = e.Item.FindControl("TitleUrl") as HyperLink;
                entry.Link = Globals.GetSiteUrls().BlogView(entry.Account, entry.ID);
                if (hl != null)
                {
                    hl.NavigateUrl = entry.Link;
                    hl.Text = entry.Title;
                }
                HyperLink pl = e.Item.FindControl("PostLink") as HyperLink;
                if (pl != null)
                {
                    pl.NavigateUrl = entry.Link;
                }
                Literal PostCreated = e.Item.FindControl("PostCreated") as Literal;
                if (PostCreated != null)
                {
                    PostCreated.Text = string.Format("发表于:{0}", entry.DateAdded.ToString("yyyy-MM-dd HH:mm"));
                }
                Literal PostText = e.Item.FindControl("PostText") as Literal;
                if (PostText != null)
                {
                    PostText.Text = Formatter.FilterScript(entry.Text);
                }

                Literal desc = e.Item.FindControl("PostDescription") as Literal;
                if (desc != null)
                {
                    string link = entry.Link;
                }

                Literal PostDesc = e.Item.FindControl("PostDesc") as Literal;

                if (entry.PostType == PostType.BlogPost)
                {
                   // strEditLink = string.Format(UIData.EditPostsLink, "", entry.ID);
                }
                if (entry.PostType == PostType.Article)
                {
                   // strEditLink = string.Format(UIData.EditArticleLink, "", entry.ID);
                }

               // strFavoriteLink = String.Format(UIData.FavoriteLink, "", entry.ID, entry.TitleUrl);
                // PostDesc.Text = string.Format(postdescWithNoComments,  entry.Author, strEditLink, strFavoriteLink, entry.ViewCount);
                PostDesc.Text = string.Format(postdescWithComments, entry.Author, entry.Link, entry.FeedBackCount, strEditLink, strFavoriteLink, entry.ViewCount);
                if (entry.Tags.Count > 0)
                {
                    PlaceHolder phTags = e.Item.FindControl("phTags") as PlaceHolder;
                    if (phTags != null)
                    {
                        phTags.Controls.Add(new LiteralControl("关键字: "));
                        foreach (string s in entry.Tags)
                        {
                            HyperLink lnk = new HyperLink();
                            lnk.Text = s;
                            lnk.NavigateUrl = Globals.GetSiteUrls().TagPage(entry.Account, s.TrimStart(' ').TrimEnd(' '));
                            phTags.Controls.Add(lnk);
                            phTags.Controls.Add(new LiteralControl(" "));
                        }
                    }
                }
            }
        }
    }
}
