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

namespace JuSNS.AppClient
{
    /// <summary>
    /// 活动类
    /// </summary>
    public class Activity
    {
        /// <summary>
        /// 添加动态
        /// </summary>
        /// <param name="info">动态信息</param>
        static public int Add(ActiveInfo info)
        {
            string str = info.UserID + "," + info.Content + "," + info.Params;
            AppRequest req = new AppRequest("Activity", "Add", str);
            int n = 0;
            int.TryParse(req.Result, out n);
            return n;
        }
        /// <summary>
        /// 添加动态
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="Content"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        static public int Add(int UserID, string Content, params string[] param)
        {
            string p = "";
            for (int i = 0; i < param.Length; i++)
            {
                p += "," + param[i] ;
            }
            if (p != "")
            {
                p = p.Substring(1);
            }
            ActiveInfo info = new ActiveInfo();
            info.UserID = UserID;
            info.Content = Content;
            info.Params = p;
            return Add(info);
        }
        /// <summary>
        /// 删除动态
        /// </summary>
        /// <param name="ActiveID">动态ID</param>
        /// <returns></returns>
        static public int Delete(int ActiveID)
        {
            AppRequest req = new AppRequest("Activity", "Del", ActiveID.ToString());
            int n = 0;
            int.TryParse(req.Result, out n);
            return n;
        }
        /// <summary>
        /// 取得某个的动态
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="Num">数量</param>
        /// <returns></returns>
        static public List<ActiveInfo> GetList(int UserID,int Num)
        {
            List<ActiveInfo> infolist = new List<ActiveInfo>();
            AppRequest req = new AppRequest("Activity", "GetList", UserID.ToString() + "," + Num);
            infolist=strToXML(req.Result);
            return infolist;
        }
        /// <summary>
        /// 取得某人朋友的动态
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="Num">条数</param>
        /// <returns></returns>
        static public List<ActiveInfo> GetFriendActivity(int UserID, int Num)
        {
            List<ActiveInfo> infolist = new List<ActiveInfo>();
            AppRequest req = new AppRequest("Activity", "GetFriendActivity", UserID.ToString() + "," + Num);
            infolist = strToXML(req.Result);
            return infolist;
        }
        static private List<ActiveInfo> strToXML(string str)
        {
            List<ActiveInfo> infolist = new List<ActiveInfo>();
            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)
                {
                    ActiveInfo info = new ActiveInfo();
                    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;
        }
    }
}
