using NLog;
using System;
using System.Text;
using System.Web;
using System.Web.Services.Protocols;

namespace TsSFCDeviceSvc.App_Code
{
    public class SFCAuthorize : SoapHeader
    {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();
        public string Password;
        public string Username;
        public string GUID;
        /// <summary>
        /// SAP环境（0：测试环境/1：正式环境）
        /// </summary>
        public UInt16 SapSetting;
        public string Version;
        public string MacAddress;
        public bool ValidUser(string inUsername, string inPassword)
        {
            //Utility.SapSetting = SapSetting;
            if (inPassword == null) throw new ArgumentNullException("inPassword");
            string conpass = "";

            if (inUsername.Length == 0) return false;

            string ip = HttpContext.Current.Request.UserHostAddress;

            byte[] password = Encoding.Unicode.GetBytes(Username);

            Array.Reverse(password);

            string pass64 = Convert.ToBase64String(password);

            if (pass64.Length < 10) pass64 += "YeT+=fue";

            conpass = pass64;

            if (conpass == Password)
            {

                return true;
            }

            //log.WriteLog("ValidUser", "密码验证未通过，有可能有非法用户在尝试登录:" + ip);
            log.Error("ValidUser", "パスワードの検証が失敗し、ログインしようとする違法ユーザがあるかもしれません:" + ip);
            return false;
        }

        public string token;

        public SFCAuthorize()
        {
            log.Warn($"这是个测试!");
        }
    }
}