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

package com.azure.resourcemanager.managednetworkfabric;

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.managednetworkfabric.fluent.AzureNetworkFabricManagementServiceApi;
import com.azure.resourcemanager.managednetworkfabric.implementation.AccessControlListsImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.AzureNetworkFabricManagementServiceApiBuilder;
import com.azure.resourcemanager.managednetworkfabric.implementation.ExternalNetworksImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.InternalNetworksImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.IpCommunitiesImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.IpExtendedCommunitiesImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.IpPrefixesImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.L2IsolationDomainsImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.L3IsolationDomainsImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.NetworkDeviceSkusImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.NetworkDevicesImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.NetworkFabricControllersImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.NetworkFabricSkusImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.NetworkFabricsImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.NetworkInterfacesImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.NetworkRackSkusImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.NetworkRacksImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.NetworkToNetworkInterconnectsImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.OperationsImpl;
import com.azure.resourcemanager.managednetworkfabric.implementation.RoutePoliciesImpl;
import com.azure.resourcemanager.managednetworkfabric.models.AccessControlLists;
import com.azure.resourcemanager.managednetworkfabric.models.ExternalNetworks;
import com.azure.resourcemanager.managednetworkfabric.models.InternalNetworks;
import com.azure.resourcemanager.managednetworkfabric.models.IpCommunities;
import com.azure.resourcemanager.managednetworkfabric.models.IpExtendedCommunities;
import com.azure.resourcemanager.managednetworkfabric.models.IpPrefixes;
import com.azure.resourcemanager.managednetworkfabric.models.L2IsolationDomains;
import com.azure.resourcemanager.managednetworkfabric.models.L3IsolationDomains;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkDeviceSkus;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkDevices;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkFabricControllers;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkFabricSkus;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkFabrics;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkInterfaces;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkRackSkus;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkRacks;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkToNetworkInterconnects;
import com.azure.resourcemanager.managednetworkfabric.models.Operations;
import com.azure.resourcemanager.managednetworkfabric.models.RoutePolicies;
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 ManagedNetworkFabricManager. Self service experience for Azure Network Fabric API. */
public final class ManagedNetworkFabricManager {
    private AccessControlLists accessControlLists;

    private IpCommunities ipCommunities;

    private IpExtendedCommunities ipExtendedCommunities;

    private IpPrefixes ipPrefixes;

    private L2IsolationDomains l2IsolationDomains;

    private L3IsolationDomains l3IsolationDomains;

    private InternalNetworks internalNetworks;

    private ExternalNetworks externalNetworks;

    private NetworkDeviceSkus networkDeviceSkus;

    private NetworkDevices networkDevices;

    private NetworkInterfaces networkInterfaces;

    private NetworkFabricControllers networkFabricControllers;

    private NetworkFabricSkus networkFabricSkus;

    private NetworkFabrics networkFabrics;

    private NetworkToNetworkInterconnects networkToNetworkInterconnects;

    private NetworkRackSkus networkRackSkus;

    private NetworkRacks networkRacks;

    private Operations operations;

    private RoutePolicies routePolicies;

    private final AzureNetworkFabricManagementServiceApi clientObject;

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

    /**
     * Creates an instance of Managed Network Fabric service API entry point.
     *
     * @param credential the credential to use.
     * @param profile the Azure profile for client.
     * @return the Managed Network Fabric service API instance.
     */
    public static ManagedNetworkFabricManager 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 Managed Network Fabric service API entry point.
     *
     * @param httpPipeline the {@link HttpPipeline} configured with Azure authentication credential.
     * @param profile the Azure profile for client.
     * @return the Managed Network Fabric service API instance.
     */
    public static ManagedNetworkFabricManager authenticate(HttpPipeline httpPipeline, AzureProfile profile) {
        Objects.requireNonNull(httpPipeline, "'httpPipeline' cannot be null.");
        Objects.requireNonNull(profile, "'profile' cannot be null.");
        return new ManagedNetworkFabricManager(httpPipeline, profile, null);
    }

