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;
using System.Xml.Schema;
namespace JuSNS.Web.appserver
{
    public partial class SActivity : 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/Action.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 Add()
        {
            string[] arr = param.Split(',');
            int UserID = 0;
            string Content = string.Empty;
            string aParams = string.Empty;
            if (!int.TryParse(arr[0], out UserID))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            Content = arr[1];
            try
            {
                aParams = arr[2];
            }
            catch { }
            AppActiveInfo info = new AppActiveInfo();
            info.AppID = AppID;
            info.UserID = UserID;
            info.Content = Content;
            info.CreationTime = DateTime.Now;
            info.Params = aParams;
            int n = Activity.Add(info);
            Response.Write(n);
        }
        /// <summary>
        /// 删除动态
        /// </summary>
        protected void Del()
        {
            int ActiveID = 0;
            if (!int.TryParse(param, out ActiveID))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            int n = Activity.Del(AppID, ActiveID);
            Response.Write(n);
        }
        /// <summary>
        /// 取得某人的动态列表
        /// </summary>
        protected void GetList()
        {
            int UserID;
            int Num;
            string[] arr = param.Split(',');
            if(!int.TryParse(arr[0],out UserID))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            if (!int.TryParse(arr[1], out Num))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            List<AppActiveInfo> infolist = Activity.GetList(AppID, UserID, Num);
            writeXml(infolist);
        }
        /// <summary>
        /// 取得某人和好友的动态
        /// </summary>
        protected void GetFriendActivity()
        {
            int UserID;
            int Num;
            string[] arr = param.Split(',');
            if (!int.TryParse(arr[0], out UserID))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            if (!int.TryParse(arr[1], out Num))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            List<AppActiveInfo> infolist = Activity.GetFriendActivity(AppID, UserID, Num);
            writeXml(infolist);
        }
        /// <summary>
        /// 取得某条动态信息 
        /// </summary>
        protected void GetInfo()
        {
            int AppID;
            int ActiveID;
            string[] arr = param.Split(',');
            if (!int.TryParse(arr[0], out AppID))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            if (!int.TryParse(arr[1], out ActiveID))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            List<AppActiveInfo> infolist = new List<AppActiveInfo>();
            infolist.Add(Activity.GetInfo(AppID, ActiveID));
            writeXml(infolist);
        }














        private void writeXml(List<AppActiveInfo> infolist)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(Server.MapPath(XmlPath));
            XmlNode pNode = xDoc.SelectSingleNode("/root");
            XmlNode xNode = pNode.FirstChild;
            for (int i = 0; i < infolist.Count; i++)
            {
                XmlNodeList nodelist = xNode.ChildNodes;
                XmlNode node = xNode.Clone();
                AppActiveInfo 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());
        }
    }
}
