// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.devcenter;

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.HttpPipelinePosition;
import com.azure.core.http.policy.AddDatePolicy;
import com.azure.core.http.policy.AddHeadersFromContextPolicy;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.HttpLoggingPolicy;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.http.policy.HttpPolicyProviders;
import com.azure.core.http.policy.RequestIdPolicy;
import com.azure.core.http.policy.RetryOptions;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.http.policy.UserAgentPolicy;
import com.azure.core.management.http.policy.ArmChallengeAuthenticationPolicy;
import com.azure.core.management.profile.AzureProfile;
import com.azure.core.util.Configuration;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.devcenter.fluent.DevCenterManagementClient;
import com.azure.resourcemanager.devcenter.implementation.AttachedNetworksImpl;
import com.azure.resourcemanager.devcenter.implementation.CatalogsImpl;
import com.azure.resourcemanager.devcenter.implementation.CheckNameAvailabilitiesImpl;
import com.azure.resourcemanager.devcenter.implementation.DevBoxDefinitionsImpl;
import com.azure.resourcemanager.devcenter.implementation.DevCenterManagementClientBuilder;
import com.azure.resourcemanager.devcenter.implementation.DevCentersImpl;
import com.azure.resourcemanager.devcenter.implementation.EnvironmentTypesImpl;
import com.azure.resourcemanager.devcenter.implementation.GalleriesImpl;
import com.azure.resourcemanager.devcenter.implementation.ImageVersionsImpl;
import com.azure.resourcemanager.devcenter.implementation.ImagesImpl;
import com.azure.resourcemanager.devcenter.implementation.NetworkConnectionsImpl;
import com.azure.resourcemanager.devcenter.implementation.OperationStatusesImpl;
import com.azure.resourcemanager.devcenter.implementation.OperationsImpl;
import com.azure.resourcemanager.devcenter.implementation.PoolsImpl;
import com.azure.resourcemanager.devcenter.implementation.ProjectAllowedEnvironmentTypesImpl;
import com.azure.resourcemanager.devcenter.implementation.ProjectEnvironmentTypesImpl;
import com.azure.resourcemanager.devcenter.implementation.ProjectsImpl;
import com.azure.resourcemanager.devcenter.implementation.SchedulesImpl;
import com.azure.resourcemanager.devcenter.implementation.SkusImpl;
import com.azure.resourcemanager.devcenter.implementation.UsagesImpl;
import com.azure.resourcemanager.devcenter.models.AttachedNetworks;
import com.azure.resourcemanager.devcenter.models.Catalogs;
import com.azure.resourcemanager.devcenter.models.CheckNameAvailabilities;
import com.azure.resourcemanager.devcenter.models.DevBoxDefinitions;
import com.azure.resourcemanager.devcenter.models.DevCenters;
import com.azure.resourcemanager.devcenter.models.EnvironmentTypes;
import com.azure.resourcemanager.devcenter.models.Galleries;
import com.azure.resourcemanager.devcenter.models.ImageVersions;
import com.azure.resourcemanager.devcenter.models.Images;
import com.azure.resourcemanager.devcenter.models.NetworkConnections;
import com.azure.resourcemanager.devcenter.models.OperationStatuses;
import com.azure.resourcemanager.devcenter.models.Operations;
import com.azure.resourcemanager.devcenter.models.Pools;
import com.azure.resourcemanager.devcenter.models.ProjectAllowedEnvironmentTypes;
import com.azure.resourcemanager.devcenter.models.ProjectEnvironmentTypes;
import com.azure.resourcemanager.devcenter.models.Projects;
import com.azure.resourcemanager.devcenter.models.Schedules;
import com.azure.resourcemanager.devcenter.models.Skus;
import com.azure.resourcemanager.devcenter.models.Usages;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/** Entry point to DevCenterManager. DevCenter Management API. */
public final class DevCenterManager {
    private DevCenters devCenters;

    private Projects projects;

    private AttachedNetworks attachedNetworks;

