using System;
using System.Data;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
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.Common;
using JuSNS.Home;
using JuSNS.Model;
using JuSNS.Config;
using JuSNS.UI.Control;

namespace JuSNS.Web.home.Album
{
    public partial class quan : JuSNS.UI.Page.UserPage
    {
        protected string HeadImage = ""; //头像地址
        protected UserInfo CU = new UserInfo(); //被访问的用户信息 
        protected UserInfo UI = new UserInfo(); //当前用户信息
        protected int QuangCount = 0; //当前好友被圈照片数
        protected string r = ""; //queryString参数r
        protected string a = ""; //queryString参数a
        protected string Photos = ""; //被圈好友列表内容
        protected string Title = "";
        protected string visiteUserID = "";
        protected string h2Title = "相册";  //相册标题
        protected string spaceTitle = "我的空间";
        protected void Page_Load(object sender, EventArgs e)
        {
            r = Request.QueryString["r"];
            a = Request.QueryString["a"];
            if (!string.IsNullOrEmpty(r))
            {
                visiteUserID = r;
                CU = getUserInfo(Convert.ToInt32(r));
                HeadImage = GetHeadImage(Convert.ToInt32(r));
                Title = CU.Username + "最新被圈照片";
                h2Title = CU.Username + "最新被圈照片";
                if (Convert.ToInt32(r) != this.UserID)
                {
                    spaceTitle = "返回" + CU.Username + "的空间";
                }
                wrap_a.Visible = false;
                wrap_b.Visible = true;
                //wrap.Attributes.Add("class", "tagged-photos");

                //隐私部分
                this.Enum_Privacy_Type = EnumPrivacyType.Lasso;
                this.VisitedUserID = Convert.ToInt32(r);
                //隐私end

                bindDataMytag();
                bindDataOtherTag();
            }
            else
            {
                visiteUserID = this.UserID.ToString();
                Title = "最新被圈好友";
                CU = getUserInfo(this.UserID);
                HeadImage = GetHeadImage(this.UserID);
                setPhotos();
                wrap_a.Visible = true;
                wrap_b.Visible = false;
            }
        }

