using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
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;

namespace JuSNS.Web.Inc.Ajax
{
    public partial class PhotoOperator : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string action = Request.QueryString["action"];
            switch (action.ToLower())
            {
                case "del": //删除照片
                    Delete();
                    break;
                case "quan": //圈人
                    Quanren();
                    break;
                case "quanquanbox": //显示圈人框
                    quanquanBox();
                    break;
                case "plsodel": //删除圈圈
                    plsoDel();
                    break;
                case "commentadd": //添加评论
                    CommentAdd();
                    break;
                case "recomment":  //回复评论
                    ReComment();
                    break;
                case "delcomment": //删除评论
                    DelComment();
                    break;
                case "commentlist": //显示回复的评论js
                    CommentList();
                    break;
                default:
                    break;
            }
        }


        protected void Delete()
        {
            string r = Input.Filter(Request.QueryString["r"]);
            Photo ph = new Photo();
            PhotoInfo Phi = ph.Info(Convert.ToInt32(r));
            int suc = 0;
            if (this.IsAdmin())
            {
                suc = ph.Del(Phi.PhotoID);
            }
            else
            {
                suc = ph.Del(Convert.ToInt32(r), this.UserID);
            }
            if (suc != 1)
            {
                Response.Write("alert('删除失败');");
            }
        }

        protected void Quanren()
        {
            string r = Input.Filter(Request.QueryString["r"]);
            string x = Input.Filter(Request.QueryString["x"]);
            string y = Input.Filter(Request.QueryString["y"]);
            string w = Input.Filter(Request.QueryString["w"]);
            string h = Input.Filter(Request.QueryString["h"]);
            string u = Input.Filter(Request.QueryString["u"]);
            string n = Input.Filter(Request.QueryString["n"]);
            string errMsg = "1";


            if (string.IsNullOrEmpty(r))
            {
                errMsg = "提交的参数不正确";
            }
            if (string.IsNullOrEmpty(u))
            {
                u = this.UserID + "";
            }
            if (string.IsNullOrEmpty(n))
            {
                n = this.UserID + "";
            }
            if (errMsg == "1")
            {
                PhotoLassoInfo plsi = new PhotoLassoInfo();
                plsi.Description = "";
                plsi.FriendID = Convert.ToInt32(n);
                plsi.IsLock = EnumIsLock.UnLock;
                plsi.PhotoID = Convert.ToInt32(r);
                plsi.PostIP = Public.GetClientIP();
                plsi.PostTime = DateTime.Now;
                plsi.PosX = Convert.ToInt32(x);
                plsi.PosY = Convert.ToInt32(y);
                plsi.PosW = Convert.ToInt32(w);
                plsi.PosH = Convert.ToInt32(h);
                plsi.UserID = this.UserID;

                PhotoLasso pls = new PhotoLasso();

                errMsg = pls.Add(plsi).ToString();
            }
            Response.Write(errMsg);
        }

        protected void quanquanBox()
        {
            string r = Input.Filter(Request.QueryString["r"]);
            string x = Input.Filter(Request.QueryString["x"]);
            string y = Input.Filter(Request.QueryString["y"]);
            string u = Input.Filter(Request.QueryString["u"]);
            string w = Input.Filter(Request.QueryString["w"]);
            string h = Input.Filter(Request.QueryString["h"]);
            Friend frd = new Friend();
            Dictionary<int, string> friendlist = frd.GetFriends(this.UserID, "");
            StringBuilder sb = new StringBuilder();
            sb.Append("<form action=\""+rooDir+"/inc/ajax/album/PhotoOperator.aspx?action=quan\" method=\"get\" name=\"quanren\">");
            sb.Append("<div style=\"text-align:left\">输入名字查找:</div>");
            sb.Append("<input type=\"text\" style=\"width:100%\" name=\"name\" oninput=\"Album.Photo.quanrenformSearch();\" onpropertychange=\"Album.Photo.quanrenformSearch();\" value=\"\"></input>");
            sdiv(ref sb, this.UserID.ToString(), this.UserName);

            sb.Append("<div style=\"text-align:left\">或者选择:</div>");
            sb.Append("<hr style=\"color:#dddddd;height:1px;\" />");

            foreach (KeyValuePair<int, string> kvp in friendlist)
            {
                sdiv(ref sb, kvp.Key.ToString(), kvp.Value);
            }
            sb.Append("<input type=\"hidden\" name=\"r\" value=\"" + r + "\" />");
            sb.Append("<input type=\"hidden\" name=\"x\" value=\"" + x + "\" />");
            sb.Append("<input type=\"hidden\" name=\"y\" value=\"" + y + "\" />");
            sb.Append("<input type=\"hidden\" name=\"w\" value=\"" + w + "\" />");
            sb.Append("<input type=\"hidden\" name=\"h\" value=\"" + h + "\" />");
            sb.Append("<input type=\"hidden\" name=\"u\" value=\"" + u + "\" />");
            sb.Append("<input type=\"hidden\" name=\"n\" value=\"\" />");
            sb.Append("<input type=\"button\" name=\"submitbutton\" class=\"f-button\" style=\"width:50px\"  onclick=\"Album.Photo.quanrenformSubmit();\" value=\"确定\" />");
            sb.Append("&nbsp;&nbsp;");
            sb.Append("<input type=\"button\" name=\"canclebutton\" class=\"f-button f-alt\" style=\"width:50px\" onclick=\"Album.Photo.quanrenformCancle();\" value=\"取消\" />");
            sb.Append("</form>");
            Response.Write(sb.ToString());
        }


        protected void sdiv(ref StringBuilder sb, string kkey, string kvalue)
        {
            string div1 = "<div style=\"text-align:left;height:20px;background-color:#ffffff;\" onmouseover=\"this.style.backgroundColor='#dddddd';\" onmouseout=\"this.style.backgroundColor='#ffffff';\">";
            string div2 = "</div>";
            sb.Append(div1 + "<input type=\"checkbox\" name=\"quanren_friend\" onclick=\"Album.Photo.quanrenformSelect();\" key=\"" + kkey + "\" value=\"" + kvalue + "\">" + kvalue + div2);
        }


        protected void plsoDel()
        {
            string r = Input.Filter(Request.QueryString["r"]);
            PhotoLasso pl = new PhotoLasso();
            PhotoLassoInfo pli = pl.Info(Convert.ToInt32(r));
            int rint = 0;
            if (this.IsAdmin())
            {
                //管理员删除圈人
                rint = pl.Del(Convert.ToInt32(r), pli.UserID, pli.FriendID);
            }
            else
            {
                //用户删除
                rint = pl.Del(Convert.ToInt32(r), this.UserID, pli.FriendID);
            }
            if (rint == 1)
            {
                Response.Write("setConfirmMSG(\"删除成功\");");
            }
            else
            {
                Response.Write("setConfirmMSG(\"删除失败\");setTimeout('closeConfirm()',500);return;");
            }
        }

        protected void CommentAdd()
        {
            string Content = Request.Form["Content"];
            Content = Common.Input.loseScript(Content);
            Content = Common.Input.loseIframe(Content);
            string PhotoID = Input.Filter(Request.Form["PhotoID"]);
            if (string.IsNullOrEmpty(PhotoID))
            {
                Response.Write("setFloatBox(\"照片参数不正确评论失败\");setTimeout(\"closeFloatBox();\",500)");
                Response.End();
                return;
            }
            if (string.IsNullOrEmpty(Content))
            {
                Response.Write("setFloatBox(\"评论内容为空失败\");setTimeout(\"closeFloatBox();\",500)");
                Response.End();
                return;
            }
            #region 过滤关键字
            string res = JuSNS.Common.Public.CheckKey(Content, true);
            if (res != "")
            {
                Content = Content.Replace(res, "***");
            }
            #endregion
            PhotoCommentInfo pci = new PhotoCommentInfo();
            pci.Content = Content;
            pci.UserID = this.UserID;
            pci.PhotoID = Convert.ToInt32(PhotoID);
            pci.PostIP = Public.GetClientIP();
            pci.PostTime = DateTime.Now;
            pci.IsLock = EnumIsLock.UnLock;
            pci.CommentID = 0;
            PhotoComment pc = new PhotoComment();
            pc.Add(pci,UserID);
            Response.Write("setFloatBox(\"评论成功\");setTimeout(\"closeFloatBox();location.replace('/home/Album/photoview.aspx?r[0]=" + PhotoID + "')\",1000)");
            Response.End();
        }

        protected void ReComment()
        {
            string Content = Request.Form["Content"];
            Content = Common.Input.loseScript(Content);
            Content = Common.Input.loseIframe(Content);
            string CommentID = Input.Filter(Request.Form["CommentID"]);
            if (string.IsNullOrEmpty(CommentID))
            {
                Response.Write("setFloatBox(\"回复参数不正确评论失败\");setTimeout(\"closeFloatBox();\",500)");
                Response.End();
                return;
            }
            if (string.IsNullOrEmpty(Content))
            {
                Response.Write("setFloatBox(\"回复内容为空失败\");setTimeout(\"closeFloatBox();\",500)");
                Response.End();
                return;
            }
            #region 过滤关键字
            string res = JuSNS.Common.Public.CheckKey(Content, true);
            if (res != "")
            {
                Content = Content.Replace(res,"***");
            }
            #endregion
            PhotoComment pc = new PhotoComment();
            PhotoCommentInfo pca = pc.Info(Convert.ToInt32(CommentID));
            if (pca.UserID == this.UserID)
            {
                Response.Write("setFloatBox(\"不能回复自己的评论\");setTimeout(\"closeFloatBox();\",500)");
                Response.End();
                return;
            }
            PhotoCommentInfo pci = new PhotoCommentInfo();
            pci.Content = Content;
            pci.UserID = this.UserID;
            pci.PhotoID = 0;
            pci.PostIP = Public.GetClientIP();
            pci.PostTime = DateTime.Now;
            pci.IsLock = EnumIsLock.UnLock;
            pci.CommentID = Convert.ToInt32(CommentID);

            pc.Add(pci,UserID);
            Response.Write("setFloatBox(\"评论成功\");setTimeout(\"closeFloatBox();location.reload()\",500)");
            Response.End();
        }

        protected void CommentList()
        {
            string CommentID = Input.Filter(Request.QueryString["CommentID"]);
            if (string.IsNullOrEmpty(CommentID))
            {
                Response.End();
                return;
            }

            PhotoComment comm = new PhotoComment();
            List<PhotoCommentInfo> comments = comm.CommentList(Convert.ToInt32(CommentID));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < comments.Count; i++)
            {
                Home.User u = new Home.User();
                UserInfo ui = u.GetUserInfo(comments[i].UserID);
                Dict d = new Dict();
                string provi = d.GetAreaName(ui.Province);
                sb.Append("<div id=\"comment-" + comments[i].ID + "\">");
                sb.Append("<div class=\"post parent-post\">");
                sb.Append("<p class=\"image\">");
                sb.Append("<a href=\""+rooDir+"/user/?r=" + comments[i].UserID + "\">");
                sb.Append("<img src=\"" + this.GetHeadImage(comments[i].UserID) + "\" /></a></p>");
                sb.Append("<div class=\"info\">");
                sb.Append("<span class=\"author\"><a href=\""+rooDir+"/user?r=" + comments[i].UserID + "\">" + ui.Username + "</a> (" + provi + ")</span>");
                sb.Append("<span class=\"time\">" + Public.getTimeSpan(comments[i].PostTime) + "</span>");
                sb.Append("<span class=\"actions\"><a href=\"javascript:void(0)\" rev=\"ReComment\" rel=\""+rooDir+"/inc/ajax/album/PhotoOperator.aspx?action=recomment&commentid=" + comments[i].ID + "\" class=\"thread-reply\">回复</a></span>");
                if (this.UserID == comments[i].UserID || this.IsAdmin())
                {
                    sb.Append("<span class=\"actions\"><a href=\"javascript:void(0)\" rev=\"DelComment\" rel=\""+rooDir+"/inc/ajax/album/PhotoOperator.aspx?action=delcomment&commentid=" + comments[i].ID + "\" class=\"thread-reply\">删除</a></span>");
                }
                sb.Append("</div>");

                sb.Append("<div class=\"content\">");
                sb.Append(comments[i].Content);
                sb.Append("<script src=\""+rooDir+"/inc/ajax/album/photooperator.aspx?action=commentlist&commentid=" + comments[i].ID + "\" type=\"text/javascript\"></script>");
                sb.Append("</div>");
                sb.Append("</div>");
                sb.Append("</div>");
            }
            Response.Write("document.write('" + sb.ToString() + "');");
            //Response.Write("EventLoad.Reload();");
        }

        protected void DelComment()
        {
            string CommentID = Input.Filter(Request.QueryString["CommentID"]);
            if (string.IsNullOrEmpty(CommentID))
            {
                Response.End();
                return;
            }
            PhotoComment ptc = new PhotoComment();
            int secd = 0;
            if (this.IsAdmin())
            {
                secd = ptc.Del(int.Parse(CommentID));
            }
            else
            {
                secd = ptc.Del(int.Parse(CommentID), this.UserID);
            }
            if (secd == 1)
            {
                Response.Write("setFloatBox(\"删除评论成功\");setTimeout(\"closeConfirm();location.reload();\",500)");
                Response.End();
            }
            else
            {
                Response.Write("setFloatBox(\"删除失败\");setTimeout(\"closeConfirm();\",500)");
                Response.End();
            }
        }
    }
}