    private Galleries galleries;

    private Images images;

    private ImageVersions imageVersions;

    private Catalogs catalogs;

    private EnvironmentTypes environmentTypes;

    private ProjectAllowedEnvironmentTypes projectAllowedEnvironmentTypes;

    private ProjectEnvironmentTypes projectEnvironmentTypes;

    private DevBoxDefinitions devBoxDefinitions;

    private Operations operations;

    private OperationStatuses operationStatuses;

    private Usages usages;

    private CheckNameAvailabilities checkNameAvailabilities;

    private Skus skus;

    private Pools pools;

    private Schedules schedules;

    private NetworkConnections networkConnections;

    private final DevCenterManagementClient clientObject;

    private DevCenterManager(HttpPipeline httpPipeline, AzureProfile profile, Duration defaultPollInterval) {
        Objects.requireNonNull(httpPipeline, "'httpPipeline' cannot be null.");
        Objects.requireNonNull(profile, "'profile' cannot be null.");
        this.clientObject =
            new DevCenterManagementClientBuilder()
                .pipeline(httpPipeline)
                .endpoint(profile.getEnvironment().getResourceManagerEndpoint())
                .subscriptionId(profile.getSubscriptionId())
                .defaultPollInterval(defaultPollInterval)
                .buildClient();
    }

    /**
     * Creates an instance of DevCenter service API entry point.
     *
     * @param credential the credential to use.
     * @param profile the Azure profile for client.
     * @return the DevCenter service API instance.
     */
    public static DevCenterManager authenticate(TokenCredential credential, AzureProfile profile) {
        Objects.requireNonNull(credential, "'credential' cannot be null.");
        Objects.requireNonNull(profile, "'profile' cannot be null.");
        return configure().authenticate(credential, profile);
    }

    /**
     * Creates an instance of DevCenter service API entry point.
     *
     * @param httpPipeline the {@link HttpPipeline} configured with Azure authentication credential.
     * @param profile the Azure profile for client.
     * @return the DevCenter service API instance.
     */
    public static DevCenterManager authenticate(HttpPipeline httpPipeline, AzureProfile profile) {
        Objects.requireNonNull(httpPipeline, "'httpPipeline' cannot be null.");
        Objects.requireNonNull(profile, "'profile' cannot be null.");
        return new DevCenterManager(httpPipeline, profile, null);
    }

    /**
     * Gets a Configurable instance that can be used to create DevCenterManager with optional configuration.
     *
     * @return the Configurable instance allowing configurations.
     */
    public static Configurable configure() {
        return new DevCenterManager.Configurable();
    }

    /** The Configurable allowing configurations to be set. */
    public static final class Configurable {
        private static final ClientLogger LOGGER = new ClientLogger(Configurable.class);

        private HttpClient httpClient;
        private HttpLogOptions httpLogOptions;
        private final List<HttpPipelinePolicy> policies = new ArrayList<>();
        private final List<String> scopes = new ArrayList<>();
        private RetryPolicy retryPolicy;
        private RetryOptions retryOptions;
        private Duration defaultPollInterval;

        private Configurable() {
        }

        /**
         * Sets the http client.
         *
         * @param httpClient the HTTP client.
         * @return the configurable object itself.
         */
        public Configurable withHttpClient(HttpClient httpClient) {
            this.httpClient = Objects.requireNonNull(httpClient, "'httpClient' cannot be null.");
            return this;
        }

        /**
         * Sets the logging options to the HTTP pipeline.
         *
         * @param httpLogOptions the HTTP log options.
         * @return the configurable object itself.
         */
        public Configurable withLogOptions(HttpLogOptions httpLogOptions) {
            this.httpLogOptions = Objects.requireNonNull(httpLogOptions, "'httpLogOptions' cannot be null.");
            return this;
        }

