using dodohold.core;
using System.Diagnostics;
using System.Text.Json;


namespace molilian.core
{
    public partial class AlimamaPlus
    {
        public async Task<TkDataDTO> endpoint_moose_share(string content, TkDataDTO result, CancellationToken cancellationToken = default)
        {
            string t = $"{DateTime.Now.Convert2UnixTimestamp(true)}";

            Random random = new();
            double randomNumber = random.NextDouble();
            string randomString = randomNumber.ToString()[2..];

            string cna = _cookies.GetContentPart("cna=", ";");
            string firstFiveCharacters = cna[..Math.Min(5, cna.Length)];

            //var variableMap = new
            //{
            //    url = content,
            //    union_lens = $"b_pvid:a219t._portal_v2_tool_links_page_home_index_htm_{t}_{randomString}_{firstFiveCharacters}",
            //    lensScene = "PUB",
            //    spmB = "_portal_v2_tool_links_page_home_index_htm"
            //}.Convert2Json(true).UrlEncode();

            string item_id = content.GetContentPart("item.htm?id=", "");
            var bizParam = new
            {
                itemId = item_id,
                pid = _account.refpid,
                creativeType = "creativeAll"
            }.Convert2Json(true).UrlEncode();


            string url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/union.moose.share.entry";
            string post = $"bizType=share&bizParam={bizParam}";

            Stopwatch stopwatch = Stopwatch.StartNew();
            stopwatch.Start();
            var client = new WebClientUtility().SetContentType("application/x-www-form-urlencoded")
                .AddHeaders("Origin", "https://pub.alimama.com")
                .AddHeaders("X-Requested-With", "XMLHttpRequest")
                .AddHeaders("Cookie", _cookies);
            client.Proxy = _proxy;
#if DEBUG
            client.Proxy = null;
#endif
            if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
            client.Timeout = GetRequestTimeout2(result.elapsedTime2);
            //var response = client.Request(url);

#if DEBUG
            var response = await client.Post(post).RequestAsync(url, "POST");
#else
            var response = await client.Post(post).RequestAsync(url, "POST", cancellationToken);
#endif

            stopwatch.Stop();
            result.elapsedTime3 = (int)stopwatch.ElapsedMilliseconds;



            var body = string.Empty;
            try
            {
                if (!response.Successed)
                {
                    result.channel = TkChannelEnum.tb;
                    result.link_type = LinkTypeEnum.unknown;
                    result.success = false;
                    result.message = "fail";

                    if (response.ResponseException != null)
                    {
                        _ = new LoggerLibrary("api_error", "fail")
                            .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
                            .SaveAsync();
                        throw response.ResponseException;
                    }

                    return result;
                }
                if (response.ResponseMessage.StatusCode == System.Net.HttpStatusCode.Found)
                {
                    _ = new LoggerLibrary("api_error", "fail")
                        .Info($"302:{response.ResponseMessage.Headers.Location}")
                        .SaveAsync();
                    throw new Exception("302 Found, was canceled");
                }
                JsonElement root;
                try
                {
                    body = response.Body();
                    //{"success":false,"message":"result empty"}
                    root = body.Convert2JsonElement();
                }
                catch (Exception ex)
                {
                    _ = new LoggerLibrary("api_error", "fail")
                        .Info(ex.Message, ex.StackTrace)
                        .SaveAsync();

                    if (body.Contains("https://g.alicdn.com/sd/punish/waf_block.html"))
                    {
                        await TkEndpointManager.SuspendAsync(_accountId, "moose_share", TkEndpointManager.SuspendReason.Passive);

                        result.channel = TkChannelEnum.tb;
                        result.link_type = LinkTypeEnum.unknown;
                        result.subCode = TkSubCodeEnum.ParseDeny;
                        result.success = false;
                        result.message = "waf_block";
                        result.reason = "接口风控";
                        return result;
                    }
                    throw new Exception(body);
                }



                bool success = root.Read<bool>("success", false);
                string err_url = root.Read("url", string.Empty);
                if (!success)
                {
                    //{"rgv587_flag": "sm", "url": "https://bixi.alicdn.com/punish/punish:resource:template:baba:default_35969158.html?qrcode=oPTbncSdl-B_dxCchZVrhw|aBW9wQ|MmZvng_0|BrNc-A&uuid=a0f4db9dc49d97e07f77109c85956b87&action=deny&origin=https%3A%2F%2Fpub.alimama.com%2Fopenapi%2Fparam2%2F1%2Fgateway.unionpub%2Funion.moose.share.entry&pureDenyWait="}
                    if (err_url.Contains("bixi.alicdn.com"))
                    {
                        await TkEndpointManager.SuspendAsync(_accountId, "moose_share", TkEndpointManager.SuspendReason.Passive);

                        result.channel = TkChannelEnum.tb;
                        result.link_type = LinkTypeEnum.unknown;
                        result.subCode = TkSubCodeEnum.ParseDeny;
                        result.success = false;
                        result.message = "waf_block";
                        result.reason = "接口风控";
                        return result;
                    }
                }

                string message = root.Read("message", string.Empty);
                string info_message = root.PathRead("msgInfo", string.Empty);
                if (!string.IsNullOrEmpty(info_message)) message = info_message;

                switch (message)
                {
                    case "detail result is empty itemId={}":
                    case "itemId params error":
                        message = "result empty";
                        result.subCode = TkSubCodeEnum.NoConvert;
                        break;

                }

                JsonElement item = default;
                JsonElement creativityCopy = default;

                if (success)
                {
                    var data = root.ElementRead("model");
                    var resultList = data.ElementRead("resultList");
                    if (resultList.ValueKind != JsonValueKind.Array)
                    {
                        _ = new LoggerLibrary("api_error", "fail")
                            .Info("resultList Undefined", body)
                            .SaveAsync();
                        throw new Exception("resultList Undefined");
                    }
                    item = resultList[0];

                    var creativityCopyArr = item.ElementRead("creativityCopy");
                    creativityCopy = creativityCopyArr[0];

                }


                string cpsShortToken = creativityCopy.Read("cpsShortToken", string.Empty);
                string cpsShortLinkUrl = creativityCopy.Read("cpsShortLinkUrl", string.Empty);
                string itemName = creativityCopy.Read("itemName", string.Empty);
                string taoToken = $"{cpsShortToken} {cpsShortLinkUrl} {itemName}";

                string shortLinkurl = creativityCopy.Read("cpsShortLinkUrl", string.Empty);

                string couponLinkTaoToken = item.Read("couponLinkTaoToken", string.Empty);
                string couponShortLinkUrl = item.Read("couponShortLinkUrl", string.Empty);
                decimal couponAmount = item.Read<decimal>("couponAmount", 0);
                string couponEffectiveEndTime = item.Read("couponEffectiveEndTime", string.Empty);
                string couponEffectiveStartTime = item.Read("couponEffectiveStartTime", string.Empty);

                string itemId = item.Read("itemId", string.Empty);
                itemId = item_id;//用数字id覆盖
                string mktId = item.Read("outputMktId", string.Empty);
                decimal promotionPrice = item.Read<decimal>("promotionPrice", 0);
                string sellerNickName = item.Read("sellerNickName", string.Empty);
                string shopTitle = item.Read("shopTitle", string.Empty);
                string pic = item.Read("pic", string.Empty);
                if (pic.StartsWith("//")) pic = "https:" + pic;

                string qrCodeUrl = item.Read("qrCodeUrl", string.Empty);

                string itemTaoToken = taoToken;

                if (response.ResponseMessage != null)
                {

                    switch (response.ResponseMessage.StatusCode)
                    {
                        case System.Net.HttpStatusCode.OK:
                            {
                                if (couponAmount > 0 && !string.IsNullOrEmpty(couponLinkTaoToken) && !string.IsNullOrEmpty(couponShortLinkUrl))
                                {
                                    taoToken = couponLinkTaoToken;
                                    shortLinkurl = couponShortLinkUrl;
                                }
                                string shortLink = GetLink(taoToken);
                                content = ReplaceUrls(content, shortLink);
                            }
                            break;
                        case System.Net.HttpStatusCode.Found:
                            string location = response.ResponseMessage.Headers.Location.OriginalString;
                            if (!string.IsNullOrEmpty(location) && location.Contains("www.alimama.com/member/login.htm"))
                            {
                                success = false;
                                message = "nologin";
                            }
                            break;
                        default:
                            {
                                success = false;
                                message = "other";
                            }
                            break;
                    }
                }
                if (!success)
                {
                    shortLinkurl = GetLink(content);
                    switch (message)
                    {
                        case "该商品已经下架或未加入淘宝客":
                        case "该链接不支持转化，请更换链接尝试":
                        case "result empty":
                            result.subCode = TkSubCodeEnum.NoConvert;
                            break;
                        case "网络错误":
                            result.subCode = TkSubCodeEnum.NetError;
                            break;
                        default:
                            var _headers = response.ResponseMessage.Headers;
                            var headers = "";
                            foreach (var h in _headers)
                            {
                                foreach (var Value in h.Value)
                                {
                                    headers += $"{h.Key}: {Value}\n";
                                }
                            }
                            _ = new LoggerLibrary("api_error", "fail")
                                .Info(message, content)
                                .Info(headers, body)
                                .SaveAsync();
                            break;
                    }
                }

                shortLinkurl = GetLink(taoToken);
                string deeplink_url = GetDeeplink(shortLinkurl);

                //补充原商品链接 2025-06-01
                result.item_url = GetLink(itemTaoToken);
                result.item_deeplink_url = GetDeeplink(result.item_url);


                result.channel = TkChannelEnum.tb;

                if (success)
                {
                    result.subCode = TkSubCodeEnum.Success;
                    switch (result.link_type)
                    {
                        case LinkTypeEnum.profile:
                        case LinkTypeEnum.goods:
                        case LinkTypeEnum.video:
                        case LinkTypeEnum.live:
                            break;
                        default:
                            result.link_type = LinkTypeEnum.goods;
                            break;
                    }
                }

                result.success = success;
                result.message = message;
                result.reason = string.Empty;

                result.content = content;
                result.couponAmount = couponAmount;
                result.couponEffectiveEndTime = couponEffectiveEndTime;
                result.couponEffectiveStartTime = couponEffectiveStartTime;
                result.taoToken = taoToken;
                if (!string.IsNullOrEmpty(shortLinkurl))
                {
                    result.shortLinkurl = shortLinkurl;
                    result.deeplink_url = deeplink_url;
                }
                result.mktId = mktId;

                result.itemId = itemId;
                result.itemName = itemName;
                result.promotionPrice = promotionPrice;
                result.sellerNickName = sellerNickName;
                result.shopTitle = shopTitle;
                result.pic = pic;
                result.qrCodeUrl = qrCodeUrl;
            }
            catch (Exception ex)
            {
                if (response.ResponseMessage?.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    if (body.Contains("<p>抱歉！页面无法访问……</p>"))
                    {
                        throw new APIException("抱歉！页面无法访问……");
                    }
                }
                throw;
            }
            return result;
        }

    }

}
