using AlibabaCloud.OpenApiClient.Models;
using AlibabaCloud.SDK.ImageSearch20210501.Models;
using AlibabaCloud.SDK.ImageSearch20210501;
using Tea;
using dodohold.core;
using Environment = System.Environment;
using Topsdk.Top;
using Topsdk.Top.Defaultability;
using Topsdk.Top.Defaultability.Domain;
using Topsdk.Top.Defaultability.Request;
using Topsdk.Top.Defaultability.Response;


namespace molilian.core
{

    public partial class AliyunPlus
    {

        public async Task<TaobaoTbkDgGeneralLinkConvertResponse> TaobaoTbkDgGeneralLinkConvert(long adzone, string page_id_list, List<string> item_id_list)
        {
            if (string.IsNullOrEmpty(page_id_list)) return null;
            if (item_id_list?.Count == 0) return null;

            try
            {
                string gateway_url = "http://eco.taobao.com/router/rest";

                // create Client
                ITopApiClient client = new DefaultTopApiClient(_app_key, _app_secret, gateway_url, _timeout, _timeout);

                Defaultability apiPackage = new(client);

                var taobaoTbkDgGeneralLinkConvertTargetItemDTO = new TaobaoTbkDgGeneralLinkConvertTargetItemDTO
                {
                    ItemIdList = item_id_list
                };

                // create request
                var request = new TaobaoTbkDgGeneralLinkConvertRequest
                {
                    AdzoneId = adzone,
                    PageIdList = page_id_list.Split(','),
                    TargetItem = taobaoTbkDgGeneralLinkConvertTargetItemDTO
                };


                var response = apiPackage.TaobaoTbkDgGeneralLinkConvert(request);
                //AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new();

                //Config config = new()
                //{
                //    AccessKeyId = _app_key,
                //    AccessKeySecret = _app_secret,
                //    Endpoint = _end_point,
                //    ConnectTimeout = _timeout,
                //    RegionId = "cn-shanghai"
                //};
                //Client client = new(config);
                //GetProductInfoByIdsRequest request = new()
                //{
                //    ItemIds = itemIds,
                //    Pid = pid,
                //    Fields = "itemId,Title,Nick,SellerId,ShortTitle,SubTitle,PicUrl,ReservePrice,ZkFinalPrice,PriceAfterCoupon,CouponTotalCount,CouponRemainCount,CouponStartTime,CouponEndTime,CouponStartFee,CouponAmount,CouponInfo,CommissionRate,CouponShareUrl,DeeplinkCouponShareUrl,Url,DeeplinkUrl"
                //};

                //var response = await client.GetProductInfoByIdsWithOptionsAsync(request, runtime);
                return response;
            }
            catch (Exception _error)
            {
                TeaException error = new TeaException(new Dictionary<string, object>
                {
                    { "message", _error.Message }
                });
                // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
                // 错误 message
                Console.WriteLine(error.Message);
                // 诊断地址
                Console.WriteLine(error.Data["Recommend"]);
                AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message);


                LoggerLibrary log = new("TaobaoTbkDgGeneralLinkConvert", "error");
                log.Info(error.Message);
                log.Info(error.Data["Recommend"].Convert2Json());
                _ = log.SaveAsync();
            }
            return null;
        }

    }

}