using System;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Flurl;
using Flurl.Http;

namespace SKIT.FlurlHttpClient.Wechat.Api
{
    public static class WechatApiClientExecuteCgibinBusinessExtensions
    {
        /// <summary>
        /// <para>异步调用 [POST] /cgi-bin/business/register 接口。</para>
        /// <para>REF: https://developers.weixin.qq.com/miniprogram/introduction/custom.html#%E5%88%9B%E5%BB%BA%E5%95%86%E6%88%B7 </para>
        /// </summary>
        /// <param name="client"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task<Models.CgibinBusinessRegisterResponse> ExecuteCgibinBusinessRegisterAsync(this WechatApiClient client, Models.CgibinBusinessRegisterRequest request, CancellationToken cancellationToken = default)
        {
            if (client is null) throw new ArgumentNullException(nameof(client));
            if (request is null) throw new ArgumentNullException(nameof(request));

            IFlurlRequest flurlReq = client
                .CreateRequest(request, HttpMethod.Post, "cgi-bin", "business", "register")
                .SetQueryParam("access_token", request.AccessToken);

            return await client.SendRequestWithJsonAsync<Models.CgibinBusinessRegisterResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
        }

        /// <summary>
        /// <para>异步调用 [POST] /cgi-bin/business/update 接口。</para>
        /// <para>REF: https://developers.weixin.qq.com/miniprogram/introduction/custom.html#%E6%9B%B4%E6%96%B0%E5%95%86%E6%88%B7%E4%BF%A1%E6%81%AF </para>
        /// </summary>
        /// <param name="client"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task<Models.CgibinBusinessUpdateResponse> ExecuteCgibinBusinessUpdateAsync(this WechatApiClient client, Models.CgibinBusinessUpdateRequest request, CancellationToken cancellationToken = default)
        {
            if (client is null) throw new ArgumentNullException(nameof(client));
            if (request is null) throw new ArgumentNullException(nameof(request));

            IFlurlRequest flurlReq = client
                .CreateRequest(request, HttpMethod.Post, "cgi-bin", "business", "update")
                .SetQueryParam("access_token", request.AccessToken);

            return await client.SendRequestWithJsonAsync<Models.CgibinBusinessUpdateResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
        }

        /// <summary>
        /// <para>异步调用 [POST] /cgi-bin/business/get 接口。</para>
        /// <para>REF: https://developers.weixin.qq.com/miniprogram/introduction/custom.html#%E6%8B%89%E5%8F%96%E5%8D%95%E4%B8%AA%E5%95%86%E6%88%B7%E4%BF%A1%E6%81%AF </para>
        /// </summary>
        /// <param name="client"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task<Models.CgibinBusinessGetResponse> ExecuteCgibinBusinessGetAsync(this WechatApiClient client, Models.CgibinBusinessGetRequest request, CancellationToken cancellationToken = default)
        {
            if (client is null) throw new ArgumentNullException(nameof(client));
            if (request is null) throw new ArgumentNullException(nameof(request));

            IFlurlRequest flurlReq = client
                .CreateRequest(request, HttpMethod.Post, "cgi-bin", "business", "get")
                .SetQueryParam("access_token", request.AccessToken);

            return await client.SendRequestWithJsonAsync<Models.CgibinBusinessGetResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
        }

        /// <summary>
        /// <para>异步调用 [POST] /cgi-bin/business/list 接口。</para>
        /// <para>REF: https://developers.weixin.qq.com/miniprogram/introduction/custom.html#%E6%8B%89%E5%8F%96%E5%A4%9A%E4%B8%AA%E5%95%86%E6%88%B7%E4%BF%A1%E6%81%AF </para>
        /// </summary>
        /// <param name="client"></param>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public static async Task<Models.CgibinBusinessListResponse> ExecuteCgibinBusinessListAsync(this WechatApiClient client, Models.CgibinBusinessListRequest request, CancellationToken cancellationToken = default)
        {
            if (client is null) throw new ArgumentNullException(nameof(client));
            if (request is null) throw new ArgumentNullException(nameof(request));

            IFlurlRequest flurlReq = client
                .CreateRequest(request, HttpMethod.Post, "cgi-bin", "business", "list")
                .SetQueryParam("access_token", request.AccessToken);

            return await client.SendRequestWithJsonAsync<Models.CgibinBusinessListResponse>(flurlReq, data: request, cancellationToken: cancellationToken);
        }
    }
}
