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

namespace JuSNS.Web.home.Movie
{
    public partial class index : JuSNS.UI.Page.BasePage
    {
        JuSNS.Home.Movie md = new JuSNS.Home.Movie();
        protected void Page_Load(object sender, EventArgs e)
        {
            GetMovieSort();
            DataBind();
            LoadList();
            this.TopLooked.InnerHtml = GetTopList(4, 2);
            this.Tophits.InnerHtml = GetTopList(6, 0);
            this.Topcomment.InnerHtml = GetTopList(6, 1);
        }

        /// <summary>
        /// 加载分页
        /// </summary>
        /// <param name="PageIndex">当前页数</param>
        protected void LoadList()
        {
            string s = Request.QueryString["s"];
            string rr=Request.QueryString["r"];
            DivPage.Visible = true;
            DivPage1.Visible = false;
            int r = 0, p = 0, PageIndex = 0;
            PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt = null;
            if (rr == "my")
            {
                this.myli.Attributes.Add("class", "current");
                this.allli.Attributes.Add("class", "");
                this.fli.Attributes.Add("class", "");
                this.showSort.Visible = false; 
                SqlConditionInfo[] stsmy = new SqlConditionInfo[1];
                stsmy[0] = new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32);
                dt = JuSNS.Home.UtilPage.GetPage("MovieList_my_aspx", PageIndex, 10, out r, out p, stsmy);
            }
            else if (rr == "all")
            {
                this.myli.Attributes.Add("class", "");
                this.allli.Attributes.Add("class", "current");
                this.fli.Attributes.Add("class", "");
                this.showSort.Visible = true;
                string ky = Request.QueryString["key"];
                if (!string.IsNullOrEmpty(ky))
                {
                    string tp = Request.QueryString["Type"];
                    this.KeyDiv.InnerHtml = "找到 <span class=\"reshow\">" + ky + "</span> 的关键字";
                    if (!string.IsNullOrEmpty(tp))
                    {
                        switch (tp)
                        {
                            case "Director":
                                SqlConditionInfo[] stall_Director = new SqlConditionInfo[1];
                                stall_Director[0] = new SqlConditionInfo("@Key", ky, TypeCode.String);
                                stall_Director[0].Blur = 3;
                                dt = JuSNS.Home.UtilPage.GetPage("MovieList_Director_aspx", PageIndex, 10, out r, out p, stall_Director);
                                break;
                            case "Billing":
                                SqlConditionInfo[] stall_Billing = new SqlConditionInfo[1];
                                stall_Billing[0] = new SqlConditionInfo("@Key", ky, TypeCode.String);
                                stall_Billing[0].Blur = 3;
                                dt = JuSNS.Home.UtilPage.GetPage("MovieList_Billing_aspx", PageIndex, 10, out r, out p, stall_Billing);
                                break;
                            case "YearOnShow":
                                SqlConditionInfo[] stall_YearOnShow = new SqlConditionInfo[1];
                                stall_YearOnShow[0] = new SqlConditionInfo("@Key", ky, TypeCode.String);
                                stall_YearOnShow[0].Blur = 3;
                                dt = JuSNS.Home.UtilPage.GetPage("MovieList_YearOnShow_aspx", PageIndex, 10, out r, out p, stall_YearOnShow);
                                break;
                        }
                    }
                    else
                    {
                        SqlConditionInfo[] stall_2 = new SqlConditionInfo[1];
                        stall_2[0] = new SqlConditionInfo("@Key", ky, TypeCode.String);
                        stall_2[0].Blur = 3;
                        this.KeyDiv.InnerHtml = "找到 <span class=\"reshow\">" + ky + "</span> 的关键字";
                        dt = JuSNS.Home.UtilPage.GetPage("MovieList_Key1_aspx", PageIndex, 10, out r, out p, stall_2);
                    }
                }
                else
                {
                    if (JuSNS.Common.Input.IsInteger(s))
                    {
                        //if (JuSNS.Common.Input.IsInteger(rr))
                        //{
                        //    if (!this.PrivacyAvailable(EnumPrivacyType.Movies, GetUserID(), Convert.ToInt32(rr)))
                        //    {
                        //        PageError("用户的隐私设置，你无法查看", "javascript:history.back();");
                        //    }
                        //}
                        SqlConditionInfo[] stall_1 = new SqlConditionInfo[1];
                        stall_1[0] = new SqlConditionInfo("@s", Convert.ToInt32(s), TypeCode.Int32);
                        dt = JuSNS.Home.UtilPage.GetPage("MovieList_all1_aspx", PageIndex, 10, out r, out p, stall_1);
                    }
                    else
                    {
                        dt = JuSNS.Home.UtilPage.GetPage("MovieList_all_aspx", PageIndex, 10, out r, out p, null);
                    }
                }
            }
            else if (JuSNS.Common.Input.IsInteger(rr))
            {
                this.myli.Attributes.Add("class", "");
                this.allli.Attributes.Add("class", "");
                this.fli.Attributes.Add("class", "current");
                this.showSort.Visible = false;
                JuSNS.Home.User us = new JuSNS.Home.User();
                UserInfo mdl = new UserInfo();
                mdl = us.GetUserInfo(Convert.ToInt32(rr));
                this.fli.InnerHtml = "<a href=\"../movie?r=" + rr + "\">" + mdl.Username + "的电影/视频</a>";
                SqlConditionInfo[] stsmy = new SqlConditionInfo[1];
                stsmy[0] = new SqlConditionInfo("@UserID", Convert.ToInt32(rr), TypeCode.Int32);
                dt = JuSNS.Home.UtilPage.GetPage("MovieList_my_aspx", PageIndex, 10, out r, out p, stsmy);
            }
            else
            {
                this.myli.Attributes.Add("class", "");
                this.allli.Attributes.Add("class", "");
                this.fli.Attributes.Add("class", "current");
                this.showSort.Visible = false;
                if (JuSNS.Common.Input.IsInteger(s))
                {
                    SqlConditionInfo[] st = new SqlConditionInfo[2];
                    st[0] = new SqlConditionInfo("@s", Convert.ToInt32(s), TypeCode.Int32);
                    st[1] = new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32);
                    dt = JuSNS.Home.UtilPage.GetPage("MovieList_2_aspx", PageIndex, 10, out r, out p, st);
                }
                else
                {
                    SqlConditionInfo[] sts = new SqlConditionInfo[1];
                    sts[0] = new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32);
                    dt = JuSNS.Home.UtilPage.GetPage("MovieList_1_aspx", PageIndex, 10, out r, out p, sts);
                }
            }
            if (dt != null)
            {
                Rpt_MovieList.DataSource = dt;
                Rpt_MovieList.DataBind();
                if (dt.Rows.Count == 0)
                {
                    DivResult.InnerHtml = "<li>暂时还没有电影。</li>";
                    DivResult.Attributes.Add("class", "nc");
                }
            }

            //判断是否调用分页
            if (r > 10)
            {
                string s_page = PageNavigator.Pagination(PageIndex, p, "../movie?r=" + rr + "&s=" + s + "");
                DivPage1.Visible = true;
                DivPage1.InnerHtml = s_page;
                DivPage.InnerHtml = s_page;
            }
            else
            {
                DivPage1.Visible = false;
                DivPage.Visible = false;
            }
            this.MovieCount.InnerHtml = r.ToString();
        }

        /// <summary>
        /// 得到分类
        /// </summary>
        protected void GetMovieSort()
        {
            string s = Request.QueryString["s"];
            string SortStr = "<select ID=\"MovieSort\" onchange=\"getMovieURL(this);\">";
            SortStr += "<option value=\"\">所有电影...</option>";
            DataTable  dt = md.getDictMovieSortList();
            if(dt!=null&&dt.Rows.Count>0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string count = dt.Rows[i]["Scount"].ToString();
                    string ID = dt.Rows[i]["ID"].ToString();
                    string CName = dt.Rows[i]["SortName"].ToString();
                    if (s == ID)
                    {
                        SortStr += "<option value=\"" + ID + "\" selected>- " + CName + "(" + count + ")</option>\r\n";
                    }
                    else
                    {
                        SortStr += "<option value=\"" + ID + "\">- " + CName + "(" + count + ")</option>\r\n";
                    }
                }
            }
            dt.Clear(); dt.Dispose();
            SortStr += "</select>";
            this.mSort.InnerHtml = SortStr;
        }

        protected string GetInfo(object mid,object userid, object hits, object comment)
        {
            string s_info = string.Empty;
            if (userid.ToString() != "0" && JuSNS.Common.Input.IsInteger(userid.ToString()))
            {
                JuSNS.Home.User us = new JuSNS.Home.User();
                UserInfo mdl = new UserInfo();
                mdl = us.GetUserInfo(Convert.ToInt32(userid.ToString()));
                s_info += "<a href=\"../movie/?r=" + userid.ToString() + "\" title=\"" + mdl.Username + "的所有电影\"><img src=\"../../template/" + JuSNS.Config.UiConfig.SkinStyle + "/images/movie-a.gif\" align=\"absmiddle\" /></a><a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", userid.ToString()) + "\" title=\"访问" + mdl.Username + "的空间\">" + mdl.Username + "</a> &nbsp; ";
            }

            return s_info + "<a href=\"top.aspx\">点击(" + hits.ToString() + ")</a> &nbsp; <a href=\"../movie/movie.aspx?id="+mid.ToString()+"#comment\">评论(" + hits.ToString() + ")</a>";
        }

        protected string GetTopList(int Num, int flg)
        {
            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=\"../movie/movie.aspx?id=" + dt.Rows[i]["MovieID"].ToString() + "\"><img  style=\"width:80px;height:96px;\" src=\"../../" + 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>";
        }
    }
}
