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

namespace JuSNS.Web.home.poke
{
    public partial class index : JuSNS.UI.Page.UserPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string a = Request.QueryString["a"];
            if (a == "save")
            {
                if (this.ReturnIsNotEmail() != string.Empty)
                {
                    Response.Write("<span class=\"font14\">您需要激活电子邮件后才能进行此操作。</span><a href=\"" + rooDir + "/EmailActive.aspx?r=" + this.UserID.ToString() + "\">点这里激活邮箱</a>");
                    Response.End();
                }
                JuSNS.Home.JUser ju = new JuSNS.Home.JUser();
                JuSNS.Home.User us = new JuSNS.Home.User();
                string rID = Request.QueryString["rID"];
                string ptype = Request.QueryString["ptype"];
                string pokefrom = Request.QueryString["pokefrom"];
                string poketo = Request.QueryString["poketo"];
                string isPub = Request.QueryString["isPub"];
                string liid = Request.QueryString["liid"];
                if (!JuSNS.Common.Input.IsInteger(rID))
                {
                    Response.Write("<span class=\"reshow\">选择要发送的好友！</span>");
                    Response.End();
                }

                if (string.IsNullOrEmpty(ptype))
                {
                    Response.Write("<span class=\"reshow\">请选择表情！</span>");
                    Response.End();
                }

                if (ptype != "-1")
                {
                    if (!JuSNS.Common.Input.IsInteger(ptype))
                    {
                        Response.Write("<span class=\"reshow\">请选择表情！</span>");
                        Response.End();
                    }
                }

                if (ptype == "-1")
                {
                    if (string.IsNullOrEmpty(pokefrom))
                    {
                        Response.Write("<span class=\"reshow\">请填写自定义表情！</span>");
                        Response.End();
                    }
                    if (string.IsNullOrEmpty(poketo))
                    {
                        poketo = string.Empty;
                    }
                }
                int gN = ju.AddPoke(UserID, Convert.ToInt32(rID), Convert.ToInt16(ptype), Convert.ToInt16(isPub), poketo, pokefrom);
                if (gN == -100)
                {
                    Response.Write("<span class=\"reshow\">你发送的动作超过" + JuSNS.Common.Public.GetXMLValue("pokeNumber") + "个记录！明天再继续吧！</span>");
                    Response.End();
                }
                else
                {
                    if (gN > 0)
                    {
                        //这里开始插入通知和动态
                        //是否首页回复。
                        if (JuSNS.Common.Input.IsInteger(liid))
                        {
                            int j = ju.ChanagePokeState(Convert.ToInt32(liid), this.UserID);
                        }



                        Response.Write("succ");
                        Response.End();
                    }
                    else
                    {
                        Response.Write("<span class=\"reshow\">发送失败！</span>");
                        Response.End();
                    }
                }
            }
            else if (a == "del")
            {
                JuSNS.Home.JUser jud = new JuSNS.Home.JUser();
                string rID = Request.QueryString["ID"];
                if (jud.DelPoke(Convert.ToInt32(rID), UserID) > 0)
                {
                    Response.Redirect("../poke/?r=my");
                }
            }
            else
            {
                string r = Request.QueryString["r"];
                if (r == "my")
                {
                    this.mpoke.Attributes.Add("class", "current");
                    this.npoke.Attributes.Add("class", "");
                    this.defaultDiv.Visible = false;
                    this.myPokeDiv.Visible = true;
                    BindData();
                }
                else
                {
                    this.mpoke.Attributes.Add("class", "");
                    this.npoke.Attributes.Add("class", "current");
                    this.defaultDiv.Visible = true;
                    this.myPokeDiv.Visible = false;
                }
            }
            bindPoke();
            DataBind();
        }

        /// <summary>
        /// 分页绑定数据
        /// </summary>
        protected void BindData()
        {
            int ReCount, PgCount;
            int PageIndex = 1;
            if (Request.QueryString["Page"] != null)
            {
                PageIndex = Convert.ToInt32(Request.QueryString["Page"]);
            }
            DataTable dt = UtilPage.GetPage("Poke_My_aspx", PageIndex, 20, out ReCount, out PgCount, new SqlConditionInfo("@UserID", UserID, TypeCode.Int32));
            string PageUrl = "../poke/?r=my";
            DataTable data = new DataTable();
            data.Columns.Add("d", typeof(string));
            JuSNS.Home.User u = new JuSNS.Home.User();
            if (ReCount > 0)
            {
                this.nopoke1.Visible = true;
                this.nopoke1.InnerHtml = ReCount + "个动作表情<span class=\"pipe\">|</span><a href=\"../poke\">给朋友打招呼</a>";
            }
            else
            {
                this.nopoke1.Visible = false;
            }

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                StringBuilder sb = new StringBuilder();
                DataRow dr = data.NewRow();
                UserInfo mdl = new UserInfo();
                UserInfo mdl1 = new UserInfo();
                mdl = u.GetUserInfo(Convert.ToInt32(dt.Rows[i]["UserID"].ToString()));
                mdl1 = u.GetUserInfo(Convert.ToInt32(dt.Rows[i]["FriendID"].ToString()));
                sb.Append("<div class=\"content-poke\">");
                sb.Append("<div class=\"imgs image\"><a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", dt.Rows[i]["UserID"].ToString()) + "\"><img src=\"" + GetHeadImage(Convert.ToInt32(dt.Rows[i]["UserID"].ToString()), 1) + "\" align=\"absmiddle\" /></a></div>\n");
                string gPT = string.Empty;
                if (dt.Rows[i]["gType"].ToString() == "-1")
                {
                    gPT = "_1";
                }
                else
                {
                    gPT = dt.Rows[i]["gType"].ToString();
                }
                sb.Append("<div class=\"cont\"><img align=\"absmiddle\" src=\"../../template/" + JuSNS.Config.UiConfig.SkinStyle + "/images/poke/pokeact_" + gPT + ".gif\"/> <a href=\"../../user/" + JuSNS.Common.Public.URLWrite("", "r", dt.Rows[i]["UserID"].ToString()) + "\">" + mdl.Username + "</a> " + JuSNS.Common.Public.GetAction(Convert.ToInt32(dt.Rows[i]["gType"].ToString()), dt.Rows[i]["pokefrom"].ToString(), dt.Rows[i]["poketo"].ToString(), dt.Rows[i]["isPub"].ToString()) + " <span class=\"time\">" + JuSNS.Common.Public.getTimeEXSpan(DateTime.Parse(dt.Rows[i]["PostTime"].ToString())) + "</span></div>\n");
                sb.Append("<div class=\"replay\"><a href=\"javascript:void(0);\" onclick=\"SendPoke('" + dt.Rows[i]["UserID"].ToString() + "','" + mdl.Username + "','" + GetHeadImage(Convert.ToInt32(dt.Rows[i]["UserID"].ToString()), 2) + "','')\">回应他(她)</a></div>\n");
                sb.Append("<div class=\"del\"><span class=\"delete\"><a href=\"javascript:void(0);\" onclick=\"javascript:Confirm(this,'<ul><li>确定要删除吗？</li></ul>','删除招呼表情',function(){window.location.href='index.aspx?r=my&a=del&id=" + dt.Rows[i]["ID"].ToString() + "'})\">删除</a></span></div>\n");
                sb.Append("</div>\n");
                dr["d"] = sb.ToString();
                data.Rows.Add(dr);
            }
            this.MyPokeList.Visible = true;
            this.MyPokeList.DataSource = data;
            this.MyPokeList.DataBind();
            PageUrl += "?";
            if (ReCount > 20)
            {
                this.DivPage.InnerHtml = PageNavigator.Pagination(PageIndex, PgCount, PageUrl);
            }
            else
            {
                this.DivPage.Visible = false;
            }
        }
        /// <summary>
        /// 绑定表情
        /// </summary>
        protected void bindPoke()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Number", typeof(int));
            dt.Columns.Add("Text", typeof(string));
            dt.Columns.Add("more", typeof(string));
            foreach (KeyValuePair<int, PokeActionInfo> kv in PokeConfig.Config)
            {
                DataRow dr = dt.NewRow();
                dr["Number"] = kv.Key;
                dr["Text"] = kv.Value.Value;
                dr["more"] = kv.Value.More;
                dt.Rows.Add(dr);
            }
            this.pokestyle.DataSource = dt;
            this.pokestyle.DataBind();
            dt.Dispose();
        }
    }
}
