using dodohold.core;
using Sayaka.Common;


namespace molilian.core
{
    public partial class JdUnionPlus
    {

        public async Task<string> H5stHack(dynamic querystring, CancellationToken cancellationToken)
        {
            string body = string.Empty;
            string useragent = _account.user_agent;
            if (string.IsNullOrEmpty(useragent)) useragent = ProviderFakeUserAgent.RandomComputer;
#if DEBUG
            _config.jd_h5st_sign_version = "5.3.2";
#endif
            var args = new
            {
                version = _config.jd_h5st_sign_version,
                body = querystring,
                pin = _account.pin,
                //envStr = _account.envStr,
                ua = _account.user_agent,
                //h5st = _account.h5st,
                appId = "586ae",
            };

            try
            {
                string data = args.Convert2Json();
                string url = _config.jd_h5st_sign_url;
                if (string.IsNullOrEmpty(url)) url = "http://localhost:3001/h5st";

#if DEBUG
                //url = "http://bjapi.molilian.com:3001/h5st";
                url = "http://127.0.0.1:3001/h5st";
#endif

                WebClientUtility client = new WebClientUtility();
                client.SetContentType("application/json");
                client.Timeout = TimeSpan.FromMilliseconds(2000);
                client.Post(data);
                var response = await client.RequestAsync(url, "POST", cancellationToken);

                body = response.Body();
                /*
{"code":200,"body":{"qs":"20250210183653774;a3a3wa33x9spd3n1;fb5df;tk04w1f0304bd41lMXgzeDN4MSsxXRJNrMWApMCLhMmN48iN4MGBZZJZFbr_lex8lZ2-o927yUFH3I1Hr4mBgVDLoBjN;75b9569aad0b0363efa5729431ead43c95ddd8ca7acb28bb8c4f9b3b101b373e;5.0;1739183813774;WNpZcw5Rph3NrZ2R5x2RoFyB3ZXRPp3RohjN1Yi9g5iNmVj8mVjN4QXRfRX9zQXR5xZO3B2OphHMjcEAt0zA6EW-3Z3L3ByR5Z3XtR3R5x2RnEz90ADAnQXR5xZO31U-0gzGmYUMQA1FIQXRfRH_qIz_4QyR5Z3XtlSR5xJN5x2RpVG83QXR5ZXRPpXN5x2RhFyB3ZXR5Z3XtZWRfRnMkQzR5ZXR5xZOoZ3L3BG83QXR5ZXRPpXN5x2RkFyB3ZXR5Z3XtZWRfRXMkQzR5ZXR5xZOgVWRfRnNkQzR5ZXR5xZOolXRfR3NkQzR5ZXR5xZO3F2OohXN3Z3L3VG83QXR5ZXRPpXN5x2Ru8yR5ZXR5xZOpZ3L3NS_3ZXR5Z3XtZWRfRX_3ZXR5Z3XtZWRfRXAiQXR5ZXRPpXN5x2RsYyR5ZXR5xp75x2R1gDAl4CA3ZXRPpH6eY3L3ZS93ZXRPp3RlBW75tWMjhD-CYnLphXNoZXELYn9icTAr0jE3Z3L3VD8mQXR5xp7;7c14c57d9aab873cc3805f9d02740b727fe3fb9720394a5e33240b303aaee289"}}                 
                 */
                var root = body.Convert2JsonElement();
                int code = root.Read<int>("code");
                string qs = root.PathRead<string>("body.qs");
                if (code == 200)
                {
                    _ = new LoggerLibrary("JdUnion", "h5st").Info("ok").SaveAsync();
                    return qs;
                }
                _ = new LoggerLibrary("JdUnion", "h5st").Info(body).SaveAsync();
            }
            catch (Exception ex)
            {
                _ = new LoggerLibrary("JdUnion", "h5st_error")
                    .Info(body)
                    .Info(ex.Message, ex.StackTrace)
                    .SaveAsync();

            }
            return string.Empty;
        }

    }
}