    /**
     * Gets a Configurable instance that can be used to create ManagedNetworkFabricManager with optional configuration.
     *
     * @return the Configurable instance allowing configurations.
     */
    public static Configurable configure() {
        return new ManagedNetworkFabricManager.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 Managed Network Fabric service API entry point.
         *
         * @param credential the credential to use.
         * @param profile the Azure profile for client.
         * @return the Managed Network Fabric service API instance.
         */
        public ManagedNetworkFabricManager 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.managednetworkfabric")
                .append("/")
                .append("1.0.0-beta.1");
            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 ManagedNetworkFabricManager(httpPipeline, profile, defaultPollInterval);
        }
    }

    /**
     * Gets the resource collection API of AccessControlLists. It manages AccessControlList.
     *
     * @return Resource collection API of AccessControlLists.
     */
    public AccessControlLists accessControlLists() {
        if (this.accessControlLists == null) {
            this.accessControlLists = new AccessControlListsImpl(clientObject.getAccessControlLists(), this);
        }
        return accessControlLists;
    }

    /**
     * Gets the resource collection API of IpCommunities. It manages IpCommunity.
     *
     * @return Resource collection API of IpCommunities.
     */
    public IpCommunities ipCommunities() {
        if (this.ipCommunities == null) {
            this.ipCommunities = new IpCommunitiesImpl(clientObject.getIpCommunities(), this);
        }
        return ipCommunities;
    }

    /**
     * Gets the resource collection API of IpExtendedCommunities. It manages IpExtendedCommunity.
     *
     * @return Resource collection API of IpExtendedCommunities.
     */
    public IpExtendedCommunities ipExtendedCommunities() {
        if (this.ipExtendedCommunities == null) {
            this.ipExtendedCommunities = new IpExtendedCommunitiesImpl(clientObject.getIpExtendedCommunities(), this);
        }
        return ipExtendedCommunities;
    }

    /**
     * Gets the resource collection API of IpPrefixes. It manages IpPrefix.
     *
     * @return Resource collection API of IpPrefixes.
     */
    public IpPrefixes ipPrefixes() {
        if (this.ipPrefixes == null) {
            this.ipPrefixes = new IpPrefixesImpl(clientObject.getIpPrefixes(), this);
        }
        return ipPrefixes;
    }

    /**
     * Gets the resource collection API of L2IsolationDomains. It manages L2IsolationDomain.
     *
     * @return Resource collection API of L2IsolationDomains.
     */
    public L2IsolationDomains l2IsolationDomains() {
        if (this.l2IsolationDomains == null) {
            this.l2IsolationDomains = new L2IsolationDomainsImpl(clientObject.getL2IsolationDomains(), this);
        }
        return l2IsolationDomains;
    }

    /**
     * Gets the resource collection API of L3IsolationDomains. It manages L3IsolationDomain.
     *
     * @return Resource collection API of L3IsolationDomains.
     */
    public L3IsolationDomains l3IsolationDomains() {
        if (this.l3IsolationDomains == null) {
            this.l3IsolationDomains = new L3IsolationDomainsImpl(clientObject.getL3IsolationDomains(), this);
        }
        return l3IsolationDomains;
    }

    /**
     * Gets the resource collection API of InternalNetworks. It manages InternalNetwork.
     *
     * @return Resource collection API of InternalNetworks.
     */
    public InternalNetworks internalNetworks() {
        if (this.internalNetworks == null) {
            this.internalNetworks = new InternalNetworksImpl(clientObject.getInternalNetworks(), this);
        }
        return internalNetworks;
    }

    /**
     * Gets the resource collection API of ExternalNetworks. It manages ExternalNetwork.
     *
     * @return Resource collection API of ExternalNetworks.
     */
    public ExternalNetworks externalNetworks() {
        if (this.externalNetworks == null) {
            this.externalNetworks = new ExternalNetworksImpl(clientObject.getExternalNetworks(), this);
        }
        return externalNetworks;
    }

    /**
     * Gets the resource collection API of NetworkDeviceSkus.
     *
     * @return Resource collection API of NetworkDeviceSkus.
     */
    public NetworkDeviceSkus networkDeviceSkus() {
        if (this.networkDeviceSkus == null) {
            this.networkDeviceSkus = new NetworkDeviceSkusImpl(clientObject.getNetworkDeviceSkus(), this);
        }
        return networkDeviceSkus;
    }

    /**
     * Gets the resource collection API of NetworkDevices. It manages NetworkDevice.
     *
     * @return Resource collection API of NetworkDevices.
     */
    public NetworkDevices networkDevices() {
        if (this.networkDevices == null) {
            this.networkDevices = new NetworkDevicesImpl(clientObject.getNetworkDevices(), this);
        }
        return networkDevices;
    }

    /**
     * Gets the resource collection API of NetworkInterfaces. It manages NetworkInterface.
     *
     * @return Resource collection API of NetworkInterfaces.
     */
    public NetworkInterfaces networkInterfaces() {
        if (this.networkInterfaces == null) {
            this.networkInterfaces = new NetworkInterfacesImpl(clientObject.getNetworkInterfaces(), this);
        }
        return networkInterfaces;
    }

    /**
     * Gets the resource collection API of NetworkFabricControllers. It manages NetworkFabricController.
     *
     * @return Resource collection API of NetworkFabricControllers.
     */
    public NetworkFabricControllers networkFabricControllers() {
        if (this.networkFabricControllers == null) {
            this.networkFabricControllers =
                new NetworkFabricControllersImpl(clientObject.getNetworkFabricControllers(), this);
        }
        return networkFabricControllers;
    }

    /**
     * Gets the resource collection API of NetworkFabricSkus.
     *
     * @return Resource collection API of NetworkFabricSkus.
     */
    public NetworkFabricSkus networkFabricSkus() {
        if (this.networkFabricSkus == null) {
            this.networkFabricSkus = new NetworkFabricSkusImpl(clientObject.getNetworkFabricSkus(), this);
        }
        return networkFabricSkus;
    }

    /**
     * Gets the resource collection API of NetworkFabrics. It manages NetworkFabric.
     *
     * @return Resource collection API of NetworkFabrics.
     */
    public NetworkFabrics networkFabrics() {
        if (this.networkFabrics == null) {
            this.networkFabrics = new NetworkFabricsImpl(clientObject.getNetworkFabrics(), this);
        }
        return networkFabrics;
    }

    /**
     * Gets the resource collection API of NetworkToNetworkInterconnects. It manages NetworkToNetworkInterconnect.
     *
     * @return Resource collection API of NetworkToNetworkInterconnects.
     */
    public NetworkToNetworkInterconnects networkToNetworkInterconnects() {
        if (this.networkToNetworkInterconnects == null) {
            this.networkToNetworkInterconnects =
                new NetworkToNetworkInterconnectsImpl(clientObject.getNetworkToNetworkInterconnects(), this);
        }
        return networkToNetworkInterconnects;
    }

    /**
     * Gets the resource collection API of NetworkRackSkus.
     *
     * @return Resource collection API of NetworkRackSkus.
     */
    public NetworkRackSkus networkRackSkus() {
        if (this.networkRackSkus == null) {
            this.networkRackSkus = new NetworkRackSkusImpl(clientObject.getNetworkRackSkus(), this);
        }
        return networkRackSkus;
    }

    /**
     * Gets the resource collection API of NetworkRacks. It manages NetworkRack.
     *
     * @return Resource collection API of NetworkRacks.
     */
    public NetworkRacks networkRacks() {
        if (this.networkRacks == null) {
            this.networkRacks = new NetworkRacksImpl(clientObject.getNetworkRacks(), this);
        }
        return networkRacks;
    }

    /**
     * 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 RoutePolicies. It manages RoutePolicy.
     *
     * @return Resource collection API of RoutePolicies.
     */
    public RoutePolicies routePolicies() {
        if (this.routePolicies == null) {
            this.routePolicies = new RoutePoliciesImpl(clientObject.getRoutePolicies(), this);
        }
        return routePolicies;
    }

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