// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
//
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.quantum.jobs;

import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.credential.TokenCredential;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.policy.*;
import com.azure.core.util.Configuration;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;
import com.azure.quantum.jobs.implementation.QuantumClientImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** A builder for creating a new instance of the QuantumClient type. */
@ServiceClientBuilder(
        serviceClients = {
            JobsClient.class,
            ProvidersClient.class,
            StorageClient.class,
            QuotasClient.class,
            JobsAsyncClient.class,
            ProvidersAsyncClient.class,
            StorageAsyncClient.class,
            QuotasAsyncClient.class
        })
public final class QuantumClientBuilder {
    private static final String SDK_NAME = "name";

    private static final String SDK_VERSION = "version";

    static final String[] DEFAULT_SCOPES = new String[] {"https://quantum.microsoft.com/.default"};

    private final Map<String, String> properties = new HashMap<>();

    /** Create an instance of the QuantumClientBuilder. */
    public QuantumClientBuilder() {
        this.pipelinePolicies = new ArrayList<>();
    }

    /*
     * The Azure subscription ID. This is a GUID-formatted string (e.g.
     * 00000000-0000-0000-0000-000000000000)
     */
    private String subscriptionId;

    /**
     * Sets The Azure subscription ID. This is a GUID-formatted string (e.g. 00000000-0000-0000-0000-000000000000).
     *
     * @param subscriptionId the subscriptionId value.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder subscriptionId(String subscriptionId) {
        this.subscriptionId = subscriptionId;
        return this;
    }

    /*
     * Name of an Azure resource group.
     */
    private String resourceGroupName;

    /**
     * Sets Name of an Azure resource group.
     *
     * @param resourceGroupName the resourceGroupName value.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder resourceGroupName(String resourceGroupName) {
        this.resourceGroupName = resourceGroupName;
        return this;
    }

    /*
     * Name of the workspace.
     */
    private String workspaceName;

    /**
     * Sets Name of the workspace.
     *
     * @param workspaceName the workspaceName value.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder workspaceName(String workspaceName) {
        this.workspaceName = workspaceName;
        return this;
    }

    /*
     * server parameter
     */
    private String host;

    /**
     * Sets server parameter.
     *
     * @param host the host value.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder host(String host) {
        this.host = host;
        return this;
    }

    /*
     * The HTTP pipeline to send requests through
     */
    private HttpPipeline pipeline;

    /**
     * Sets The HTTP pipeline to send requests through.
     *
     * @param pipeline the pipeline value.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder pipeline(HttpPipeline pipeline) {
        this.pipeline = pipeline;
        return this;
    }

    /*
     * The serializer to serialize an object into a string
     */
    private SerializerAdapter serializerAdapter;

    /**
     * Sets The serializer to serialize an object into a string.
     *
     * @param serializerAdapter the serializerAdapter value.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder serializerAdapter(SerializerAdapter serializerAdapter) {
        this.serializerAdapter = serializerAdapter;
        return this;
    }

    /*
     * The HTTP client used to send the request.
     */
    private HttpClient httpClient;

    /**
     * Sets The HTTP client used to send the request.
     *
     * @param httpClient the httpClient value.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder httpClient(HttpClient httpClient) {
        this.httpClient = httpClient;
        return this;
    }

    /*
     * The configuration store that is used during construction of the service
     * client.
     */
    private Configuration configuration;

    /**
     * Sets The configuration store that is used during construction of the service client.
     *
     * @param configuration the configuration value.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder configuration(Configuration configuration) {
        this.configuration = configuration;
        return this;
    }

    /*
     * The TokenCredential used for authentication.
     */
    private TokenCredential tokenCredential;

    /**
     * Sets The TokenCredential used for authentication.
     *
     * @param tokenCredential the tokenCredential value.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder credential(TokenCredential tokenCredential) {
        this.tokenCredential = tokenCredential;
        return this;
    }

    /*
     * The logging configuration for HTTP requests and responses.
     */
    private HttpLogOptions httpLogOptions;

    /**
     * Sets The logging configuration for HTTP requests and responses.
     *
     * @param httpLogOptions the httpLogOptions value.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder httpLogOptions(HttpLogOptions httpLogOptions) {
        this.httpLogOptions = httpLogOptions;
        return this;
    }

    /*
     * The retry policy that will attempt to retry failed requests, if
     * applicable.
     */
    private RetryPolicy retryPolicy;

