using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;

namespace HteamDd.Web.Web
{
    public partial class Dd_preview : System.Web.UI.Page
    {
        public string ID;
        public string title;
        public string content;
        public string lable;
        public string type;
        public string userName;
        public string linkaddress;
        public string playurl;
        protected void Page_Load(object sender, EventArgs e)
        {         
                if (Session["title"] == null && Session["lable"] == null&&Session["type"]==null)
                {
                    if (Request["type"] == "0")
                    {                       
                        SessionProcess();
                        Session["address"] = "#";
                    }
                    else if (Request["type"] == "1")
                    {
                        SessionProcess();
                        Session["address"] = "#";
                    }
                    else if (Request["type"] == "3")
                    {
                        if (Request["title"].ToString() =="")//判断当 标题 为空的时候
                        {            
                            Session["title"] = GetLinkaddress(Request["address"].ToString());
                            Session["content"] = Request["description"].ToString();
                            Session["lable"] = Request["lable"].ToString();
                            Session["type"] = Request["type"].ToString();
                            Session["user"] = Request["user"].ToString();
                            Session["address"] = GetLinkaddress(Request["address"].ToString());
                        }
                        else
                        {
                            Session["title"] = Request["title"].ToString();
                            Session["content"] = Request["description"].ToString();
                            Session["lable"] = Request["lable"].ToString();
                            Session["type"] = Request["type"].ToString();
                            Session["user"] = Request["user"].ToString();
                            Session["address"] = GetLinkaddress(Request["address"].ToString());
                        }
                    }
                    else if (Request["type"] == "4")
                    {
                        SessionProcess();
                        Session["address"] = Request["address"].ToString();
                    }
                }               
                else
                {
                    if (Session["update"] == null)//没有发布和编辑直接预览查看
                    {
                        Preview_Values();
                    }
                    else
                    {                      
                        Preview_Values();                 
                    }                                    
                }             
            }

        /// <summary>
        /// 在网页加载时给声明的变量赋值
        /// </summary>
        private void Preview_Values()
        {
            title = Server.HtmlEncode(Session["title"].ToString());
            content = Server.HtmlEncode(Session["content"].ToString());
            lable = Server.HtmlEncode(Session["lable"].ToString());
            type = Session["type"].ToString();
            userName = Session["user"].ToString();
            linkaddress = Server.HtmlEncode(Session["address"].ToString());
            if (Session["type"].ToString() == "4")
            {
                playurl = "http://player.youku.com/player.php/sid/" + GetPlayUrl(title) + "/v.swf&amp;isAutoPlay=true";

            }
            Session.Remove("title");
            Session.Remove("content");
            Session.Remove("lable");
            Session.Remove("type");
            Session.Remove("user");
            Session.Remove("address");
        }


        /// <summary>
        /// 用session来接收传过来的值
        /// </summary>
        private void SessionProcess()
        {
            Session["title"] = Request["title"].ToString();
            Session["content"] = Request["content"].ToString();
            Session["lable"] = Request["lable"].ToString();
            Session["type"] = Request["type"].ToString();
            Session["user"] = Request["user"].ToString();
        }
        /// <summary>
        /// 获得视频ID
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetPlayUrl(string url)
        {
            int index = url.IndexOf("id_", 20, 25);
            try
            {
                string subString = url.Substring(index, 21);
                string[] sub = subString.Split('.');
                string urlname = sub[0];
                string[] urlid = urlname.Split('_');
                return urlid[1];
            }
            catch
            {
                return "";
            }
           
        }

        /// <summary>
        /// 获取链接的网页代码
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        private string GetSourceCode(string url)
        {
            WebRequest req = WebRequest.Create(url);  //获取网页源代码
            string html = new StreamReader(req.GetResponse().GetResponseStream(), System.Text.Encoding.GetEncoding("utf-8")).ReadToEnd();  //需指定网页的编码方式，如gb2312或 utf-8
            return html;
        }

        /// <summary>
        /// 用来处理获取的链接地址
        /// </summary>
        private string GetLinkaddress( string address)
        {
            string link;
            linkaddress = address;//获取链接的地址
            int num = linkaddress.Length;//用来判断链接的地址的长度
            if (num < 7)
            {
               return  link = "http://" + linkaddress;
            }
            else
            {
                link = linkaddress.Substring(0, 7);//用来截取前7个字符串
                if (link == "http://")
                {
                   return link = linkaddress;
                }
                else
                {
                   return link = "http://" + linkaddress;
                }
            }
        }
    }
}