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;
namespace JuSNS.Web.appserver
{
    public partial class SFriend : 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 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 Get()
        {
            int UserID;
            if (!int.TryParse(param, out UserID))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            int[] arr = Friend.Get(UserID);
            string[] sArr = new string[arr.Length];
            int p=0;
            foreach (int i in arr)
            {
                sArr[p] = i.ToString();
                p++;
            }
            string str = String.Join(",", sArr);
            Response.Write(str);
        }
        /// <summary>
        /// 是否是朋友
        /// </summary>
        protected void AreFriends()
        {
            int UserID;
            int FriendID;
            string[] arr=param.Split(',');
            try
            {
                if (!int.TryParse(arr[0], out UserID))
                {
                    Response.Write((int)EnumErr.ErrParams);
                    return;
                }
                if (!int.TryParse(arr[1], out FriendID))
                {
                    Response.Write((int)EnumErr.ErrParams);
                    return;
                }
            }
            catch
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            bool bl = Friend.AreFriends(UserID, FriendID);
            if (bl)
            {
                Response.Write("1"); 
                return;
            }
            Response.Write("0");
        }
        /// <summary>
        /// 取得某App
        /// </summary>
        protected void GetAppUsers()
        {
            int UserID;
            if (!int.TryParse(param, out UserID))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }     
            int[] iArr = Friend.GetAppUsers(UserID, AppID);
            string[] sArr = new string[iArr.Length];
            for (int i = 0; i < iArr.Length; i++)
            {
                sArr[i] = iArr[i].ToString();
            }
            string str = String.Join(",", sArr);
            Response.Write(str);
        }
    }
}
