using BBWYB.Common.Http;
using com.alibaba.openapi.client.entity;
using com.alibaba.openapi.client.policy;
using Newtonsoft.Json.Linq;

namespace com.alibaba.openapi.client
{
    public class SyncAPIClient
    {
        private ClientPolicy clientPolicy;
        private RestApiService restApiService;
        private http.HttpClient alibabaHttpClient;

        public SyncAPIClient(String appKey, String appSecret)
        {
            this.clientPolicy = new ClientPolicy();
            this.clientPolicy.AppKey = appKey;
            this.clientPolicy.SecretKey = appSecret;
        }

        public SyncAPIClient(String appKey, String appSecret, RestApiService restApiService)
        {
            this.clientPolicy = new ClientPolicy();
            this.clientPolicy.AppKey = appKey;
            this.clientPolicy.SecretKey = appSecret;
            this.restApiService = restApiService;
            this.alibabaHttpClient = new http.HttpClient(clientPolicy, restApiService);
        }

        public SyncAPIClient(String appKey, String appSecret, String gatewayHost)
        {
            this.clientPolicy = new ClientPolicy();
            this.clientPolicy.AppKey = appKey;
            this.clientPolicy.SecretKey = appSecret;
            this.clientPolicy.ServerHost = gatewayHost;
        }

        public SyncAPIClient(ClientPolicy clientPolicy)
        {
            this.clientPolicy = clientPolicy;
        }

        public JObject NewRequest(Request request, RequestPolicy policy)
        {
            return alibabaHttpClient.NewRequest(request, policy);
        }

        public T send<T>(Request request, RequestPolicy policy)
        {
            http.HttpClient httpClient = new http.HttpClient(clientPolicy);
            T result = httpClient.request<T>(request, policy);
            return result;
        }

        public Res execute<Res>(GatewayAPIRequest gatewayAPIRequest, String accessToken)
        {
            http.HttpClient httpClient = new http.HttpClient(clientPolicy);
            RequestPolicy policy = new RequestPolicy();
            policy.UseHttps = true;
            Request request = new Request();
            request.ApiId = gatewayAPIRequest.ApiId;
            request.RequestEntity = gatewayAPIRequest;
            request.AccessToken = accessToken;
            Res result = httpClient.request<Res>(request, policy);
            return result;
        }

        public AuthorizationToken getToken(String code)
        {

            APIId apiId = new APIId();
            apiId.Name = "getToken";
            apiId.NamespaceValue = "system.oauth2";
            apiId.Version = 1;

            Request request = new Request();
            request.ApiId = apiId;

            request.AddtionalParams["code"] = code;
            request.AddtionalParams["grant_type"] = "authorization_code";
            request.AddtionalParams["need_refresh_token"] = true;
            request.AddtionalParams["client_id"] = clientPolicy.AppKey;
            request.AddtionalParams["client_secret"] = clientPolicy.SecretKey;
            request.AddtionalParams["redirect_uri"] = "default";
            RequestPolicy oauthPolicy = new RequestPolicy();
            oauthPolicy.UseHttps = true;

            return this.send<AuthorizationToken>(request, oauthPolicy);
        }

        public AuthorizationToken refreshToken(String refreshToken)
        {

            APIId apiId = new APIId();
            apiId.Name = "getToken";
            apiId.NamespaceValue = "system.oauth2";
            apiId.Version = 1;

            Request request = new Request();
            request.ApiId = apiId;

            request.AddtionalParams["refreshToken"] = refreshToken;
            request.AddtionalParams["grant_type"] = "refresh_token";
            request.AddtionalParams["client_id"] = clientPolicy.AppKey;
            request.AddtionalParams["client_secret"] = clientPolicy.SecretKey;
            request.AddtionalParams["redirect_uri"] = "default";
            RequestPolicy oauthPolicy = new RequestPolicy();
            oauthPolicy.UseHttps = true;
            return this.send<AuthorizationToken>(request, oauthPolicy);


        }
    }
}