using dodohold.core;


namespace molilian.core
{
    public partial class VeapiPlus
    {

        private string _accountName;
        private string _key;
        private string _sessionKey;
        public VeapiPlus(string accountName, string key, string sessionKey)
        {
            _accountName = accountName;
            _key = key;
            _sessionKey = sessionKey;
        }

        public TkDataDTO UnionParse(string content, ref TkDataDTO result)
        {
            string para = content.UrlEncode();
            string url = $"http://api.veapi.cn/tbk/hcapi?vekey={_key}&sessionkey={_sessionKey}&para={para}";

            var response = new WebClientUtility().Request(url);

            var body = response.Body();
            var root = body.Convert2JsonElement();

            int error = root.Read<int>("error", 0);
            bool success = error == 0;
            string message = root.Read("msg", string.Empty);

            string taoToken = root.PathRead("data.ios_tbk_pwd", string.Empty);
            string num_iid = root.PathRead("data.num_iid", string.Empty);
            decimal couponAmount = root.PathRead<decimal>("data.coupon_amount", 0);

            string shortLinkurl = root.PathRead("data.coupon_short_url", string.Empty);

            string shortLink = AlimamaPlus.GetLink(taoToken);
            content = AlimamaPlus.ReplaceUrls(content, shortLink);

            if (!success)
            {
                shortLinkurl = AlimamaPlus.GetLink(content);
            }
            string deeplink_url = AlimamaPlus.GetDeeplink(shortLinkurl);


            result.channel = TkChannelEnum.tb;
            result.accountName = _accountName;
            result.success = success;
            result.message = message;
            result.content = content;
            result.couponAmount = couponAmount;
            result.taoToken = taoToken;
            result.shortLinkurl = shortLinkurl;
            result.deeplink_url = deeplink_url;
            result.num_iid = num_iid;
            return result;

        }


        public async Task<TkDataDTO> UnionParseAsync(string content, TkDataDTO result, CancellationToken cancellationToken)
        {
            string para = content.UrlEncode();
            string url = $"http://api.veapi.cn/tbk/hcapi?vekey={_key}&sessionkey={_sessionKey}&para={para}";

            var response = await new WebClientUtility().RequestAsync(url, "GET", cancellationToken);

            var body = response.Body();
            var root = body.Convert2JsonElement();

            int error = root.Read<int>("error", 0);
            bool success = error == 0;
            string message = root.Read("msg", string.Empty);

            string taoToken = root.PathRead("data.ios_tbk_pwd", string.Empty);
            string num_iid = root.PathRead("data.num_iid", string.Empty);
            decimal couponAmount = root.PathRead<decimal>("data.coupon_amount", 0);

            string shortLinkurl = root.PathRead("data.coupon_short_url", string.Empty);

            string shortLink = AlimamaPlus.GetLink(taoToken);
            content = AlimamaPlus.ReplaceUrls(content, shortLink);

            if (!success)
            {
                shortLinkurl = AlimamaPlus.GetLink(content);
            }
            string deeplink_url = AlimamaPlus.GetDeeplink(shortLinkurl);


            result.channel = TkChannelEnum.tb;
            result.accountName = _accountName;
            result.success = success;
            result.message = message;
            result.content = content;
            result.couponAmount = couponAmount;
            result.taoToken = taoToken;
            result.shortLinkurl = shortLinkurl;
            result.deeplink_url = deeplink_url;
            result.num_iid = num_iid;
            return result;

        }

    }

}
