using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Data;
using System.Web;
namespace JuSNS.AppClient
{
    /// <summary>
    /// 用户类
    /// </summary>
    public class User
    {
        static private int _UserID;
        static public int UserID
        {
            get { return _UserID; }
        }
        static User()
        {
            _UserID = GetLoggedUserID();
        }
        /// <summary>
        /// 取得登陆者用户ID
        /// </summary>
        /// <returns></returns>
        static public int GetLoggedUserID()
        {
            int UserID = 0;
            int.TryParse(HttpContext.Current.Session["AppUserID"].ToString(), out UserID);
            return UserID;
        }
        /// <summary>
        /// 取得当前用户信息
        /// </summary>
        /// <returns></returns>
        static public UserInfo GetUserInfo()
        {
            return GetUserInfo(_UserID);
        }
        /// <summary>
        /// 取得某用户的信息
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns></returns>
        static public UserInfo GetUserInfo(int UserID)
        {
            UserInfo info = new UserInfo();
            PropertyInfo[] propertys = info.GetType().GetProperties();
            foreach (PropertyInfo pInfo in propertys)
            {
                AppRequest req = new AppRequest(UserID.ToString(), "ApiUser", "GetProperty", pInfo.Name);
                object value;
                if (req.Result.ToLower() == "true")
                {
                    value = 1;
                }
                else if (req.Result.ToLower() == "false")
                {
                    value = 0;
                }
                else
                {
                    value = Convert.ChangeType(req.Result, pInfo.PropertyType); 
                }
                pInfo.SetValue(info, value, null);
            }
            return info; 
        }
        /// <summary>
        /// 取得用户名称
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns></returns>
        static public string GetUserName(int UserID)
        {
            object obj = GetField("UserName");
            return obj.ToString();
        }
        /// <summary>
        /// 取得当前用户用户名
        /// </summary>
        /// <returns></returns>
        static public string GetUserName()
        {
            return GetUserName(_UserID);
        }
        /// <summary>
        /// 取得用户邮件
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        static public string GetUserEmail(int UserID)
        {
            object obj = GetField("Email");
            return obj.ToString();
        }
        /// <summary>
        /// 取得当前用户邮件
        /// </summary>
        /// <returns></returns>
        static public string GetUserEmail()
        {
            return GetUserEmail(_UserID);
        }
        /// <summary>
        /// 区得某个字段的值
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="FieldName">字段名称</param>
        /// <returns></returns>
        static public object GetField(int UserID,string FieldName)
        {
            AppRequest req = new AppRequest(UserID.ToString(), "ApiUser", "GetProperty", FieldName);
            return (object)req.Result;
        }
        /// <summary>
        /// 取得某用户指定字段的信息
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="Fields"></param>
        /// <returns></returns>
        static public object[] GetFields(int UserID,string[] Fields)
        {
            AppRequest req = new AppRequest(UserID.ToString(), "ApiUser", "GetAllProperty", string.Join(",", Fields));
            object[] arr = new object[1];
            string[] sArr = req.Result.Split(',');
            int i = 0;
            foreach(string s in sArr)
            {
                i++;
                Array.Resize<object>(ref arr, i);
                arr[i - 1] = sArr[i - 1];
            }
            return arr;
        }
        /// <summary>
        /// 取得当前用户某个字段的值
        /// </summary>
        /// <param name="FieldName">字段名称</param>
        /// <returns></returns>
        static public object GetField(string FieldName)
        {
            return GetField(_UserID, FieldName);
        }
        /// <summary>
        /// 取得当前用户指定字段的信息
        /// </summary>
        /// <param name="Fields"></param>
        /// <returns></returns>
        static public object[] GetFields(string[] Fields)
        {
            return GetFields(_UserID, Fields);
        }
        /// <summary>
        /// 是否有此字段
        /// </summary>
        /// <param name="FieldName">字段名</param>
        /// <returns></returns>
        static public bool HasField(string FieldName)
        {
            AppRequest req = new AppRequest(_UserID.ToString(), "ApiUser", "HasField", FieldName);
            string s = req.Result;
            if (s == "0")
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 取得所有可访问字段
        /// </summary>
        /// <returns></returns>
        static public string[] HasFields()
        {
            AppRequest req = new AppRequest(_UserID.ToString(), "ApiUser", "GetFields", "");
            string s = req.Result;
            return s.Split(',');
        }
        /// <summary>
        /// 判断某用户是否添加了当前应用
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        static public bool IsAppAdded(int UserID)
        {
            AppRequest req = new AppRequest(UserID.ToString(), "ApiUser", "IsAppAdded", "0");
            string s = req.Result;
            if (s == "0")
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 取得当前用户头像
        /// </summary>
        /// <returns></returns>
        static public string GetHeadImg()
        {
            AppRequest req = new AppRequest(_UserID.ToString(), "ApiUser", "GetHeadImg", "");
            string s = req.Result;
            return s;
        }
        /// <summary>
        /// 取得指定用户头像
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        static public string GetHeadImg(int UserID)
        {
            AppRequest req = new AppRequest(UserID.ToString(), "ApiUser", "GetHeadImg", "");
            string s = req.Result;
            return s;
        }
        /// <summary>
        /// 取得某个字段是否可写
        /// </summary>
        /// <param name="field"></param>
        /// <returns></returns>
        static public bool CanWrite(string field)
        {
            AppRequest req = new AppRequest(UserID.ToString(), "ApiUser", "CanWrite", field);
            string s = req.Result;
            if (s == "0")
            {
                return false;
            }
            return true;
        }
        /// <summary>
        /// 设置某个字段的值
        /// </summary>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        static public int SetField(string field, string value)
        {
            AppRequest req = new AppRequest(_UserID.ToString(), "ApiUser", "SetField", field + "," + value);
            string s = req.Result;
            int n = 0;
            int.TryParse(s, out n);
            return n;
        }
        /// <summary>
        /// 设置指定用户某个字段的值
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="field"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        static public int SetField(int UserID, string field, string value)
        {
            AppRequest req = new AppRequest(UserID.ToString(), "ApiUser", "SetField", field + "," + value);
            string s = req.Result;
            int n = 0;
            int.TryParse(s, out n);
            return n;
        }
        /// <summary>
        /// 系统是否存在某个邮箱
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        static public bool ExistEmail(string email)
        {
            AppRequest req = new AppRequest(UserID.ToString(), "ApiUser", "ExistEmail", email);
            string s = req.Result;
            if (s == "0")
            {
                return false;
            }
            return true;
        }
    }
}
