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 System.Reflection;
using JuSNS.ApiInterface;
using JuSNS.Model.ApiInterface;
using System.Collections.Generic;
using System.Xml;
using System.Text;

namespace JuSNS.Web.appserver
{
    public partial class SNotification : System.Web.UI.Page
    {
        /// <summary>
        /// appkey
        /// </summary>
        protected string appkey = string.Empty;
        /// <summary>
        /// 请求类型
        /// </summary>
        protected string action = string.Empty;
        /// <summary>
        /// 参数
        /// </summary>
        protected string param = string.Empty;
        /// <summary>
        /// 应用程序ID
        /// </summary>
        protected int AppID = 0;
        protected string XmlPath = "./xml/Notice.xml";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.init();
            MethodInfo methodInfo;

            methodInfo = this.GetType().GetMethod(action, BindingFlags.NonPublic | BindingFlags.Instance);

            if (methodInfo == null)
            {
                Response.Write((int)EnumErr.ErrRequestType);
                Response.End();
            }
            methodInfo.Invoke(this, null);
        }
        /// <summary>
        /// 初始化
        /// </summary>
        protected void init()
        {
            appkey = Request.Form["appkey"];
            action = Request.Form["action"];
            param = Request.Form["param"];
            bool valid = App.VerifyKey(appkey, out AppID);
            if (!valid)
            {
                Response.Write((int)EnumErr.ErrKey);
                Response.End();
            }
            if (string.IsNullOrEmpty(action))
            {
                Response.Write((int)EnumErr.ErrRequestType);
                Response.End();
            }
        }
        /// <summary>
        /// 发送通知
        /// </summary>
        protected void Send()
        {
            NoticeInfo info = new NoticeInfo();
            string[] str=param.Split(',');
            if (str.Length != 4)
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            int sender = 0;
            int receiver = 0;
            string notice = string.Empty;
            string postip = string.Empty;
            DateTime posttime = DateTime.Now;
            int.TryParse(str[0], out sender);
            int.TryParse(str[1], out receiver);
            notice = str[2];
            postip = str[3];
            info.AppID = this.AppID;
            info.Sender = sender;
            info.Receiver = receiver;
            info.PostIP = postip;
            info.Notice = notice;
            info.PostTime = posttime;
            int n = Notification.Send(info);
            Response.Write(n);
        }
        /// <summary>
        /// 取得通知
        /// </summary>
        protected void GetNotice()
        {
            int UserID = 0;
            if (!int.TryParse(param, out UserID))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            List<NoticeInfo> infolist = Notification.GetNotice(UserID, this.AppID);
            writeXml(infolist);
        }

        private void writeXml(List<NoticeInfo> infolist)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(Server.MapPath(XmlPath));
            XmlNode pNode = xDoc.SelectSingleNode("/root");
            XmlNode xNode = xDoc.SelectSingleNode("/root/Row");
            for (int i = 0; i < infolist.Count; i++)
            {
                XmlNode node = xNode.Clone();
                XmlNodeList nodelist = node.ChildNodes;
                NoticeInfo info = infolist[i];

                PropertyInfo[] mInfo = info.GetType().GetProperties();
                foreach (PropertyInfo pi in mInfo)
                {
                    XmlNode N = node.SelectSingleNode(pi.Name);
                    object tobj = pi.GetValue(info, null);
                    if (tobj == null)
                    {
                        N.InnerText = "";
                    }
                    else
                    {
                        N.InnerText = tobj.ToString();
                    }
                }
                 
                pNode.AppendChild(node);
            }
            pNode.RemoveChild(pNode.ChildNodes[0]);
            StringBuilder sb = new StringBuilder();
            XmlWriter xw = XmlWriter.Create(sb);
            xDoc.WriteTo(xw);
            xw.Flush();
            Response.Write(sb.ToString());
        }
    }
}
