using System;
using TsSFCDevice.Client.Biz.DeviceSvc;

namespace TsSFCDevice.Client.Biz.Base.Service
{
    public class TsSFCMainSvc
    {
        /// <summary>
        /// token
        /// </summary>
        #region 字段/属性
        const string SOPHeadUserName = "TECHSCAN_Myl";
        string m_Connstring = "";
        string user = "";
        string pwd = "";
        string version = "";
        static string GUID = "";
        static string Mac = "";
        SFCAuthorize auth;
        MainService svc;
        #endregion

        public TsSFCMainSvc(string User, string Pwd, string Version, string Conn)
        {
            m_Connstring = Conn;
            user = User;
            pwd = Pwd;
            version = Version;

            byte[] password = System.Text.Encoding.Unicode.GetBytes(user);
            Array.Reverse(password, 0, password.Length);
            string pass64 = Convert.ToBase64String(password, 0, password.Length);
            if (pass64.Length < 10)
            {
                pass64 = pass64 + "YeT+=fue";
            }

            auth = new SFCAuthorize();
            auth.Username = user;
            auth.Password = pass64;
            auth.Version = Version;
            if (GUID == "")
            {
                GUID = Guid.NewGuid().ToString();
            }
            auth.GUID = GUID;

            if (Mac == "")
            {
                //Mac = (new SysInfo()).GetMac();
            }
            auth.MacAddress = Mac;
            auth.SapSetting = (ushort)(1);

            svc = new MainService()
            {

                Url = m_Connstring,
                Timeout = 300000,
                SFCAuthorizeValue = auth
            };
        }

        public MainService CurrentSvc
        {
            get
            {
                if (svc == null)
                {
                    svc = new MainService();
                    byte[] password = System.Text.Encoding.Unicode.GetBytes(user);
                    Array.Reverse(password, 0, password.Length);
                    string pass64 = Convert.ToBase64String(password, 0, password.Length);
                    if (pass64.Length < 10)
                    {
                        pass64 = pass64 + "YeT+=fue";
                    }
                    auth = new SFCAuthorize();
                    auth.Username = user;
                    auth.Password = pass64;
                    auth.Version = version;
                    auth.SapSetting =
                        (ushort)(1);
                    svc.Url = m_Connstring;
                    //ser.Timeout = 300000;
                    svc.Timeout = 300000;
                    auth.GUID = GUID;
                    svc.SFCAuthorizeValue = auth;
                }
                return svc;
            }
        }


        public void SetWebService(string svcUrl, int vServiceTimeOut = 300000)
        {
            svc = new MainService();

            byte[] password = System.Text.Encoding.Unicode.GetBytes(user);
            Array.Reverse(password, 0, password.Length);
            string pass64 = Convert.ToBase64String(password, 0, password.Length);
            if (pass64.Length < 10)
            {
                pass64 = pass64 + "YeT+=fue";
            }

            svc.Url = svcUrl;
            //ser.Timeout = 300000;
            svc.Timeout = vServiceTimeOut;

            #region WS安全交互处理
            auth = new SFCAuthorize();
            auth.Username = SOPHeadUserName;
            auth.Password = pass64;
            //auth.Version = Utility.CurrentDevice.RFVersion;
            auth.SapSetting = (ushort)(1);
            auth.GUID = GUID;

            svc.SFCAuthorizeValue = auth;
            #endregion
        }
    }
}
