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.Web.Caching;
using Spaces;
using System.IO;
public partial class Tag_Files : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        FileList.ItemCreated += new RepeaterItemEventHandler(FileList_ItemCreated);
        if (!IsPostBack)
            BindPage();
    }
    
    void BindPage()
    {
        string s = Request.QueryString["TagName"];
        if (s == null)
        {
            return;
        }

        txtKey.Text = s;
        FileList.DataSource =SiteHelper.GetTagFiles(s, 300);
        FileList.DataBind();
    }

    
    #region 文件
    #region 创建资源列表
    void FileList_ItemCreated(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            HyperLink name = e.Item.FindControl("Name") as HyperLink;
            Literal FileSize = e.Item.FindControl("FileSize") as Literal;
            Image fileIcon = e.Item.FindControl("fileIcon") as Image;
            Literal Type = e.Item.FindControl("Type") as Literal;
            Literal Created = e.Item.FindControl("Created") as Literal;
            HyperLink Settings = e.Item.FindControl("Settings") as HyperLink;
            LinkButton Delete = e.Item.FindControl("Delete") as LinkButton;
            PlaceHolder pnlOpt = e.Item.FindControl("pnlOpt") as PlaceHolder;
            HyperLink OpenDir = e.Item.FindControl("OpenDir") as HyperLink;
            Literal Description = e.Item.FindControl("Description") as Literal;
            // Literal FileID = e.Item.FindControl("FileID") as Literal;
            //HyperLink ViewDesc = e.Item.FindControl("ViewDesc") as HyperLink;
            DataRowView dr = e.Item.DataItem as DataRowView;
            string color = "<font color=\"#006600\">{0}</a>";
            if (dr != null)
            {
                // fileIcon.ImageUrl = string.Format("{0}/images/icons/{1}.gif", Globals.ApplicationPath, Convert.ToInt32(dr["Attribute"]));
                // name.Text = dr["Name"].ToString();
                // name.ToolTip = dr["Description"].ToString();
                // name.NavigateUrl = Globals.GetSiteUrls().DownloadPage(dr["Account"].ToString(), Convert.ToInt32(dr["FileID"]));
                // FileID.Text = "f_" + Utils.EncryptID(Convert.ToInt32(dr["FileID"]));
                Created.Text = Convert.ToDateTime(dr["Created"]).ToString("MM-dd hh:mm");


                string ext = Path.GetExtension(dr["SourceName"].ToString()).ToLower();
                name.Text = string.Format(color, Utils.FormatFileName(dr["Name"].ToString(), 15));
                string account = dr["Account"].ToString();
                if (Utils.VerifyLimit(dr["Limits"].ToString(), 'R'))
                {


                    int fileID = Convert.ToInt32(dr["FileID"]);
                    name.NavigateUrl = Globals.GetSiteUrls().DownloadPage(account, fileID);
                    name.ToolTip = dr["Description"].ToString();
                    if (Convert.ToInt32(dr["Price"]) > 1)
                    {
                        name.NavigateUrl = "javascript:alert('" + string.Format("访问该文件需{0}个银豆", dr["Price"]) + "');";
                        name.ToolTip = string.Format("访问该文件需{0}个银豆", dr["Price"]);
                    }
                    if (pnlOpt != null)
                    {
                        SetOperate(pnlOpt, ext, account, fileID);
                    }




                }

                fileIcon.ImageUrl = GetImagePath(ext);
                FileSize.Text = Utils.FormatSpaceSize(Convert.ToInt32(dr["FileSize"]));
                OpenDir.Text = string.Format(color, Utils.GetDirName(dr["Directory"].ToString()));
                OpenDir.NavigateUrl = Globals.GetSiteUrls().UserDisk(account, Convert.ToInt32(dr["DirectoryID"]));
                OpenDir.Target = "_blank";
                // string desc = dr["Description"].ToString();
                // if (desc.Trim() == "")
                //     desc = "该文件没有说明!";
                // Description.Text = desc;
                // ViewDesc.NavigateUrl = "javascript:ShowDesc('" + FileID.Text + "')";
                //ViewDesc.Text = "查看";

            }




        }
    }
    #endregion

    private void SetOperate(PlaceHolder pnlOpt, string ext, string account, int fileID)
    {
        pnlOpt.Controls.Clear();
        HyperLink linkOperate;
        HyperLink readPage;
        switch (ext)
        {
            case ".html":
                goto case ".htm";
            case ".htm":

                goto case ".txt";
            case ".inc":
                goto case ".txt";
            case ".css":
                goto case ".txt";
            case ".txt":
                goto case ".aspx";
            case ".js":
                goto case ".vbs";
            case ".vbs":
                goto case ".aspx";
            case ".asp":

                goto case ".aspx";
            case ".asmx":
                goto case ".aspx";

            case ".ascx":
                goto case ".aspx";

            case ".aspx":
                goto case ".xml";
            case ".config":
                goto case ".xml";
            case ".xsd":
                goto case ".xml";
            case ".xsl":
                goto case ".xml";
            case ".xml":


                readPage = new HyperLink();
                readPage.ImageUrl = string.Format("{0}/images/icons/IE.GIF", Globals.ApplicationPath);
                readPage.NavigateUrl = Globals.GetSiteUrls().ReadPage(account, fileID);
                readPage.Target = "_blank";
                pnlOpt.Controls.Add(readPage);

                break;
            case ".jpg":
                goto case ".gif";
            case ".gif":
                linkOperate = new HyperLink();
                linkOperate.ImageUrl = string.Format("{0}/images/icons/view.gif", Globals.ApplicationPath);
                linkOperate.NavigateUrl = Globals.GetSiteUrls().ReadPage(account, fileID);
                linkOperate.Target = "_blank";
                pnlOpt.Controls.Add(linkOperate);
                break;
            case ".swf":

                linkOperate = new HyperLink();
                linkOperate.ImageUrl = string.Format("{0}/images/icons/asf.gif", Globals.ApplicationPath);
                linkOperate.NavigateUrl = Globals.GetSiteUrls().PlayFlash(account, fileID);
                pnlOpt.Controls.Add(linkOperate);
                pnlOpt.Controls.Add(new LiteralControl("&nbsp;"));
                readPage = new HyperLink();
                readPage.ImageUrl = string.Format("{0}/images/icons/IE.GIF", Globals.ApplicationPath);
                readPage.NavigateUrl = Globals.GetSiteUrls().ReadPage(account, fileID);
                readPage.Target = "_blank";
                pnlOpt.Controls.Add(readPage);
                break;
            case ".exe":

                break;
            case ".rar":
                goto case ".zip";
            case ".zip":

                break;
            case ".mdb":

                break;
            case ".doc":

                break;
            case ".asf":
                goto case ".mp3";
            case ".wma":
                goto case ".mp3";
            case ".wmv":
                goto case ".mp3";
            case ".avi":
                goto case ".mp3";
            case ".wav":
                goto case ".mp3";
            case ".mpeg":
                goto case ".mp3";
            case ".midi":
                goto case ".mp3";
            case ".aiff":
                goto case ".mp3";
            case ".au":
                goto case ".mp3";
            case ".mp3":
                linkOperate = new HyperLink();
                linkOperate.ImageUrl = string.Format("{0}/images/icons/mid.gif", Globals.ApplicationPath);
                linkOperate.NavigateUrl = Globals.GetSiteUrls().PlayMP3(account, fileID);
                pnlOpt.Controls.Add(linkOperate);

                break;
            default:

                pnlOpt.Visible = false;
                break;
        }


    }

    private string GetImagePath(string ext)
    {
        string icon = string.Empty;
        switch (ext)
        {
            case ".html":
                goto case ".htm";
            case ".htm":
                icon = "html.gif";
                break;
            case ".inc":
                goto case ".txt";
            case ".css":
                goto case ".txt";
            case ".txt":
                icon = "txt.gif";
                break;
            case ".js":
                icon = "perl.gif";
                break;
            case ".vbs":
                icon = "perl.gif";
                break;
            case ".asp":
                icon = "asp.gif";
                break;
            case ".asmx":
                goto case ".aspx";

            case ".ascx":
                goto case ".aspx";

            case ".aspx":
                icon = "asp.gif";
                break;
            case ".config":
                goto case ".xml";
            case ".xsd":
                goto case ".xml";
            case ".xsl":
                goto case ".xml";
            case ".xml":
                icon = "xml.gif";
                break;
            case ".jpg":
                icon = "jpg.gif";
                break;
            case ".gif":
                icon = "gif.gif";
                break;
            case ".swf":
                icon = "swf.gif";
                break;
            case ".exe":
                icon = "exe.gif";
                break;
            case ".rar":
                goto case ".zip";
            case ".zip":
                icon = "zip.gif";
                break;
            case ".mdb":
                icon = "access.gif";
                break;
            case ".doc":
                icon = "word.gif";
                break;
            case ".asf":
                goto case ".mp3";
            case ".wma":
                goto case ".mp3";
            case ".wmv":
                goto case ".mp3";
            case ".avi":
                goto case ".mp3";
            case ".wav":
                goto case ".mp3";
            case ".mpeg":
                goto case ".mp3";
            case ".midi":
                goto case ".mp3";
            case ".aiff":
                goto case ".mp3";
            case ".au":
                goto case ".mp3";
            case ".mp3":
                icon = "mid.gif";
                break;
            default:
                icon = "unknown.gif";
                break;

        }
        return string.Format("{0}/images/icons/{1}", Globals.ApplicationPath, icon);
    }
    #endregion
}
