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 System.Text;
using Spaces;
using System.Collections.Generic;
public partial class js : System.Web.UI.Page
{
  
    protected void Page_Load(object sender, EventArgs e)
    {
        string type = Request.QueryString["Type"];
        if (type == null)
            return;

        Response.HeaderEncoding=Encoding.UTF8;
        switch (type.ToLower())
        { 
            case "newtopic":
                GetNewTopic();
                break;
            case "tag":
                GetTags();
                break;
            case "newfile":
                GetNewFile();
                break;
        
        }     

    }
    void GetNewFile()
    {/*
        string baseUrl = SpacesUrl;//防止重复读取集合
        TagType tagType = TagType;
        DataView list = Files.GetNewFiles(Count);
        StringBuilder sb = new StringBuilder();
        string format = GetFileTemplate();
        foreach (DataRowView dr in list)
        {
            string account = dr["Account"].ToString();
            string name=Utils.FormatFileName(dr["Name"].ToString(), 15);
            string fileUrl=
            sb.AppendFormat(format,name,);
        }
      * */
    }
    string GetFileTemplate()
    {
        string format = @"<table width=""99%"" height=""22"" border=""0"" cellpadding=""0"" cellspacing=""4"">
          <tr> 
            <td width=""35%"">
            <img src=""/images/icons/swf.gif"" style=""border-width:0px;"" /> &nbsp;<a href=""{1}"">{0}</a>
            &nbsp;
            </td>
           <td width=""10%""></td>
            <td width=""10%""><div align=""center"">2MB</div></td>
             <td width=""20%""> 09-01 04:05 </td>
            <td width=""25%""><a  href=""/windinwing/disk/D6E10261840AB582.aspx"" target=""_blank""><font color=""#006600"">我的文档</a></a>    
</td>
          </tr>
        </table> ";
        return format;
    }
    void GetTags()
    {
        string baseUrl = SpacesUrl;//防止重复读取集合
        TagType tagType=TagType;
        List<TagItem> list= SiteTools.GetTagTop(tagType, Count);
        StringBuilder sb = new StringBuilder();
        string tagUrl = TagUrlFormat(tagType);
        sb.Append("<table style=\"BORDER-COLLAPSE: collapse\" bordercolor=\"#ffffff\" cellspacing=\"1\" cellpadding=\"2\" width=\"100%\" bgcolor=\"#F3F3F3\" border=\"1\"> ");
       
        foreach (TagItem t in list)
        {
            string url = string.Format(tagUrl, Server.UrlEncode(t.TagName.TrimStart(' ').TrimEnd(' ')));
            sb.Append("<tr><td align=middle width=18 height=13><!--放个图标什么的--></td>");
            sb.AppendFormat("<td width=100% height=13><a href=\"{3}{1}\" target=\"_blank\" title=\"{0}\">  {0} [{2}]  </a>  </td></tr>", t.TagName,url, t.RefCount,baseUrl);
        }
        sb.Append("</table>");
        Response.Write(string.Format("document.write('{0}')", sb.ToString()));
    }
    private string TagUrlFormat(TagType tagType)
    {
        string tagUrl = string.Empty;
        switch (tagType)
        {
            case TagType.All:
                tagUrl = "/TagTopList.aspx?TagName={0}";
                break;
            case TagType.BlogTopic:
                tagUrl = "/TagTopList.aspx?TagName={0}";
                break;
            case TagType.File:
                tagUrl = "/Tag_Files.aspx?TagName={0}";
                break;
            case TagType.Photo:
                tagUrl = "/Tag_Photos.aspx?TagName={0}";
                break;
        }
        return tagUrl;
    }
    #region 主题调用
    /// <summary>
    /// 获取最新blog主题
    /// </summary>
    void GetNewTopic()
    {
        string baseUrl = SpacesUrl;//防止重复读取集合
      List<DayBook> list=  Blogs.GetNewPosts(Count);
      StringBuilder sb = new StringBuilder();
      sb.Append("<table style=\"BORDER-COLLAPSE: collapse\" bordercolor=\"#ffffff\" cellspacing=\"1\" cellpadding=\"2\" width=\"100%\" bgcolor=\"#F3F3F3\" border=\"1\"> ");
      foreach (DayBook d in list)
      {
          string author = ShowAuthor ? string.Format("[<a href=\"{1}\" target=\"_blank\">{0}</a>]", d.Author,string.Format(Config.GetConfig().DefaultDomain,d.Account)) : "";
          sb.Append("<tr><td align=middle width=18 height=13><!--放个图标什么的--></td>");
          sb.AppendFormat("<td width=100% height=13><a href=\"{4}{1}\" target=\"_blank\" title=\"{0}\">  {0}  </a> {2} {3} </td></tr>", Utils.FormatLength(d.Title,Length), d.Link,author , ShowTime ? string.Format("[{0}]", d.DateAdded.ToString("yy-MM-dd hh:mm")) : "",baseUrl);
      }
      sb.Append("</table>");
      Response.Write(string.Format("document.write('{0}')",sb.ToString()));
  }
#endregion 

    #region 属性
    public TagType TagType
    {
        get {
            try {
                int t = int.Parse(Request.QueryString["TagType"]);
                switch (t)
                { 
                     
                      
                    case 1:
                        return TagType.BlogTopic;
                    case 2:
                        return TagType.File;
                    case 3:
                        return TagType.Photo;
                    default:
                        return TagType.All;
                }
            }
            catch {
                return TagType.All;
            
            }
        
        }
    }
  public int Count
  {
      get
      {
          try
          {
              int i = int.Parse(Request.QueryString["Count"]);
              if (i > 300)
                  i = 300;
              return i;
          }
          catch
          {
              return 10;
          }

      }
  }
  public int Length
  {
      get { try { return int.Parse(Request.QueryString["Length"]); } catch { return 18; } }
  }
  public string SpacesUrl
  {
      get
      {
          string url = Request.QueryString["SpacesUrl"];
          if (url == null)
              return string.Empty;
          if (!url.StartsWith("http://"))
              url = string.Format("http://{0}", url);
          return url;
      }

  }
  public bool ShowAuthor
  {
      get { try { return bool.Parse(Request.QueryString["ShowAuthor"]); } catch { return false; } }
  }
  public bool ShowTime
  {
      get { try { return bool.Parse(Request.QueryString["ShowTime"]); } catch { return false; } }
  }
    #endregion
}
