using dodohold.core;
using Sayaka.Common;


namespace molilian.core
{
    public partial class JdUnionPlus
    {



        public async Task<JdDataDTO> listOrderSku(JdDataDTO result, string wareUrl, CancellationToken cancellationToken = default)
        {
            string[] parts = wareUrl.Split(new string[] { ".html" }, StringSplitOptions.None);
            if (parts.Length > 1)
            {
                wareUrl = parts[0] + ".html";
            }


            var ts = DateTime.Now.Convert2UnixTimestamp(true);
            string cookies = _account.cookies;

            string uuid = cookies.GetContentPart("__jdu=", ";");
            string __jda = cookies.GetContentPart("__jda=", ";");
            if (__jda.Split('.').Length > 1)
            {
                uuid = __jda.Split('.')[1];
            }

            string useragent = _account.user_agent;
            if (string.IsNullOrEmpty(useragent)) useragent = ProviderFakeUserAgent.RandomComputer;
            string url = $"https://api.m.jd.com/api?functionId=unionPromoteLinkService&" +
                $"appid=unionpc&_={ts}&loginType=3&uuid={uuid}";

            var args = new
            {
                funName = "getCode",
                param = new
                {
                    couponLink = "",
                    isPinGou = 0,
                    materialId = result.itemId,
                    materialType = 1,
                    planId = 3479956501,
                    promotionType = 15,
                    receiveType = "cps",
                    wareUrl = wareUrl,
                    isSmartGraphics = 0,
                    command = 1,
                    ext1 = "618|pc|".UrlEncode()
                },
                lientPageId = "jingfen_pc"
            };
            string data = args.Convert2Json().UrlEncode();

            WebClientUtility client = new WebClientUtility();
            client.Proxy = _proxy;
#if DEBUG
            client.Proxy = null;
#endif
            client.SetContentType("application/x-www-form-urlencoded");
            client.AddHeaders("X-Referer-Page", "https://union.jd.com/proManager/index");
            client.AddHeaders("X-Rp-Client", "h5_1.0.0");
            client.AddHeaders("Referer", "https://union.jd.com/");
            client.AddHeaders("Origin", "https://union.jd.com");
            client.UserAgent = useragent;
            client.AddHeaders("Cookie", cookies);
            client.Post($"body={data}");
            var response = await client.RequestAsync(url, "POST", cancellationToken);

            string body = response.Body();
            var root = body.Convert2JsonElement();
            int code = root.Read<int>("code");
            string message = root.Read<string>("message");
            string shortCode = root.PathRead<string>("data.shortCode");

            if (code == 200)
            {
                result.success = true;
                result.message = "OK";
                result.shortLinkurl = shortCode;
                result.content = result.shortLinkurl;
                result.deeplink_url = GetDeeplink(result.shortLinkurl);
            }
            else
            {
                if (string.IsNullOrEmpty(body)) message = "网络异常";
                result.success = false;
                result.message = "转链失败";
                result.reason = message;
                result.shortLinkurl = wareUrl;
                result.deeplink_url = GetDeeplink(result.shortLinkurl);
                _ = new LoggerLibrary("JdUnion", "GetPromotionByCrawlerAsync").Info(body).SaveAsync();
            }
            return result;
        }

    }
}
