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 JuSNS.Model;
using JuSNS.Common;

namespace JuSNS.Web.home.Movie
{
    public partial class Movie : JuSNS.UI.Page.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            GetInfo();
            if (!Page.IsPostBack)
            {
                getFriendComm();
                this.MovieListURL.InnerHtml = getMovieListURL();
            }
            this.TopLooked.InnerHtml = GetTopList(4, 2);
            this.Tophits.InnerHtml = GetTopList(4, 0);
            this.Topcomment.InnerHtml = GetTopList(4, 1);
            this.GetUserUserList.InnerHtml = GetUserUrl();
        }

        protected string GetTopList(int Num, int flg)
        {
            JuSNS.Home.Movie md = new JuSNS.Home.Movie();
            string s_top = string.Empty;
            DataTable dt = md.GetTopMovie(Num, flg);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    s_top += "<li><a href=\"" + rooDir + "/home/movie/movie.aspx?id=" + dt.Rows[i]["MovieID"].ToString() + "\"><img  style=\"width:80px;height:96px;\" src=\"" + rooDir + "/" + dt.Rows[i]["PosterFile"].ToString() + "\"  title=\"" + dt.Rows[i]["title"].ToString() + "\"/><span>" + dt.Rows[i]["title"].ToString() + "</span></a></li>";
                }
            }
            dt.Clear(); dt.Dispose();
            return "<ul>" + s_top + "</ul>";
        }

        protected string getMovieListURL()
        {
            string cStr = string.Empty;
            JuSNS.Home.Movie MD = new JuSNS.Home.Movie();
            JuSNS.Model.MovieInfo MM = new JuSNS.Model.MovieInfo();
            MM = MD.GetMovieInfo(Convert.ToInt32(Request.QueryString["ID"]));
            string ServerName = string.Empty;
            switch (MM.zt_dy)
            {
                case "youku":
                    ServerName = "优酷";
                    break;
                case "56":
                    ServerName = "我乐网";
                    break;
                case "tudou":
                    ServerName = "土豆网";
                    break;
                case "bobo":
                    ServerName = "BOBO P2P";
                    break;
                case "qvod":
                    ServerName = "QVOD P2P";
                    break;
                case "real":
                    ServerName = "RealOne";
                    break;
                case "media":
                    ServerName = "MediaPlayer";
                    break;
            }
            cStr += "<div class=\"serverName\">&nbsp;<a href=\"javascript:void(0);\">服务器①(" + ServerName + ")</a></div>";
            string mURLStr = string.Empty;
            string mURL = MM.zt_url;
            string[] mURLARR = mURL.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < mURLARR.Length; i++)
            {
                string getURLs = mURLARR[i];
                string getplayURL = string.Empty;
                if (mURLARR[i].IndexOf("$") > -1)
                {
                    getURLs = mURLARR[i].Split('$')[0];
                    getplayURL = mURLARR[i].Split('$')[1];
                }
                else
                {
                    getURLs = "播放"+(i+1);
                    getplayURL = mURLARR[i];
                }

                mURLStr += "<li  class=\"movieplay\"><a href=\"javascript:;\"  onclick=\"showDivMovie('" + Request.QueryString["ID"] + "','','" + getplayURL + "');\">" + getURLs + "</a></li>";
            }
            cStr += "<div class=\"serverlist nl\"><ul>" + mURLStr + "</ul></div>";
            if (MM.zt_dy1 != string.Empty && MM.zt_dy1 != null)
            {
                string ServerName1 = string.Empty;
                switch (MM.zt_dy1)
                {
                    case "youku":
                        ServerName1 = "优酷";
                        break;
                    case "56":
                        ServerName1 = "我乐网";
                        break;
                    case "tudou":
                        ServerName1 = "土豆网";
                        break;
                    case "bobo":
                        ServerName1 = "BOBO P2P";
                        break;
                    case "qvod":
                        ServerName1 = "QVOD P2P";
                        break;
                    case "real":
                        ServerName1 = "RealOne";
                        break;
                    case "media":
                        ServerName1 = "MediaPlayer";
                        break;
                }
                cStr += "<div class=\"serverName\">&nbsp;<a href=\"javascript:void(0);\">服务器②(" + ServerName1 + ")</a></div>";
                string mURLStr1 = string.Empty;
                string mURL1 = MM.zt_url1;
                string[] mURLARR1 = mURL1.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                for (int i = 0; i < mURLARR1.Length; i++)
                {
                    string getURLs1 = mURLARR1[i];
                    string getplayURL1 = string.Empty;
                    if (mURLARR1[i].IndexOf("$") > -1)
                    {
                        getURLs1 = mURLARR1[i].Split('$')[0];
                        getplayURL1 = mURLARR1[i].Split('$')[1];
                    }
                    else
                    {
                        getURLs1 = "播放" + (i + 1);
                        getplayURL1 = mURLARR1[i]; 
                    }
                    mURLStr1 += "<li class=\"movieplay\"><a href=\"javascript:void(0);\"  onclick=\"showDivMovie('" + Request.QueryString["ID"] + "','1','" + getplayURL1 + "');\">" + getURLs1 + "</a></li>";
                }
                cStr += "<div class=\"serverlist nl\"><ul>" + mURLStr1 + "</ul></div>";
            }
            return cStr;
        }

        protected string GetUserUrl()
        {
            string s_list = string.Empty;
            JuSNS.Home.Movie movie = new JuSNS.Home.Movie();
            DataTable dt = movie.GetMovieUserList(Convert.ToInt32(Request.QueryString["Id"]));
            if (dt != null && dt.Rows.Count > 0)
            {
                s_list += "<div class=\"hdtitle s97 bold\">相关视频(点击播放，地址由用户提交)</div>";
                s_list += "<div class=\"movieuserplay\"><ul>";
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    s_list += "<li><a href=\"javascript:void(0);\" onclick=\"showUserlistPlay('" + dt.Rows[i]["ID"].ToString() + "','" + dt.Rows[i]["UserID"].ToString() + "','"+dt.Rows[i]["MovieURL"].ToString()+"')\">" + dt.Rows[i]["MovieName"].ToString() + "</a></li>";
                }
                s_list += "</ul></div>";
            }
            dt.Clear(); dt.Dispose();
            return s_list;
        }

        /// <summary>
        /// 取得参数信息
        /// </summary>
        protected void GetInfo()
        {
            int MovieID = 0;
            string s_MovieID = Request.QueryString["ID"];
            if (string.IsNullOrEmpty(s_MovieID))
                PageError("参数传递错误", "" + rooDir + "/home/movie/index.aspx");
            MovieID = Convert.ToInt32(s_MovieID);
            HidMovieID.Value = MovieID.ToString();

            JuSNS.Home.Movie movie = new JuSNS.Home.Movie();
            MovieInfo movieinfo = new MovieInfo();
            movieinfo = movie.GetMovieInfo(MovieID);
            if (movieinfo.UserID > 0)
            {
                if (!this.PrivacyAvailable(EnumPrivacyType.Movies, GetUserID(), movieinfo.UserID))
                {
                    PageError("用户的隐私设置。你无法查看此影片。", "javascript:history.back();", true);
                }
            }
            this.ptitle.InnerText = movieinfo.Title + "电影概况";
            em_Name.InnerHtml = movieinfo.Title;
            span_Name.InnerHtml = movieinfo.Title + "<span class=\"time normal\"> (此影片被浏览了" + movieinfo.hits + "次)</span>";
            Img_Movie.Src = rooDir + "/" + movieinfo.PosterFile;
            Img_Movie.Alt = movieinfo.Title;
            A_Upload.InnerHtml = "电影海报(" + movieinfo.MoviePic+")";
            A_Upload.HRef = "pic.aspx?ID=" + MovieID;
            string s_Director = "";
            string[] arr_Director = movieinfo.Director.Split('|');
            for (int i = 0; i < arr_Director.Length; i++)
            {
                s_Director += " <a href=\"" + rooDir + "/home/movie/index.aspx?r=all&Type=Director&Key=" + Input.URLEncode(arr_Director[i].ToString()) + "\">" + arr_Director[i].ToString() + "</a><br />";
            }
            span_Director.InnerHtml = s_Director.TrimEnd('/');
            string s_Billing = "";
            string[] arr_Billing = movieinfo.Billing.Split('|');
            for (int i = 0; i < arr_Billing.Length; i++)
            {
                s_Billing += " <a href=\"" + rooDir + "/home/movie/index.aspx?r=all&Type=Billing&Key=" + Input.URLEncode(arr_Billing[i].ToString()) + "\">" + arr_Billing[i].ToString() + "</a><br />";
            }
            span_Billing.InnerHtml = s_Billing.TrimEnd('/');
            a_Year.InnerHtml = movieinfo.YearOnShow.ToString();
            a_Year.HRef = "" + rooDir + "/home/movie/index.aspx?r=all&Type=YearOnShow&Key=" + movieinfo.YearOnShow + "";
            int SortID = movieinfo.SortID;
            if (SortID != 0)
            {
                sapn_Sort.InnerHtml = "<a href=\"" + rooDir + "/home/movie?r=all&s=" + SortID.ToString() + "\">" + movie.GetMovieSortName(SortID) + "</a>";
            }

            if (movieinfo.Gut.Length > 200)
                span_Gut.InnerHtml = Input.HtmlDecode(Input.GetSubString(movieinfo.Gut, 200)) + "... <a href=\"javascript:void(0)\" onclick=\"MoreGut(" + MovieID + ",'Open');\">更多</a>";
            else
                span_Gut.InnerHtml = Input.HtmlDecode(movieinfo.Gut);

            string s_Comment = movie.getComment(GetUserID(), MovieID);
            if (!string.IsNullOrEmpty(s_Comment))
            {
                s_Comment = "<div id=\"movie-review\">" +
                            "<div class=\"content\" runat=\"server\" id=\"div_review\">" + JuSNS.Common.Input.HtmlEncode(s_Comment) + "" +
                            "</div><br />(<a href=\"javascript:void(0);\"" +
                            " class=\"edit\" onclick=\"javascript:EditComm(document.form1.HidMovieID.value);\">修改评论</a>)</div>";
            }
            else
            {
                s_Comment = "<p><textarea id=\"review\" rows=\"3\" maxlength=\"500\" style=\"width: 250px\" class=\"f-text\"></textarea>" +
                            "</p><p class=\"pstr\"><input type=\"button\" class=\"f-button\" value=\"发表评论\" " +
                            "onclick=\"javascript:SaveComm(document.form1.review.value,document.form1.HidMovieID.value);\" />" +
                            "<span id=\"span_err\"></span></p>";
            }
            Common.InnerHtml = s_Comment;

            Response.Cookies["star"].Value = string.Empty;
            int sorce = movie.getScore(GetUserID(), MovieID);
            switch (sorce)
            {
                case 0:
                    a_Think.Attributes.Remove("onmouseout");
                    a_Think.Attributes.Remove("onmouseover");
                    a_Think.Attributes.Add("class", "movie-wannasee");
                    Session["ThinkMovie"] = "True";
                    break;
                case -1:
                    a_NoThink.Attributes.Remove("onmouseout");
                    a_NoThink.Attributes.Remove("onmouseover");
                    a_NoThink.Attributes.Add("class", "movie-notinterested");
                    Session["NoThinkMovie"] = "True";
                    break;
                case -3:
                    break;
                default:
                    span_star.Attributes.Add("class", "rate-star rate-star" + sorce);
                    Session["Star"] = "True";
                    break;
            }
            int istar = Convert.ToInt32(movieinfo.Score);
            p_result.InnerHtml = "<span class=\"rate-star rate-star" + istar + "\">" + istar + "</span> " + movieinfo.Score * 2 + "/10 " +
                                 "(<span class=\"reshow\">" + movieinfo.Seen + "</span> 人) &nbsp; " +
                                 "<span class=\"reshow\">" + movieinfo.Wanna + "</span> 人想看，<span class=\"reshow\">" + movieinfo.Wonna + "</span> 人不想看";
        }

        /// <summary>
        /// 取得好友对电影的评论
        /// </summary>
        protected void getFriendComm()
        {
            JuSNS.Home.Movie movie = new JuSNS.Home.Movie();
            DataTable dt = movie.FriendMovieComm(GetUserID(), Convert.ToInt32(this.HidMovieID.Value));
            span_Cnt.InnerHtml = dt.Rows.Count.ToString();
            if (dt != null)
            {
                Rpt_Comm.DataSource = dt;
                Rpt_Comm.DataBind();
            }
            dt.Clear();
            dt.Dispose();
        }

       

        /// <summary>
        /// 取得想看/不想看/以及看过对此的星级评价样式
        /// </summary>
        /// <param name="type">类型</param>
        /// <returns>返回样式</returns>
        protected string getCSS(object type)
        {
            string s_CSS = "";
            switch (type.ToString())
            {
                case "-1":
                    s_CSS = "movie-notinterested";
                    break;
                case "0":
                    s_CSS = "movie-wannasee";
                    break;
                case "1":
                    s_CSS = "rate-star rate-star1";
                    break;
                case "2":
                    s_CSS = "rate-star rate-star2";
                    break;
                case "3":
                    s_CSS = "rate-star rate-star3";
                    break;
                case "4":
                    s_CSS = "rate-star rate-star4";
                    break;
                case "5":
                    s_CSS = "rate-star rate-star5";
                    break;
            }
            return s_CSS;
        }

        /// <summary>
        /// 取得评论内容
        /// </summary>
        /// <param name="Content">内容</param>
        /// <returns></returns>
        protected string getContent(object Content)
        {
            string s_Content = "";
            if (Content != DBNull.Value)
                s_Content = JuSNS.Common.Input.HtmlDecode(Content.ToString());
            return s_Content;
        }
    }
}