        /**
         * Adds the pipeline policy to the HTTP pipeline.
         *
         * @param policy the HTTP pipeline policy.
         * @return the configurable object itself.
         */
        public Configurable withPolicy(HttpPipelinePolicy policy) {
            this.policies.add(Objects.requireNonNull(policy, "'policy' cannot be null."));
            return this;
        }

        /**
         * Adds the scope to permission sets.
         *
         * @param scope the scope.
         * @return the configurable object itself.
         */
        public Configurable withScope(String scope) {
            this.scopes.add(Objects.requireNonNull(scope, "'scope' cannot be null."));
            return this;
        }

        /**
         * Sets the retry policy to the HTTP pipeline.
         *
         * @param retryPolicy the HTTP pipeline retry policy.
         * @return the configurable object itself.
         */
        public Configurable withRetryPolicy(RetryPolicy retryPolicy) {
            this.retryPolicy = Objects.requireNonNull(retryPolicy, "'retryPolicy' cannot be null.");
            return this;
        }

        /**
         * Sets the retry options for the HTTP pipeline retry policy.
         *
         * <p>This setting has no effect, if retry policy is set via {@link #withRetryPolicy(RetryPolicy)}.
         *
         * @param retryOptions the retry options for the HTTP pipeline retry policy.
         * @return the configurable object itself.
         */
        public Configurable withRetryOptions(RetryOptions retryOptions) {
            this.retryOptions = Objects.requireNonNull(retryOptions, "'retryOptions' cannot be null.");
            return this;
        }

        /**
         * Sets the default poll interval, used when service does not provide "Retry-After" header.
         *
         * @param defaultPollInterval the default poll interval.
         * @return the configurable object itself.
         */
        public Configurable withDefaultPollInterval(Duration defaultPollInterval) {
            this.defaultPollInterval =
                Objects.requireNonNull(defaultPollInterval, "'defaultPollInterval' cannot be null.");
            if (this.defaultPollInterval.isNegative()) {
                throw LOGGER
                    .logExceptionAsError(new IllegalArgumentException("'defaultPollInterval' cannot be negative"));
            }
            return this;
        }

