using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Xml;

namespace JuSNS.AppClient
{
    /// <summary>
    /// 通知类
    /// </summary>
    public class Notification
    {
        /// <summary>
        /// 发送通知
        /// </summary>
        /// <param name="info">通知信息</param>
        /// <returns></returns>
        static public int Send(NoticeInfo info)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(info.Sender);
            sb.Append(",");
            sb.Append(info.Receiver);
            sb.Append(",");
            sb.Append(info.PostIP);
            sb.Append(",");
            sb.Append(info.Notice);
            AppRequest req = new AppRequest("Notification", "Send", sb.ToString());
            int n = 0;
            int.TryParse(req.Result, out n);
            return n;
        }
        /// <summary>
        /// 发送通知
        /// </summary>
        /// <param name="Sender"></param>
        /// <param name="Receiver"></param>
        /// <param name="Notice"></param>
        /// <returns></returns>
        static public int Send(int Sender, int Receiver, string Notice)
        {
            NoticeInfo info = new NoticeInfo();
            info.Sender = Sender;
            info.Receiver = Receiver;
            info.Notice = Notice;
            info.PostIP = getClientIP();
            return Send(info);
        }
        /// <summary>
        /// 取得某用户的通知
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        static public List<NoticeInfo> GetNotice(int UserID)
        {
            AppRequest req = new AppRequest("Notification", "GetNotice", UserID.ToString());
            List<NoticeInfo> infolist = strToXML(req.Result);
            return infolist;
        }




        static private List<NoticeInfo> strToXML(string str)
        {
            List<NoticeInfo> infolist = new List<NoticeInfo>();
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(str);
            XmlNodeList nodelist = xDoc.SelectNodes("/root/Row");
            for (int i = 0; i < nodelist.Count; i++)
            {
                XmlNode node = nodelist[i];
                if (node.NodeType != XmlNodeType.Comment)
                {
                    NoticeInfo info = new NoticeInfo();
                    PropertyInfo[] pInfo = info.GetType().GetProperties();
                    foreach (PropertyInfo pinfo in pInfo)
                    {
                        XmlNode myNode = node.SelectSingleNode(pinfo.Name);
                        object tobj;
                        if (myNode.InnerText.ToLower() == "true")
                        {
                            tobj = 1;
                        }
                        else if (myNode.InnerText.ToLower() == "false")
                        {
                            tobj = 0;
                        }
                        else
                        {
                            tobj = Convert.ChangeType(myNode.InnerText, pinfo.PropertyType);
                        }
                        pinfo.SetValue(info, tobj, null);
                    }
                    infolist.Add(info);
                }
            }
            return infolist;
        }
        static private string getClientIP()
        {
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null)
            {
                return System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            }
            else
            {
                return System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
        }
    }
}
