using dodohold.core;
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;
using System.Diagnostics;
using System.Text.Json;
using TencentCloud.Tcm.V20210413.Models;


namespace molilian.core
{
    public partial class AlimamaPlus
    {
        public async Task<TkDataDTO> endpoint_aliyun(string content, TkDataDTO result, CancellationToken cancellationToken = default)
        {
            string item_id = content.GetContentPart("item.htm?id=", "");
            if (string.IsNullOrEmpty(item_id))
            {
                _ = new LoggerLibrary("endpoint", "aliyun")
                    .Info(content, "response null")
                    .SaveAsync();
                result.channel = TkChannelEnum.tb;
                result.link_type = LinkTypeEnum.unknown;
                result.success = false;
                result.message = "放弃转链";
                result.reason = "无效商品id";
                return result;
            }



            var plus = new AliyunPlus(_account.appkey, _account.appSecret);
            var response = await plus.GetProductInfoByIds(item_id, _account.open_pid);
            if (response == null)
            {
                _ = new LoggerLibrary("endpoint", "aliyun")
                    .Info(content, "response null")
                    .SaveAsync();
                result.channel = TkChannelEnum.tb;
                result.link_type = LinkTypeEnum.unknown;
                result.success = false;
                result.message = "fail";
                result.reason = "接口异常";
                return result;
            }

            result.success = (bool)response.Body.Success;
            if (!result.success)
            {
                result.message = "fail";
                result.reason = $"{response.Body.Code}:{response.Body.Message}";

                _ = new LoggerLibrary("endpoint", "aliyun")
                    .Info(content, result.reason)
                    .SaveAsync();

                result.channel = TkChannelEnum.tb;
                result.link_type = LinkTypeEnum.unknown;
                result.success = false;
                return result;
            }

            if (response.Body.Data.Auctions.Count == 0)
            {
                _ = new LoggerLibrary("endpoint", "aliyun")
                    .Info(content, "response null")
                    .SaveAsync();
                result.channel = TkChannelEnum.tb;
                result.link_type = LinkTypeEnum.unknown;
                result.success = false;
                result.message = "result empty";
                result.reason = string.Empty;
                result.subCode = TkSubCodeEnum.NoConvert;
                return result;
            }


            var data = response.Body.Data.Auctions[0].Result;
            result.itemId = data.InputItemId;
            result.couponAmount = (decimal)data.CouponAmount;
            result.couponEffectiveStartTime = data.CouponStartTime;
            result.couponEffectiveEndTime = data.CouponEndTime;
            result.taoToken = string.Empty;
            string url = data.Url;
            if (url.StartsWith("//")) url = $"https:{url}";
            result.shortLinkurl = url;
            result.deeplink_url = GetDeeplink(result.shortLinkurl);

            result.item_url = url;
            result.item_deeplink_url = result.deeplink_url;

            string couponShareUrl = data.CouponShareUrl;
            if (_account.useCouponLinkFirst && !string.IsNullOrEmpty(couponShareUrl))
            {
                if (couponShareUrl.StartsWith("//")) couponShareUrl = $"https:{couponShareUrl}";
                result.shortLinkurl = couponShareUrl;
                result.deeplink_url = GetDeeplink(couponShareUrl);
            }

            result.mktId = data.ItemId;
            result.itemName = data.Title;
            if (decimal.TryParse(data.PriceAfterCoupon, out decimal promotionPrice))
            {
                result.promotionPrice = promotionPrice;
            }
            if (decimal.TryParse(data.ReservePrice, out decimal reservePrice))
            {
                result.reservePrice = reservePrice;
            }
            if (decimal.TryParse(data.ZkFinalPrice, out decimal zkFinalPrice))
            {
                result.zkFinalPrice = zkFinalPrice;
            }
            result.sellerNickName = data.Nick;
            result.shopTitle = data.ShopTitle;
            string picUrl = data.PicUrl;
            if (picUrl.StartsWith("//")) picUrl = $"https:{picUrl}";
            result.pic = picUrl;

            return result;
        }

    }

}