        /**
         * Creates an instance of DevCenter service API entry point.
         *
         * @param credential the credential to use.
         * @param profile the Azure profile for client.
         * @return the DevCenter service API instance.
         */
        public DevCenterManager authenticate(TokenCredential credential, AzureProfile profile) {
            Objects.requireNonNull(credential, "'credential' cannot be null.");
            Objects.requireNonNull(profile, "'profile' cannot be null.");

            StringBuilder userAgentBuilder = new StringBuilder();
            userAgentBuilder
                .append("azsdk-java")
                .append("-")
                .append("com.azure.resourcemanager.devcenter")
                .append("/")
                .append("1.0.0-beta.5");
            if (!Configuration.getGlobalConfiguration().get("AZURE_TELEMETRY_DISABLED", false)) {
                userAgentBuilder
                    .append(" (")
                    .append(Configuration.getGlobalConfiguration().get("java.version"))
                    .append("; ")
                    .append(Configuration.getGlobalConfiguration().get("os.name"))
                    .append("; ")
                    .append(Configuration.getGlobalConfiguration().get("os.version"))
                    .append("; auto-generated)");
            } else {
                userAgentBuilder.append(" (auto-generated)");
            }

            if (scopes.isEmpty()) {
                scopes.add(profile.getEnvironment().getManagementEndpoint() + "/.default");
            }
            if (retryPolicy == null) {
                if (retryOptions != null) {
                    retryPolicy = new RetryPolicy(retryOptions);
                } else {
                    retryPolicy = new RetryPolicy("Retry-After", ChronoUnit.SECONDS);
                }
            }
            List<HttpPipelinePolicy> policies = new ArrayList<>();
            policies.add(new UserAgentPolicy(userAgentBuilder.toString()));
            policies.add(new AddHeadersFromContextPolicy());
            policies.add(new RequestIdPolicy());
            policies
                .addAll(
                    this
                        .policies
                        .stream()
                        .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_CALL)
                        .collect(Collectors.toList()));
            HttpPolicyProviders.addBeforeRetryPolicies(policies);
            policies.add(retryPolicy);
            policies.add(new AddDatePolicy());
            policies.add(new ArmChallengeAuthenticationPolicy(credential, scopes.toArray(new String[0])));
            policies
                .addAll(
                    this
                        .policies
                        .stream()
                        .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_RETRY)
                        .collect(Collectors.toList()));
            HttpPolicyProviders.addAfterRetryPolicies(policies);
            policies.add(new HttpLoggingPolicy(httpLogOptions));
            HttpPipeline httpPipeline =
                new HttpPipelineBuilder()
                    .httpClient(httpClient)
                    .policies(policies.toArray(new HttpPipelinePolicy[0]))
                    .build();
            return new DevCenterManager(httpPipeline, profile, defaultPollInterval);
        }
    }

    /**
     * Gets the resource collection API of DevCenters. It manages DevCenter.
     *
     * @return Resource collection API of DevCenters.
     */
    public DevCenters devCenters() {
        if (this.devCenters == null) {
            this.devCenters = new DevCentersImpl(clientObject.getDevCenters(), this);
        }
        return devCenters;
    }

    /**
     * Gets the resource collection API of Projects. It manages Project.
     *
     * @return Resource collection API of Projects.
     */
    public Projects projects() {
        if (this.projects == null) {
            this.projects = new ProjectsImpl(clientObject.getProjects(), this);
        }
        return projects;
    }

    /**
     * Gets the resource collection API of AttachedNetworks. It manages AttachedNetworkConnection.
     *
     * @return Resource collection API of AttachedNetworks.
     */
    public AttachedNetworks attachedNetworks() {
        if (this.attachedNetworks == null) {
            this.attachedNetworks = new AttachedNetworksImpl(clientObject.getAttachedNetworks(), this);
        }
        return attachedNetworks;
    }

    /**
     * Gets the resource collection API of Galleries. It manages Gallery.
     *
     * @return Resource collection API of Galleries.
     */
    public Galleries galleries() {
        if (this.galleries == null) {
            this.galleries = new GalleriesImpl(clientObject.getGalleries(), this);
        }
        return galleries;
    }

    /**
     * Gets the resource collection API of Images.
     *
     * @return Resource collection API of Images.
     */
    public Images images() {
        if (this.images == null) {
            this.images = new ImagesImpl(clientObject.getImages(), this);
        }
        return images;
    }

    /**
     * Gets the resource collection API of ImageVersions.
     *
     * @return Resource collection API of ImageVersions.
     */
    public ImageVersions imageVersions() {
        if (this.imageVersions == null) {
            this.imageVersions = new ImageVersionsImpl(clientObject.getImageVersions(), this);
        }
        return imageVersions;
    }

    /**
     * Gets the resource collection API of Catalogs. It manages Catalog.
     *
     * @return Resource collection API of Catalogs.
     */
    public Catalogs catalogs() {
        if (this.catalogs == null) {
            this.catalogs = new CatalogsImpl(clientObject.getCatalogs(), this);
        }
        return catalogs;
    }

    /**
     * Gets the resource collection API of EnvironmentTypes. It manages EnvironmentType.
     *
     * @return Resource collection API of EnvironmentTypes.
     */
    public EnvironmentTypes environmentTypes() {
        if (this.environmentTypes == null) {
            this.environmentTypes = new EnvironmentTypesImpl(clientObject.getEnvironmentTypes(), this);
        }
        return environmentTypes;
    }

    /**
     * Gets the resource collection API of ProjectAllowedEnvironmentTypes.
     *
     * @return Resource collection API of ProjectAllowedEnvironmentTypes.
     */
    public ProjectAllowedEnvironmentTypes projectAllowedEnvironmentTypes() {
        if (this.projectAllowedEnvironmentTypes == null) {
            this.projectAllowedEnvironmentTypes =
                new ProjectAllowedEnvironmentTypesImpl(clientObject.getProjectAllowedEnvironmentTypes(), this);
        }
        return projectAllowedEnvironmentTypes;
    }

    /**
     * Gets the resource collection API of ProjectEnvironmentTypes. It manages ProjectEnvironmentType.
     *
     * @return Resource collection API of ProjectEnvironmentTypes.
     */
    public ProjectEnvironmentTypes projectEnvironmentTypes() {
        if (this.projectEnvironmentTypes == null) {
            this.projectEnvironmentTypes =
                new ProjectEnvironmentTypesImpl(clientObject.getProjectEnvironmentTypes(), this);
        }
        return projectEnvironmentTypes;
    }

    /**
     * Gets the resource collection API of DevBoxDefinitions. It manages DevBoxDefinition.
     *
     * @return Resource collection API of DevBoxDefinitions.
     */
    public DevBoxDefinitions devBoxDefinitions() {
        if (this.devBoxDefinitions == null) {
            this.devBoxDefinitions = new DevBoxDefinitionsImpl(clientObject.getDevBoxDefinitions(), this);
        }
        return devBoxDefinitions;
    }

    /**
     * Gets the resource collection API of Operations.
     *
     * @return Resource collection API of Operations.
     */
    public Operations operations() {
        if (this.operations == null) {
            this.operations = new OperationsImpl(clientObject.getOperations(), this);
        }
        return operations;
    }

    /**
     * Gets the resource collection API of OperationStatuses.
     *
     * @return Resource collection API of OperationStatuses.
     */
    public OperationStatuses operationStatuses() {
        if (this.operationStatuses == null) {
            this.operationStatuses = new OperationStatusesImpl(clientObject.getOperationStatuses(), this);
        }
        return operationStatuses;
    }

    /**
     * Gets the resource collection API of Usages.
     *
     * @return Resource collection API of Usages.
     */
    public Usages usages() {
        if (this.usages == null) {
            this.usages = new UsagesImpl(clientObject.getUsages(), this);
        }
        return usages;
    }

    /**
     * Gets the resource collection API of CheckNameAvailabilities.
     *
     * @return Resource collection API of CheckNameAvailabilities.
     */
    public CheckNameAvailabilities checkNameAvailabilities() {
        if (this.checkNameAvailabilities == null) {
            this.checkNameAvailabilities =
                new CheckNameAvailabilitiesImpl(clientObject.getCheckNameAvailabilities(), this);
        }
        return checkNameAvailabilities;
    }

    /**
     * Gets the resource collection API of Skus.
     *
     * @return Resource collection API of Skus.
     */
    public Skus skus() {
        if (this.skus == null) {
            this.skus = new SkusImpl(clientObject.getSkus(), this);
        }
        return skus;
    }

    /**
     * Gets the resource collection API of Pools. It manages Pool.
     *
     * @return Resource collection API of Pools.
     */
    public Pools pools() {
        if (this.pools == null) {
            this.pools = new PoolsImpl(clientObject.getPools(), this);
        }
        return pools;
    }

    /**
     * Gets the resource collection API of Schedules. It manages Schedule.
     *
     * @return Resource collection API of Schedules.
     */
    public Schedules schedules() {
        if (this.schedules == null) {
            this.schedules = new SchedulesImpl(clientObject.getSchedules(), this);
        }
        return schedules;
    }

    /**
     * Gets the resource collection API of NetworkConnections. It manages NetworkConnection.
     *
     * @return Resource collection API of NetworkConnections.
     */
    public NetworkConnections networkConnections() {
        if (this.networkConnections == null) {
            this.networkConnections = new NetworkConnectionsImpl(clientObject.getNetworkConnections(), this);
        }
        return networkConnections;
    }

    /**
     * @return Wrapped service client DevCenterManagementClient providing direct access to the underlying auto-generated
     *     API implementation, based on Azure REST API.
     */
    public DevCenterManagementClient serviceClient() {
        return this.clientObject;
    }
}
