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.IO;
using Discuz.Toolkit;
using System.Text.RegularExpressions;
using System.Text;

namespace InfoCenter
{
    public partial class DZNT_UserApi : System.Web.UI.Page
    {
        private string DZconfig1 = string.Empty;
        private string DZconfig2 = string.Empty;
        private string DZconfig3 = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            string kftconfigpath = Server.MapPath("../../../config/ApiConfig.config");
            string[] kftconfigcontent = File.ReadAllText(kftconfigpath, Encoding.UTF8).Split('$');
            DZconfig1 = kftconfigcontent[0];
            DZconfig2 = kftconfigcontent[1];
            DZconfig3 = kftconfigcontent[3];

            //进行动作判断
            string action = GetRequest("action");

            switch (action)
            {
                case "login":
                    LoginDZNT();
                    break;
                case "logout":
                    LogoutDZNT();
                    break;
                case "deleteuser":
                    DeleteUserDZNT();
                    break;
                case "updatepwd":
                    UpdateUserPasswordDZNT();
                    break;
                case "updatecredits":
                    UpdateUserScoreDZNT();
                    break;
            }

            Response.End();
        }

        /// <summary>
        /// 同步修改DZ!NT论坛会员积分
        /// </summary>
        private void UpdateUserScoreDZNT()
        {
            //string username = GetRequest("username");
            //string password = GetRequest("score");
            //Discuz.Toolkit.DiscuzSession dz = new Discuz.Toolkit.DiscuzSession(DZconfig1, DZconfig2, DZconfig3);
            //Discuz.Toolkit.UserForEditing ufe = new Discuz.Toolkit.UserForEditing();

            //int uid = dz.GetUserID(username);
            ////ufe.cr = password;
            //dz.SetUserInfo(uid, ufe);
        }

        /// <summary>
        /// 同步修改DZ!NT论坛会员密码
        /// </summary>
        private void UpdateUserPasswordDZNT()
        {
            string username = GetRequest("username");
            string password = GetRequest("password");
            Discuz.Toolkit.DiscuzSession dz = new Discuz.Toolkit.DiscuzSession(DZconfig1, DZconfig2, DZconfig3);
            Discuz.Toolkit.UserForEditing ufe = new Discuz.Toolkit.UserForEditing();

            int uid = dz.GetUserID(username);
            ufe.Password = password;
            dz.SetUserInfo(uid, ufe);
        }

        /// <summary>
        /// 删除DZ!NT论坛会员
        /// </summary>
        private void DeleteUserDZNT()
        {

        }

        /// <summary>
        /// 注销DZ!NT论坛
        /// </summary>
        private void LogoutDZNT()
        {
            //对DZ论坛整合进行过滤，防止DLL不存在的报错现象发生
            try
            {
                //获取论坛COOKIE地址
                Regex regApi = new Regex(@"http://(?<domain>[^\/]*)", RegexOptions.IgnoreCase);
                Match mApi = regApi.Match(DZconfig3);
                DiscuzSession dz = new DiscuzSession(DZconfig1, DZconfig2, DZconfig3);
                dz.Logout(mApi.Groups["domain"].Value);
            }
            catch { }
        }

        /// <summary>
        /// 登录DZ!NT论坛
        /// </summary>
        private void LoginDZNT()
        {
            string username = GetRequest("username");
            string password = GetRequest("password");
            string email = username + "@v5shop.com.cn";

            Regex regApi = new Regex(@"http://(?<domain>[^\/]*)", RegexOptions.IgnoreCase);
            Match mApi = regApi.Match(DZconfig3);
            //对DZ论坛整合进行过滤，防止DLL不存在的报错现象发生
            DiscuzSession dz = new DiscuzSession(DZconfig1, DZconfig2, DZconfig3);

            int uid = dz.GetUserID(username);
            if (uid.Equals(0))
            {
                //注册新用户
                uid = dz.Register(username, password, username + "@v5shop.com.cn", false);
            }
            //登陆
            dz.Login(uid, password, false, 1000, mApi.Groups["domain"].Value);
            return;
        }

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