using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SilverlightWeb.portlets.Silverlight_sample.UserControl
{
    public partial class VideoWebUserControl : System.Web.UI.UserControl
    {
        int[] myPlayList;
        public string SLObjectExtra { set; get; }

        [Personalizable, WebBrowsable]
        public int[] MyPlayList
        {
            get { return myPlayList; }
            set { myPlayList = value; }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            loadFileList();

        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            if (this.FileUpload1.HasFile)
            {
                DBTool.sendToDb(FileUpload1.FileBytes, FileUpload1.FileName, FileUpload1.FileName);
                loadFileList();
            }
        }

        public string urls = "";
        protected void loadFileList()
        {
            string handleName = "webFileHandler.ashx";
            List<FileInfo> files = DBTool.getfileInfos();
            string baseUrl = Context.Request.Url.ToString();
            if (handleName != null)
            {
                int i = baseUrl.LastIndexOf("/");
                if (i > 0)
                {
                    baseUrl = baseUrl.Substring(0, i + 1) + handleName;
                }
            }
            urls = "";
            foreach (FileInfo jf in files)
            {
                jf.url = baseUrl + "?mode=download_" + jf.id;
                urls = urls + "<a name=\"videoUrl\" href=\"" + jf.url + "\" visible=\"false\" style=\"visibility:hidden\" >" + jf.name + "</a>";
            }
            this.urlList.Text = urls;
        }
    }
}