using Topsdk.Top.Ability304.Request;
using Topsdk.Top.Ability304.Response;
using Topsdk.Util;

namespace Topsdk.Top.Ability304;


public class Ability304
{

    private ITopApiClient topApiClient;

    public Ability304(ITopApiClient topApiClient)
    {
        this.topApiClient = topApiClient;
    }

    /*
        获取ISV发起请求服务器IP
        taobao.appip.get
    */
    public TaobaoAppipGetResponse TaobaoAppipGet(TaobaoAppipGetRequest request)
    {
        var jsonResult = topApiClient.Execute(request.GetApiCode(),request.ToRequestParam(),request.ToFileParam());
        if (string.IsNullOrEmpty(jsonResult))
        {
            throw new Exception("error");
        }
        var resultObj = TopUtils.parseResultJson<TaobaoAppipGetResponse>(jsonResult);
        if(string.IsNullOrEmpty(resultObj.Body))
        {
            resultObj.Body = jsonResult;
        }
        return resultObj;
    }
    /*
        业务文件获取
        taobao.files.get
    */
    public TaobaoFilesGetResponse TaobaoFilesGet(TaobaoFilesGetRequest request)
    {
        var jsonResult = topApiClient.Execute(request.GetApiCode(),request.ToRequestParam(),request.ToFileParam());
        if (string.IsNullOrEmpty(jsonResult))
        {
            throw new Exception("error");
        }
        var resultObj = TopUtils.parseResultJson<TaobaoFilesGetResponse>(jsonResult);
        if(string.IsNullOrEmpty(resultObj.Body))
        {
            resultObj.Body = jsonResult;
        }
        return resultObj;
    }
    /*
        刷新Access Token
        taobao.top.auth.token.refresh
    */
    public TaobaoTopAuthTokenRefreshResponse TaobaoTopAuthTokenRefresh(TaobaoTopAuthTokenRefreshRequest request)
    {
        var jsonResult = topApiClient.Execute(request.GetApiCode(),request.ToRequestParam(),request.ToFileParam());
        if (string.IsNullOrEmpty(jsonResult))
        {
            throw new Exception("error");
        }
        var resultObj = TopUtils.parseResultJson<TaobaoTopAuthTokenRefreshResponse>(jsonResult);
        if(string.IsNullOrEmpty(resultObj.Body))
        {
            resultObj.Body = jsonResult;
        }
        return resultObj;
    }
    /*
        获取Access Token
        taobao.top.auth.token.create
    */
    public TaobaoTopAuthTokenCreateResponse TaobaoTopAuthTokenCreate(TaobaoTopAuthTokenCreateRequest request)
    {
        var jsonResult = topApiClient.Execute(request.GetApiCode(),request.ToRequestParam(),request.ToFileParam());
        if (string.IsNullOrEmpty(jsonResult))
        {
            throw new Exception("error");
        }
        var resultObj = TopUtils.parseResultJson<TaobaoTopAuthTokenCreateResponse>(jsonResult);
        if(string.IsNullOrEmpty(resultObj.Body))
        {
            resultObj.Body = jsonResult;
        }
        return resultObj;
    }
    /*
        获取授权账号对应的OpenUid
        taobao.openuid.get
    */
    public TaobaoOpenuidGetResponse TaobaoOpenuidGet(TaobaoOpenuidGetRequest request,String session)
    {
        var jsonResult = topApiClient.Execute(request.GetApiCode(),request.ToRequestParam(),request.ToFileParam(),session);
        if (string.IsNullOrEmpty(jsonResult))
        {
            throw new Exception("error");
        }
        var resultObj = TopUtils.parseResultJson<TaobaoOpenuidGetResponse>(jsonResult);
        if(string.IsNullOrEmpty(resultObj.Body))
        {
            resultObj.Body = jsonResult;
        }
        return resultObj;
    }
    /*
        通过订单获取对应买家的openUID
        taobao.openuid.get.bytrade
    */
    public TaobaoOpenuidGetBytradeResponse TaobaoOpenuidGetBytrade(TaobaoOpenuidGetBytradeRequest request,String session)
    {
        var jsonResult = topApiClient.Execute(request.GetApiCode(),request.ToRequestParam(),request.ToFileParam(),session);
        if (string.IsNullOrEmpty(jsonResult))
        {
            throw new Exception("error");
        }
        var resultObj = TopUtils.parseResultJson<TaobaoOpenuidGetBytradeResponse>(jsonResult);
        if(string.IsNullOrEmpty(resultObj.Body))
        {
            resultObj.Body = jsonResult;
        }
        return resultObj;
    }
    /*
        TOPDNS配置
        taobao.httpdns.get
    */
    public TaobaoHttpdnsGetResponse TaobaoHttpdnsGet(TaobaoHttpdnsGetRequest request)
    {
        var jsonResult = topApiClient.Execute(request.GetApiCode(),request.ToRequestParam(),request.ToFileParam());
        if (string.IsNullOrEmpty(jsonResult))
        {
            throw new Exception("error");
        }
        var resultObj = TopUtils.parseResultJson<TaobaoHttpdnsGetResponse>(jsonResult);
        if(string.IsNullOrEmpty(resultObj.Body))
        {
            resultObj.Body = jsonResult;
        }
        return resultObj;
    }
    /*
        获取TOP通道解密秘钥
        taobao.top.secret.get
    */
    public TaobaoTopSecretGetResponse TaobaoTopSecretGet(TaobaoTopSecretGetRequest request)
    {
        var jsonResult = topApiClient.Execute(request.GetApiCode(),request.ToRequestParam(),request.ToFileParam());
        if (string.IsNullOrEmpty(jsonResult))
        {
            throw new Exception("error");
        }
        var resultObj = TopUtils.parseResultJson<TaobaoTopSecretGetResponse>(jsonResult);
        if(string.IsNullOrEmpty(resultObj.Body))
        {
            resultObj.Body = jsonResult;
        }
        return resultObj;
    }
    /*
        通过mixnick转换openuid
        taobao.openuid.get.bymixnick
    */
    public TaobaoOpenuidGetBymixnickResponse TaobaoOpenuidGetBymixnick(TaobaoOpenuidGetBymixnickRequest request)
    {
        var jsonResult = topApiClient.Execute(request.GetApiCode(),request.ToRequestParam(),request.ToFileParam());
        if (string.IsNullOrEmpty(jsonResult))
        {
            throw new Exception("error");
        }
        var resultObj = TopUtils.parseResultJson<TaobaoOpenuidGetBymixnickResponse>(jsonResult);
        if(string.IsNullOrEmpty(resultObj.Body))
        {
            resultObj.Body = jsonResult;
        }
        return resultObj;
    }
    /*
        sdk信息回调
        taobao.top.sdk.feedback.upload
    */
    public TaobaoTopSdkFeedbackUploadResponse TaobaoTopSdkFeedbackUpload(TaobaoTopSdkFeedbackUploadRequest request)
    {
        var jsonResult = topApiClient.Execute(request.GetApiCode(),request.ToRequestParam(),request.ToFileParam());
        if (string.IsNullOrEmpty(jsonResult))
        {
            throw new Exception("error");
        }
        var resultObj = TopUtils.parseResultJson<TaobaoTopSdkFeedbackUploadResponse>(jsonResult);
        if(string.IsNullOrEmpty(resultObj.Body))
        {
            resultObj.Body = jsonResult;
        }
        return resultObj;
    }

    public ITopApiClient TopApiClient
    {
        get => topApiClient;
        set => topApiClient = value ?? throw new ArgumentNullException(nameof(value));
    }
}
