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 V5Shop.Common;
using V5Shop.Web.UI;
using System.Text.RegularExpressions;
using V5Shop.Data;

namespace V5Shop.Api
{
    public partial class V5ShopApi : TplPage
    {
        private string username = string.Empty;
        private string password = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            username = utils.V5ShopRequest("username");
            password = utils.V5ShopRequest("password");

            CheckApi();
        }

        /// <summary>
        /// 同步API接口
        /// </summary>
        private void CheckApi()
        {
            //判断验证码是否合法-前期先不考虑加入后台，使用固定验证码
            string verifyCode = "sdkfdsjj$#12r90dfjs@1*&()_^&0fdjs";
            string action = utils.V5ShopRequest("action");
            string code = utils.V5ShopRequest("v5_code");
            //如果验证码不一致则停止处理
            if (code != verifyCode)
                return;
            //根据action判断进行什么动作
            switch (action)
            {
                case "login":
                    LoginV5Shop();
                    break;
                case "logout":
                    LogoutV5Shop();
                    break;
                case "deleteuser":
                    DeleteUserV5Shop();
                    break;
                case "updatepwd":
                    UpdateUserPasswordV5Shop();
                    break;
                case "updatecredits":
                    UpdateUserScoreV5Shop();
                    break;
            }
        }

        /// <summary>
        /// 同步修改会员积分
        /// </summary>
        private void UpdateUserScoreV5Shop()
        {

        }

        /// <summary>
        /// 同步修改会员密码
        /// </summary>
        private void UpdateUserPasswordV5Shop()
        {
            DatabaseProvider.GetInstance().UpdateUserPass(username, password);
        }

        /// <summary>
        /// 同步删除会员
        /// </summary>
        private void DeleteUserV5Shop()
        {
            DataTable dt = DatabaseProvider.GetInstance().GetUserByName(username);
            if (dt.Rows.Count != 0)
            {
                string uid = dt.Rows[0]["ID"].ToString();
                DatabaseProvider.GetInstance().DeleteUser(uid);
            }
        }

        /// <summary>
        /// 同步注销会员
        /// </summary>
        private void LogoutV5Shop()
        {
            string TheUrl = string.Empty;
            string p = @"http://(?<domain>[^\/]*)";
            TheUrl = HttpContext.Current.Request.Url.ToString();
            Regex reg = new Regex(p, RegexOptions.IgnoreCase);
            Match m = reg.Match(TheUrl);
            string encodestr = m.Groups["domain"].Value;

            //清理登陆信息
            HttpCookie cookieUser = new HttpCookie("V5ShopUser");
            if (TheUrl.ToLower().IndexOf("http://localhost") == -1 && TheUrl.ToLower().IndexOf("http://127.0.0.1") == -1)
            {
                cookieUser.Domain = encodestr.ToLower().Replace("www.", "");
            }
            cookieUser.Values.Clear();
            cookieUser.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.AppendCookie(cookieUser);
            //清理登陆信息
            HttpCookie cookieUserTemp = new HttpCookie("V5ShopUserTemp");
            if (TheUrl.ToLower().IndexOf("http://localhost") == -1 && TheUrl.ToLower().IndexOf("http://127.0.0.1") == -1)
            {
                cookieUserTemp.Domain = encodestr.ToLower().Replace("www.", "");
            }
            cookieUserTemp.Values.Clear();
            cookieUserTemp.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.AppendCookie(cookieUserTemp);
            //清理购物车信息
            HttpCookie cookieGuid = new HttpCookie("guid");
            if (TheUrl.ToLower().IndexOf("http://localhost") == -1 && TheUrl.ToLower().IndexOf("http://127.0.0.1") == -1)
            {
                cookieGuid.Domain = encodestr.ToLower().Replace("www.", "");
            }
            cookieGuid.Values.Clear();
            cookieGuid.Expires = DateTime.Now.AddYears(-1);
            HttpContext.Current.Response.AppendCookie(cookieGuid);
        }

        /// <summary>
        /// 同步登录会员
        /// </summary>
        private void LoginV5Shop()
        {
            base.Login(username);
        }

        /// <summary>
        /// 获取传递过来的参数
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private string GetRequest(string key)
        {
            return Request.QueryString[key] == null ? "" : Request.QueryString[key].ToString();
        }
    }
}