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 JuSNS.ApiInterface;
using System.Reflection;
using System.Text;
namespace JuSNS.Web.appserver
{
    public partial class SApiUser : System.Web.UI.Page
    {
        /// <summary>
        /// 请求类型
        /// </summary>
        protected string action = string.Empty;
        /// <summary>
        /// 参数
        /// </summary>
        protected string param = string.Empty;
        /// <summary>
        /// appkey
        /// </summary>
        protected string appkey = string.Empty;
        /// <summary>
        /// 用户ID
        /// </summary>
        protected string userid = string.Empty;
        /// <summary>
        /// 用户接口类
        /// </summary>
        protected ApiUser apiUser;
        /// <summary>
        /// 当前请求用户ID
        /// </summary>
        protected int UserID = 0;
        /// <summary>
        /// 应用程序ID
        /// </summary>
        protected int AppID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.init();
            apiUser = new ApiUser(UserID);
            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()
        {
            param = Request.Form["param"];
            appkey = Request.Form["appkey"];
            userid = Request.Form["userid"];
            action = Request.Form["action"];

            bool valid = App.VerifyKey(appkey,out AppID);
            if (!valid)
            {
                Response.Write((int)EnumErr.ErrKey);
                Response.End();
            }
            if (!int.TryParse(userid, out UserID))
            {
                Response.Write((int)EnumErr.ErrUserID);
                Response.End();
            }
            if (string.IsNullOrEmpty(action))
            {
                Response.Write((int)EnumErr.ErrRequestType);
                Response.End();
            }
        }
        /// <summary>
        /// 存在用户ID
        /// </summary>
        protected void ExistUserID()
        {
            if (!apiUser.ExistUserID(apiUser.UserID))
            {
                Response.Write("0");
                return;
            }
            Response.Write("1");
        }
        /// <summary>
        /// 取得用户属性
        /// </summary>
        protected void GetProperty()
        {
            string field = param;
            bool hasFields = apiUser.HasField(field);
            if (!hasFields)
            {
                Response.Write((int)EnumErr.ErrField);
                return;
            }
            string value = apiUser.GetProperty(field);
            Response.Write(value);
        }
        /// <summary>
        /// 取得几个字段
        /// </summary>
        protected void GetAllProperty()
        {
            string[] fields=param.Split(',');
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < fields.Length; i++)
            {
                string field = fields[i];
                bool hasFields = apiUser.HasField(field);
                if (!hasFields)
                {
                    Response.Write((int)EnumErr.ErrField);
                    return;
                }
                sb.Append(apiUser.GetProperty(field));
                sb.Append(",");
            }
            string str = sb.ToString();
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Substring(0, str.Length - 1);
            }
            Response.Write(str);
        }
        /// <summary>
        /// 是否有某个字段
        /// </summary>
        protected void HasField()
        {
            bool hasField = apiUser.HasField(param);
            if (hasField)
            {
                Response.Write("1");
                return;
            }
            Response.Write("0");
            return;
        }
        /// <summary>
        /// 取得所有字段名称
        /// </summary>
        protected void GetFields()
        {
            string[] fields = apiUser.GetFields();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < fields.Length; i++)
            {
                sb.Append(fields[i]);
                sb.Append(",");
            }
            string str = sb.ToString();
            if (!string.IsNullOrEmpty(str))
            {
                str = str.Substring(0, str.Length - 1);
            }
            Response.Write(fields);
        }
        /// <summary>
        /// 设置某个字段的值
        /// </summary>
        protected void SetField()
        {
            string[] str = param.Split(',');
            if (str.Length != 2)
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            int n = apiUser.SetField(str[0], str[1]);
            Response.Write(n);
        }
        /// <summary>
        /// 某个字段是否可以写入
        /// </summary>
        protected void CanWrite()
        {
            bool b = apiUser.CanWrite(param);
            if (b)
            {
                Response.Write("1");
                return;
            }
            Response.Write("0");
        }
        /// <summary>
        /// 判断用户是否添加某个应用
        /// </summary>
        protected void IsAppAdded()
        {
            int appID = 0;
            if (!int.TryParse(param, out appID))
            {
                Response.Write((int)EnumErr.ErrParams);
                return;
            }
            bool b = apiUser.IsAppAdded(AppID);
            if (b)
                Response.Write("1");
            else
                Response.Write("0");
        }
        /// <summary>
        /// 取得用户头像
        /// </summary>
        protected void GetHeadImg()
        {
            string str=apiUser.GetHeadImg();
            Response.Write(str);
        }
        /// <summary>
        /// 是否存在某个邮件
        /// </summary>
        protected void ExistEmail()
        {
            bool b = apiUser.ExistEmail(param);
            if(b)
                Response.Write("1");
            else
                Response.Write("0");
        }
    }
}
