using dodohold.core;
using Dataoke;
using TencentCloud.Scf.V20180416.Models;
using System.Text.RegularExpressions;
using TencentCloud.Teo.V20220901.Models;
using static dodohold.core.ZTOExpress.CreateOrderArgs;
using System.Xml.Linq;

namespace molilian.core
{
    public class ParseUrlDTO
    {
        public string? skuId { get; set; }
        public string? itemUrl { get; set; }
        public string? hasCoupon { get; set; }
        public string? couponLink { get; set; }
    }
    public class GoodsDetailDTO
    {
        public decimal actualPrice { get; set; }
        public int cid1 { get; set; }
        public string cid1Name { get; set; }
        public int cid2 { get; set; }
        public string cid2Name { get; set; }
        public int cid3 { get; set; }
        public string cid3Name { get; set; }
        public int comments { get; set; }
        public decimal commission { get; set; }
        public string commissionEndTime { get; set; }
        public decimal commissionShare { get; set; }
        public string commissionStartTime { get; set; }
        public decimal couponAmount { get; set; }
        public string couponConditions { get; set; }
        public string couponEndTime { get; set; }
        public string couponLink { get; set; }
        public int couponReceiveCount { get; set; }
        public int couponRemainCount { get; set; }
        public string couponStartTime { get; set; }
        public int couponTotalCount { get; set; }
        public int couponType { get; set; }
        public string couponUseStartTime { get; set; }
        public string couponUserEndTime { get; set; }
        public string[] detailImages { get; set; }
        public string extensionContent { get; set; }
        public int goodsCommentShare { get; set; }
        public int inOrderCount30Days { get; set; }
        public int isFlagship { get; set; }
        public int isFreeFreightRisk { get; set; }
        public int isFreeShipping { get; set; }
        public int isOwner { get; set; }
        public int isSeckill { get; set; }
        public string itemId { get; set; }
        public int lowest { get; set; }
        public string materialUrl { get; set; }
        public decimal originPrice { get; set; }
        public string picMain { get; set; }
        public decimal plusCommissionShare { get; set; }
        public string productIntro { get; set; }
        public int shopId { get; set; }
        public string shopName { get; set; }
        public long skuId { get; set; }
        public string skuName { get; set; }
        public string[] smallImages { get; set; }
    }
    public partial class DataokePlus
    {
        string _app_key = string.Empty;
        string _app_secret = string.Empty;

        public DataokePlus(string app_key, string app_secret)
        {
            _app_key = app_key;
            _app_secret = app_secret;
        }
        public string JDParse(string unionId, string materialId, string couponUrl = null)
        {
            string version = "v1.0.0";
            string api = "https://openapi.dataoke.com/api/dels/jd/kit/promotion-union-convert";
            DtkApp app = new(_app_key, _app_secret);
            ApiParameters apiParameters = new();
            apiParameters.Add("unionId", unionId);
            apiParameters.Add("materialId", materialId);
            if (!string.IsNullOrEmpty(couponUrl)) apiParameters.Add("couponUrl", couponUrl);

            var log = new LoggerLibrary("dataoke", "promotion-union-convert").Info(unionId, materialId);
            string body = app.Get(api, version, apiParameters);
            _ = log.Info(body).SaveAsync();
            //var root = body.Convert2JsonElement();
            return body;
        }

        public GoodsDetailDTO? GetDetails(params string[] skuIds)
        {
            GoodsDetailDTO? result = null;
            string body = string.Empty;
            try
            {
                string version = "v1.0.0";
                string api = "https://openapi.dataoke.com/api/dels/jd/goods/get-details";
                DtkApp app = new(_app_key, _app_secret);
                ApiParameters apiParameters = new();
                apiParameters.Add("skuIds", string.Join(',', skuIds));


                var log = new LoggerLibrary("dataoke", "details").Info(string.Join(',', skuIds));
                body = app.Get(api, version, apiParameters);
                _ = log.Info(body).SaveAsync();


                var root = body.Convert2JsonElement();
                int code = root.Read<int>("code", -1);
                if (code == 0)
                {
                    var _goods = root.ElementRead("data")[0].GetRawText();
                    result = _goods.Convert2Object<GoodsDetailDTO>();
                }
            }
            catch (Exception ex)
            {
                _ = new LoggerLibrary("dtk_error", "fail")
                    .Info(string.Join(',', skuIds), body)
                    .Info(ex.Message, ex.StackTrace)
                    .SaveAsync();

                NotifyCore.Notify(new NifyMessage
                {
                    message = $"【大淘客异常】\n\n{ex.Message}\n{ex.StackTrace}",
                    priority = NifyMessagePriority.high,
                    tags = ["red_circle"]
                });
            }

            return result;
        }

        public GoodsDetailDTO? GetGoods(string url)
        {
            ParseUrlDTO? sku_info = ParseUrl(url);
            if (sku_info == null) { return null; }

            GoodsDetailDTO? result = GetDetails(sku_info.skuId);
            return result;
        }

        public ParseUrlDTO? ParseUrl(string url)
        {
            ParseUrlDTO? result = null;
            try
            {
                string version = "v1.0.0";
                string api = "https://openapi.dataoke.com/api/dels/jd/kit/parseUrl";
                DtkApp app = new(_app_key, _app_secret);
                ApiParameters apiParameters = new();
                apiParameters.Add("url", url);

                var log = new LoggerLibrary("dataoke", "parseUrl").Info(url);
                string body = app.Get(api, version, apiParameters);
                _ = log.Info(body).SaveAsync();

                var root = body.Convert2JsonElement();
                int code = root.Read<int>("code", -1);
                if (code == 0)
                {
                    result = new ParseUrlDTO
                    {
                        skuId = root.PathRead("data.skuId", string.Empty),
                        itemUrl = root.PathRead("data.itemUrl", string.Empty),
                        hasCoupon = root.PathRead("data.hasCoupon", string.Empty),
                        couponLink = root.PathRead("data.couponLink", string.Empty)
                    };
                }
            }
            catch (Exception e) { }

            return result;
        }
    }


}