        /// <summary>
        /// 设置好友被圈的照片
        /// </summary>
        protected void setPhotos()
        {
            int ReCount, PgCount;
            int PageIndex = 1;
            if (Request.QueryString["Page"] != null)
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            DataTable dt = UtilPage.GetPage("FriendLasso_List", PageIndex, 21, out ReCount, out PgCount, new SqlConditionInfo("@UserID", this.UserID, TypeCode.Int32));
            QuangCount = ReCount;
            string PageUrl = "quan.aspx";
            DataTable data = new DataTable();
            data.Columns.Add("d1", typeof(string));
            Home.Album ab = new Home.Album();
            JuSNS.Home.User u = new JuSNS.Home.User();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                UserInfo ui = u.GetUserInfo((int)dt.Rows[i]["FriendID"]);
                Dict dc = new Dict();
                DictAreaInfo dai = dc.Info(ui.Province);
                StringBuilder sb = new StringBuilder();
                PhotoInfo pi = getPhotoInfo((int)dt.Rows[i]["PhotoID"]);
                DataRow dr = data.NewRow();
                string imgPath = this.GetSmallPic(pi.FilePath, 1);
                if (pi.PhotoType == 0)
                {
                    imgPath = this.GetHeadImage(pi.UploadUser,1);
                }
                sb.Append("                        <div class=\"photo-album nl\">\n");
                sb.Append("                                    <div class=\"image nl\">\n");
                sb.Append("                                         <a href=\"photoview.aspx?r[0]=" + dt.Rows[i]["PhotoID"] + "\"><img src=\"" + imgPath + "\" alt=\"" + ui.Username + "\" /></a>\n");
                sb.Append("                                    </div>\n");
                sb.Append("                                     <div class=\"nr paddings10 w120\">\n");
                sb.Append("                                        <div class=\"pstr\"><a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", ui.UserID.ToString()) + "\" class=\"bold\">" + ui.Username + "</a> (" + dai.Name + ")</div>\n");
                sb.Append("                                        <div class=\"pstr\"><a href=\"quan.aspx?r=" + ui.UserID + "&uName=" + JuSNS.Common.Input.URLEncode(ui.Username) + "\">" + ui.Username + "被圈的照片</a></div>\n");
                sb.Append("                                        <div class=\"time1\">被圈于" + Common.Public.getDaySpan(Convert.ToDateTime(dt.Rows[i]["PostTime"])) + " 天前</div>\n");
                sb.Append("                                     </div>\n");
                sb.Append("                        </div>\n");
                dr["d1"] = sb.ToString();
                data.Rows.Add(dr);
            }
            this.FriendLasso.Visible = true;
            this.FriendLasso.DataSource = data;
            this.FriendLasso.RepeatColumns = 3;
            this.FriendLasso.RepeatDirection = RepeatDirection.Vertical;
            this.FriendLasso.DataBind();
            PageUrl += "?a=all";
            if (ReCount > 21)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
            else
            {
                this.DivPage.Visible = false;
            }

        }

        /// <summary>
        /// 取得照片文件名
        /// </summary>
        /// <param name="photoid">照片id</param>
        /// <returns></returns>
        private PhotoInfo getPhotoInfo(int photoid)
        {
            Photo p = new Photo();
            return p.Info(photoid);
        }
        /// <summary>
        /// 取得用户信息
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <returns></returns>
        private UserInfo getUserInfo(int userid)
        {
            Home.User u = new Home.User();
            return u.GetUserInfo(userid);
        }
        /// <summary>
        /// 取得地理信息
        /// </summary>
        /// <param name="dict">地理ID</param>
        /// <returns></returns>
        private DictAreaInfo getArea(int dict)
        {
            Dict d = new Dict();
            return d.Info(dict);
        }
        /// <summary>
        /// 在自己的相册中被圈的照片
        /// </summary>
        protected void bindDataMytag()
        {
            PhotoLasso pl = new PhotoLasso();
            List<PhotoLassoInfo> infolist = pl.UserInfoList(CU.UserID, true);
            DataTable dt = new DataTable();
            dt.Columns.Add("d1", typeof(string));
            if (QuangCount == 0)
            {
                QuangCount = infolist.Count;
            }
            if (infolist.Count < 1)
            {
                myh.Visible = false;
            }
            for (int i = 0; i < infolist.Count; i++)
            {
                DataRow dr = dt.NewRow();
                PhotoInfo pi = getPhotoInfo(infolist[i].PhotoID);
                string imgPath = this.GetSmallPic(pi.FilePath, 2);
                if (pi.PhotoType == 0)
                {
                    imgPath = this.GetHeadImage(pi.UploadUser,3);
                }
                dr["d1"] = "<div class=\"quanofablum\"><a href=\"Photoview.aspx?r[0]=" + infolist[i].PhotoID + "\">"
                    + "<img src=\"" + imgPath + "\" alt=\"\" /></a></div>";
                dt.Rows.Add(dr);
            }
            selfTag.RepeatColumns = 4;
            selfTag.RepeatDirection = RepeatDirection.Vertical;
            selfTag.DataSource = dt;
            selfTag.DataBind();
        }
        /// <summary>
        /// 在别人的相册中被圈的照片
        /// </summary>
        protected void bindDataOtherTag()
        {
            PhotoLasso pl = new PhotoLasso();
            List<PhotoLassoInfo> infolist = pl.UserInfoList(CU.UserID, false);
            DataTable dt = new DataTable();
            if (QuangCount == 0)
            {
                QuangCount = infolist.Count;
            }
            if (infolist.Count < 1)
            {
                otherh.Visible = false;
            }
            dt.Columns.Add("d1", typeof(string));
            for (int i = 0; i < infolist.Count; i++)
            {
                DataRow dr = dt.NewRow();
                PhotoInfo pi = getPhotoInfo(infolist[i].PhotoID);
                string imgPath = this.GetSmallPic(pi.FilePath, 2);
                if (pi.PhotoType == 0)
                {
                    imgPath = Public.GetSmallHeadPic(pi.FilePath, 3);
                }
                dr["d1"] = "<div class=\"quanofablum\"><a href=\"Photoview.aspx?r[0]=" + infolist[i].PhotoID + "\">"
                    + "<img src=\"" + imgPath + "\" /></a></div>";
                dt.Rows.Add(dr);
            }

            otherTag.RepeatColumns = 4;
            otherTag.RepeatDirection = RepeatDirection.Vertical;
            otherTag.DataSource = dt;
            otherTag.DataBind();
        }
    }
}
