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

namespace JuSNS.Web.user.share
{
    public partial class index : JuSNS.UI.Page.BasePage
    {
        protected int myid;
        protected string CurrentCSS = string.Empty;
        protected string CurrentCSS1 = string.Empty;
        protected string CurrentCSS2 = string.Empty;
        public bool isMy = false;
        public string sUserName = string.Empty;
        protected string checkstr = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.GetUserID() == 0)
            {
                checkstr = " onclick=\"javascript:{unlogin();return false}\"";
            }
            string s_action = Request.QueryString["action"];
            if (s_action != string.Empty&&s_action!=null)
            {
                int Id = Convert.ToInt32(Request.QueryString["Id"]);
                int uId = Convert.ToInt32(Request.QueryString["uid"]);
                Share sd = new Share();
                if (sd.Del(Id, uId) > 0)
                {
                    Response.Write("成功！");
                    Response.End();
                }
            }
            else
            {
                string r = Request.QueryString["r"];
                if (!string.IsNullOrEmpty(r))
                {
                    if (JuSNS.Common.Input.IsInteger(r))
                    {
                        if (!this.PrivacyAvailable(EnumPrivacyType.Share, GetUserID(), Convert.ToInt32(r)))
                        {
                            PageError("用户的隐私设置，你无法查看", "javascript:history.back();");
                        }
                        CurrentCSS = " class=\"current\"";
                        CurrentCSS1 = "";
                        CurrentCSS2 = "";
                        isMy = false;
                        if (r != "0")
                        {
                            User us = new User();
                            UserInfo uinfo = new UserInfo();
                            uinfo = us.GetUserInfo(Convert.ToInt32(r));
                            sUserName = uinfo.Username + "的";
                        }
                        else
                        {
                            sUserName = string.Empty;
                        }
                    }
                    else
                    {
                        CurrentCSS = "";
                        CurrentCSS1 = "";
                        CurrentCSS2 = " class=\"current\"";
                        isMy = false;
                        sUserName = string.Empty;
                    }
                }
                else
                {
                    isMy = true;
                    CurrentCSS = "";
                    CurrentCSS2 = "";
                    CurrentCSS1 = " class=\"current\"";
                    sUserName = string.Empty;
                }
                BindData(); DataBind();
            }
        }

        /// <summary>
        /// 分页绑定数据
        /// </summary>
        protected void BindData()
        {
            int ReCount, PgCount;
            int PageIndex = 1;
            if (JuSNS.Common.Input.IsInteger(Request.QueryString["Page"]))
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            DataTable dt = new DataTable();
            SqlConditionInfo[] sc = null;
            string r = Request.QueryString["r"];
            if (r == "all")
            {
                this.ptitle.InnerText = "所有人的分享";
                sc = new SqlConditionInfo[] { new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32) };
                dt = UtilPage.GetPage("User_share_index_all", PageIndex, PAGESZ, out ReCount, out PgCount, sc);
            }
            else
            {
                if (JuSNS.Common.Input.IsInteger(r))
                {
                    this.ptitle.InnerText = "朋友的分享";
                    if (r == "0")
                    {
                        sc = new SqlConditionInfo[] { new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32) };
                        dt = UtilPage.GetPage("User_share_index_Friend", PageIndex, PAGESZ, out ReCount, out PgCount, sc);
                    }
                    else
                    {
                        sc = new SqlConditionInfo[] { new SqlConditionInfo("@UserID", Convert.ToInt32(r), TypeCode.Int32) };
                        dt = UtilPage.GetPage("User_share_index_UserID", PageIndex, PAGESZ, out ReCount, out PgCount, sc);
                    }
                }
                else
                {
                    this.ptitle.InnerText = "我的分享";
                    sc = new SqlConditionInfo[] { new SqlConditionInfo("@UserID", GetUserID(), TypeCode.Int32) };
                    dt = UtilPage.GetPage("User_share_index_my", PageIndex, PAGESZ, out ReCount, out PgCount, sc);
                }
            }
            this.RptData.DataSource = dt;
            this.RptData.DataBind();
            this.LtrCount.Text = ReCount.ToString();
            if (ReCount > PAGESZ)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, "index.aspx?r=" + Request.QueryString["r"] + "");
            }
        }

        protected string GetURL(object ID, object ShareID, object UserID, object ShareType) 
        {
            return string.Empty;
        }

        protected string GetLinkCaseID(object ID, object caseID, object type)
        {
            if (type.ToString() == "1")
            {
                return ID.ToString();
            }
            else
            {
                return caseID.ToString();
            }
        }

        /// <summary>
        /// 得到分享分类别描述
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        protected string GetShareDesc(object shareid,object sharetype,object sharedesc,object shareurl,object caseid)
        {
            Share sd = new Share();
            string TmpshareDesc = sharedesc.ToString();
            switch (sharetype.ToString())
            {
                case "1":
                    TmpshareDesc = "<span style=\"color:#999999;font-size:11.5px;\">" + shareurl + "</span><br />" + sharedesc.ToString();
                    break;
                case "2":
                    TmpshareDesc = sharedesc.ToString();
                    break;
                case "3":
                    TmpshareDesc = sd.AlbumCount(int.Parse(caseid.ToString())) + "张照片<br />" + sharedesc.ToString();
                    break;
                case "4":
                    TmpshareDesc = sharedesc.ToString();
                    TmpshareDesc += "<br /><a href=\"links.aspx?type=" + sharetype.ToString() + "&id=" +GetLinkCaseID(shareid,caseid,sharetype)+ "\">阅读全文</a>";
                    break;
                case "5":
                    TmpshareDesc = sharedesc.ToString();
                    break;
                case "6":
                    TmpshareDesc = sharedesc.ToString();
                    break;
                case "7":
                    TmpshareDesc = sharedesc.ToString();
                    break;
                case "8":
                    TmpshareDesc = sharedesc.ToString();
                    break;
                default:
                    TmpshareDesc = sharedesc.ToString();
                    break;
            }
            return TmpshareDesc;
        }

        /// <summary>
        /// 得到图片，视频，抑或为空
        /// </summary>
        /// <param name="shareid">ID</param>
        /// <param name="sharetype">类型</param>
        /// <returns></returns>
        protected string GetSharePicStyle(object shareid, object sharetype,object userid,object caseid)
        {
            string picstr = string.Empty;
            Share sd =new Share();
            switch (sharetype.ToString())
            { 
                case "3":
                    Home.Album ab = new Home.Album();
                    string TmpStr = this.GetSmallPic(ab.CoverPath(Convert.ToInt32(caseid.ToString())), 1);
                    if (!string.IsNullOrEmpty(TmpStr))
                    {
                        picstr = "<div class=\"nl image\" style=\"margin-right:10px;\"><a title=\"相册\" href=\"links.aspx?Id=" + caseid.ToString() + "&type=" + sharetype.ToString() + "&userid=" + userid.ToString() + "\"><img src=\"" + TmpStr + "\" style=\"width:100px;\"  align=\"left\"/></a></div>";
                    }
                    else
                    {
                        picstr = "<div class=\"nl image\" style=\"margin-right:10px;\"><a title=\"相册\" href=\"links.aspx?Id=" + caseid.ToString() + "&type=" + sharetype.ToString() + "&userid=" + userid.ToString() + "\"><img src=\"" + rooDir + "/template/" + JuSNS.Config.UiConfig.SkinStyle + "/images/000.jpg\"  align=\"left\" /></a></div>";
                    }
                    break;
                case"2":
                    string TMPpicstr = sd.PhotoURL(Convert.ToInt32(caseid.ToString()));
                    picstr = "<div class=\"nl image\" style=\"margin-right:10px;\"><a title=\"照片\" href=\"links.aspx?Id=" + caseid.ToString() + "&type=" + sharetype.ToString() + "&userid=" + userid.ToString() + "\"><img src=\"" + GetSmallPic(TMPpicstr, 1) + "\" style=\"width:100px;\"  align=\"left\"/></a></div>";
                    break;
                case "5":
                    picstr = "<div class=\"nl image\" style=\"margin-right:10px;\"><a title=\"视频\" href=\"javascript:void(0);\" class=\"share-content-vthumb\"><img src=\"" + rooDir + "/template/images/vd.gif\"  align=\"absmiddle\" /></a></div>";
                    break;
                case "6":
                    picstr = "<div class=\"nl\" style=\"margin-right:10px;\"><a href=\"../../user/?r=" + caseid.ToString() + "\"><img src=\"" + GetHeadImage(Convert.ToInt32(caseid.ToString()), 2) + "\" style=\"width:100px;\" align=\"absmiddle\" /></a></div>";
                    break;
            }
            return picstr;
        }

        /// <summary>
        /// 获得用户名（昵称）
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        protected string GetUserName(object userid)
        {
            JuSNS.Home.User user = new JuSNS.Home.User();
            UserInfo userinfo = new UserInfo();
            userinfo = user.GetUserInfo(Convert.ToInt32(userid));
            string getUsername = userinfo.Username;
            return getUsername;
        }

        /// <summary>
        /// 得到头像
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        protected string GetUserHeadPic(object userid)
        {
            return "<img src=\"" + GetHeadImage(Convert.ToInt32(userid.ToString())) + "\" style=\"width:48px;height:48px;\" /></a>";
        }

        /// <summary>
        /// 得到格式化后的时间
        /// </summary>
        /// <param name="PostTime">分享时间</param>
        /// <returns></returns>
        protected string GetSharePostTime(object PostTime)
        {
            if (JuSNS.Common.Input.IsDate(PostTime.ToString()))
            {
                return JuSNS.Common.Public.getTimeSpan(DateTime.Parse(PostTime.ToString()));
            }
            else
            {
                return string.Empty;
            }
        }
        /// <summary>
        /// 分享类型
        /// </summary>
        /// <param name="sharetype">传入的标志</param>
        /// <returns>分享的类型(中文)</returns>
        protected string GetUserShareSingleType(object sharetype)
        {
            string returnstr = string.Empty;
            string s_ShareType = sharetype.ToString();
            switch (s_ShareType)
            {
                case "1":
                    returnstr = "网页";
                    break;
                case "2":
                    returnstr = "照片";
                    break;
                case "3":
                    returnstr = "相册";
                    break;
                case "4":
                    returnstr = "日志";
                    break;
                case "5":
                    returnstr = "视频";
                    break;
                case "6":
                    returnstr = "朋友";
                    break;
                case "7":
                    returnstr = "新闻";
                    break;
                default:
                    returnstr = "其他";
                    break;
            }
            return returnstr;
        }

        protected string GetShareCommentCount(object userid, object shareid)
        {
            string shareCommentStr = "没有评论";
            Share si = new Share();
            int gCount = si.CommentsCount(int.Parse(shareid.ToString()));
            if (gCount > 0)
            {
                shareCommentStr = "<a href=\"javascript:void(0);\" onclick=\"showCommentContent('" + shareid.ToString() + "');\">共" + gCount + "个评论</a>";
            }
            return shareCommentStr;
        }


        protected string GetOperType(object id,object userid)
        {
            JuSNS.Home.Share ShareD = new JuSNS.Home.Share();
            ShareInfo sinfo = new ShareInfo();
            sinfo = ShareD.Info(Convert.ToInt32(id.ToString()));
            if (GetUserID().ToString() != userid.ToString())
            {
                return "<span  class=\"share\"><a href=\"javascript:;\" onclick=\"javascript:ShowShareDiv('" + (int)sinfo.ShareType + "','" + sinfo.ID + "','" + sinfo.Title + "','" + sinfo.Url + "','" + sinfo.Description + "','" + sinfo.UserID + "');\" >分享</a></span>";
            }
            else
            {
                return "<span  class=\"delete\"><a href=\"javascript:;\" onclick=\"Confirm(this,'确定删除共享吗？','删除',function(){DelShareID('" + id.ToString() + "','" + userid.ToString() + "')});\" >删除</a></span>";
            }
        }
    }
}
