using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dodohold.core;
using Dataoke;
using System.Text.RegularExpressions;
using Org.BouncyCastle.Ocsp;
using System.Diagnostics;
using TencentCloud.Fmu.V20191213.Models;
using Microsoft.VisualBasic;
using System.Text.Json;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Security.Cryptography;
using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.FileSystemGlobbing.Internal;


namespace molilian.core
{
    public partial class JdUnionPlus
    {
        internal static async Task<UnionCouponDTO> JdCouponAsync(JdPoolDTO account, DataokeDTO api, string content, UnionCouponDTO result, CancellationToken cancellationToken = default)
        {

            //result.channel = data.channel;
            //result.success = data.success;
            //result.reason = data.reason;
            //result.message = data.message;
            //result.deeplink_url = data.deeplink_url;
            //result.couponAmount = data.couponAmount;
            //result.itemId = data.itemId;
            //result.pic = data.pic;


            string url = GetLink(content);
            string itemId = GetItemId(url);

            var dtkResult = DataokeCore.GetCoupon(api, account, url, itemId);
            if (dtkResult == null)
            {
                result.success = false;
                result.message = "放弃转链";
                result.reason = "转链失败";
                return result;
            }

            if (dtkResult.couponAmount == 0)
            {
                result.success = false;
                result.message = "没有优惠券";
                result.reason = "";
                return result;
            }

            result.channel = TkChannelEnum.jd;
            result.success = true;
            result.message = "success";
            result.couponAmount = dtkResult.couponAmount;
            result.deeplink_url = GetDeeplink(dtkResult.couponLink);
            result.couponAmount = dtkResult.couponAmount;
            result.itemId = dtkResult.skuId.ToString();

            if (DateTime.TryParse(dtkResult.couponStartTime, out DateTime couponStartTime))
            {
                result.couponEffectiveStartTime = couponStartTime.Convert2UnixTimestamp(true).ToString();
            }
            if (DateTime.TryParse(dtkResult.couponEndTime, out DateTime couponEndTime))
            {
                result.couponEffectiveEndTime = couponEndTime.Convert2UnixTimestamp(true).ToString();
            }

            result.pic = dtkResult.picMain;

            return result;
        }
    }
}