    /**
     * Sets The retry policy that will attempt to retry failed requests, if applicable.
     *
     * @param retryPolicy the retryPolicy value.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder retryPolicy(RetryPolicy retryPolicy) {
        this.retryPolicy = retryPolicy;
        return this;
    }

    /*
     * The list of Http pipeline policies to add.
     */
    private final List<HttpPipelinePolicy> pipelinePolicies;

    /**
     * Adds a custom Http pipeline policy.
     *
     * @param customPolicy The custom Http pipeline policy to add.
     * @return the QuantumClientBuilder.
     */
    public QuantumClientBuilder addPolicy(HttpPipelinePolicy customPolicy) {
        pipelinePolicies.add(customPolicy);
        return this;
    }

    /**
     * Builds an instance of QuantumClientImpl with the provided parameters.
     *
     * @return an instance of QuantumClientImpl.
     */
    private QuantumClientImpl buildInnerClient() {
        if (host == null) {
            this.host = "https://quantum.azure.com";
        }
        if (pipeline == null) {
            this.pipeline = createHttpPipeline();
        }
        if (serializerAdapter == null) {
            this.serializerAdapter = JacksonAdapter.createDefaultSerializerAdapter();
        }
        QuantumClientImpl client =
                new QuantumClientImpl(
                        pipeline, serializerAdapter, subscriptionId, resourceGroupName, workspaceName, host);
        return client;
    }

    private HttpPipeline createHttpPipeline() {
        Configuration buildConfiguration =
                (configuration == null) ? Configuration.getGlobalConfiguration() : configuration;
        if (httpLogOptions == null) {
            httpLogOptions = new HttpLogOptions();
        }
        List<HttpPipelinePolicy> policies = new ArrayList<>();
        String clientName = properties.getOrDefault(SDK_NAME, "UnknownName");
        String clientVersion = properties.getOrDefault(SDK_VERSION, "UnknownVersion");
        if (tokenCredential != null) {
            policies.add(new BearerTokenAuthenticationPolicy(tokenCredential, DEFAULT_SCOPES));
        }
        policies.add(
                new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, buildConfiguration));
        HttpPolicyProviders.addBeforeRetryPolicies(policies);
        policies.add(retryPolicy == null ? new RetryPolicy() : retryPolicy);
        policies.add(new CookiePolicy());
        policies.addAll(this.pipelinePolicies);
        HttpPolicyProviders.addAfterRetryPolicies(policies);
        policies.add(new HttpLoggingPolicy(httpLogOptions));
        HttpPipeline httpPipeline =
                new HttpPipelineBuilder()
                        .policies(policies.toArray(new HttpPipelinePolicy[0]))
                        .httpClient(httpClient)
                        .build();
        return httpPipeline;
    }

    /**
     * Builds an instance of JobsAsyncClient async client.
     *
     * @return an instance of JobsAsyncClient.
     */
    public JobsAsyncClient buildJobsAsyncClient() {
        return new JobsAsyncClient(buildInnerClient().getJobs());
    }

    /**
     * Builds an instance of ProvidersAsyncClient async client.
     *
     * @return an instance of ProvidersAsyncClient.
     */
    public ProvidersAsyncClient buildProvidersAsyncClient() {
        return new ProvidersAsyncClient(buildInnerClient().getProviders());
    }

    /**
     * Builds an instance of StorageAsyncClient async client.
     *
     * @return an instance of StorageAsyncClient.
     */
    public StorageAsyncClient buildStorageAsyncClient() {
        return new StorageAsyncClient(buildInnerClient().getStorages());
    }

    /**
     * Builds an instance of QuotasAsyncClient async client.
     *
     * @return an instance of QuotasAsyncClient.
     */
    public QuotasAsyncClient buildQuotasAsyncClient() {
        return new QuotasAsyncClient(buildInnerClient().getQuotas());
    }

    /**
     * Builds an instance of JobsClient sync client.
     *
     * @return an instance of JobsClient.
     */
    public JobsClient buildJobsClient() {
        return new JobsClient(buildInnerClient().getJobs());
    }

    /**
     * Builds an instance of ProvidersClient sync client.
     *
     * @return an instance of ProvidersClient.
     */
    public ProvidersClient buildProvidersClient() {
        return new ProvidersClient(buildInnerClient().getProviders());
    }

    /**
     * Builds an instance of StorageClient sync client.
     *
     * @return an instance of StorageClient.
     */
    public StorageClient buildStorageClient() {
        return new StorageClient(buildInnerClient().getStorages());
    }

    /**
     * Builds an instance of QuotasClient sync client.
     *
     * @return an instance of QuotasClient.
     */
    public QuotasClient buildQuotasClient() {
        return new QuotasClient(buildInnerClient().getQuotas());
    }
}
