package com.ruoyi.newwxpay.transfer;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.cipher.PrivacyDecryptor;
import com.wechat.pay.java.core.cipher.PrivacyEncryptor;
import com.wechat.pay.java.core.http.*;
import com.wechat.pay.java.core.util.GsonUtil;

import java.util.Objects;

public class TransferSingleService {

    private final HttpClient httpClient;
    private final HostName hostName;
    private final PrivacyEncryptor encryptor;
    private final PrivacyDecryptor decryptor;

    private TransferSingleService(HttpClient httpClient, HostName hostName, PrivacyEncryptor encryptor, PrivacyDecryptor decryptor) {
        this.httpClient = (HttpClient) Objects.requireNonNull(httpClient);
        this.hostName = hostName;
        this.encryptor = (PrivacyEncryptor)Objects.requireNonNull(encryptor);
        this.decryptor = (PrivacyDecryptor)Objects.requireNonNull(decryptor);
    }


    public InitiateSingleTransferResponse initiateSingleTransfer(InitiateSingleTransferRequest request) {
        String requestPath = "https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/transfer-bills";
        PrivacyEncryptor var10001 = this.encryptor;
        Objects.requireNonNull(var10001);
        InitiateSingleTransferRequest realRequest = request.cloneWithCipher(var10001::encrypt);
        if (this.hostName != null) {
            requestPath = requestPath.replaceFirst(HostName.API.getValue(), this.hostName.getValue());
        }

        HttpHeaders headers = new HttpHeaders();
        headers.addHeader("Accept", MediaType.APPLICATION_JSON.getValue());
        headers.addHeader("Content-Type", MediaType.APPLICATION_JSON.getValue());
        headers.addHeader("Wechatpay-Serial", this.encryptor.getWechatpaySerial());
        HttpRequest httpRequest = (new com.wechat.pay.java.core.http.HttpRequest.Builder()).httpMethod(HttpMethod.POST).url(requestPath).headers(headers).body(this.createRequestBody(realRequest)).build();
        HttpResponse<InitiateSingleTransferResponse> httpResponse = this.httpClient.execute(httpRequest, InitiateSingleTransferResponse.class);
        return (InitiateSingleTransferResponse)httpResponse.getServiceResponse();
    }

    private RequestBody createRequestBody(Object request) {
        return (new com.wechat.pay.java.core.http.JsonRequestBody.Builder()).body(GsonUtil.toJson(request)).build();
    }

    public static class Builder {
        private HttpClient httpClient;
        private HostName hostName;
        private PrivacyEncryptor encryptor;
        private PrivacyDecryptor decryptor;

        public Builder() {
        }

        public TransferSingleService.Builder config(Config config) {
            this.httpClient = (new DefaultHttpClientBuilder()).config(config).build();
            this.encryptor = config.createEncryptor();
            this.decryptor = config.createDecryptor();
            return this;
        }

        public TransferSingleService.Builder hostName(HostName hostName) {
            this.hostName = hostName;
            return this;
        }

        public TransferSingleService.Builder httpClient(HttpClient httpClient) {
            this.httpClient = httpClient;
            return this;
        }

        public TransferSingleService.Builder encryptor(PrivacyEncryptor encryptor) {
            this.encryptor = encryptor;
            return this;
        }

        public TransferSingleService.Builder decryptor(PrivacyDecryptor decryptor) {
            this.decryptor = decryptor;
            return this;
        }

        public TransferSingleService build() {
            return new TransferSingleService(this.httpClient, this.hostName, this.encryptor, this.decryptor);
        }
    }
}
