using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Web;
using System.IO;

namespace JuSNS.AppClient
{
    public class AppRequest
    {
        public string UserID;
        public string AccessType;
        public string Action;
        public string Param;
        public string AppKey;
        private string _Result;
        /// <summary>
        /// 请求结果
        /// </summary>
        public string Result
        {
            get { return _Result; }
        }
        public AppRequest(string UserID, string AccessType, string Action, string Param)
        {
            this.AccessType = AccessType;
            this.AppKey = Config.AppKey;
            this.Param = Param;
            this.Action = Action;
            this.UserID = UserID;
            this.Send();
        }

        public AppRequest(string AccessType, string Action, string Param)
        {
            this.AccessType = AccessType;
            this.AppKey = Config.AppKey;
            this.Param = Param;
            this.Action = Action;
            this.UserID = "0";
            this.Send();
        }
        /// <summary>
        /// 发送请求
        /// </summary>
        public void Send()
        {
            WebRequest req;
            string querystring = "action=" + Action + "&param=" + Param + "&appkey=" + this.AppKey;

            if (!(UserID == "0"))
            {
                querystring = "userid=" + UserID + "&" + querystring;
            }
            string url = Config.ServerUrl + "/S" + AccessType + ".aspx";
            req = WebRequest.Create(url);
            req.Method = "Post";
            req.ContentType = "application/x-www-form-urlencoded;;charset=\"utf-8\"";
            byte[] bytes = Encoding.UTF8.GetBytes(querystring);
            req.ContentLength = bytes.Length;
            using (Stream sm = req.GetRequestStream())
            {
                sm.Write(bytes, 0, bytes.Length);
            }
            WebResponse response = req.GetResponse();
            StreamReader sr = new StreamReader(response.GetResponseStream());
            string str = sr.ReadToEnd();
            Array arr = Enum.GetValues(typeof(EnumErr));
            for (int i = 0; i < arr.Length; i++)
            {
                string errstr = ((int)(EnumErr)arr.GetValue(i)).ToString();
                if (errstr == str)
                {
                    EnumErr enumEr = (EnumErr)Convert.ToInt32(errstr);
                    switch (enumEr)
                    {
                        case EnumErr.ErrField:
                            throw new Exception("不支持此字段");
                        case EnumErr.ErrKey:
                            throw new Exception("AppKey错误");
                        case EnumErr.ErrParams:
                            throw new Exception("参数错误");
                        case EnumErr.ErrRequestType:
                            throw new Exception("请求类型错误");
                        case EnumErr.ErrUserID:
                            throw new Exception("用户ID错误");
                        case EnumErr.NoExistUserID:
                            throw new Exception("用户ID不存在");                           
                    }
                }
            }
            _Result = str;
        }
    }
}
