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

package com.azure.resourcemanager.avs.implementation;

import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.avs.fluent.CloudLinksClient;
import com.azure.resourcemanager.avs.fluent.models.CloudLinkInner;
import com.azure.resourcemanager.avs.models.CloudLink;
import com.azure.resourcemanager.avs.models.CloudLinks;

public final class CloudLinksImpl implements CloudLinks {
    private static final ClientLogger LOGGER = new ClientLogger(CloudLinksImpl.class);

    private final CloudLinksClient innerClient;

    private final com.azure.resourcemanager.avs.AvsManager serviceManager;

    public CloudLinksImpl(CloudLinksClient innerClient, com.azure.resourcemanager.avs.AvsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<CloudLink> list(String resourceGroupName, String privateCloudName) {
        PagedIterable<CloudLinkInner> inner = this.serviceClient().list(resourceGroupName, privateCloudName);
        return Utils.mapPage(inner, inner1 -> new CloudLinkImpl(inner1, this.manager()));
    }

    public PagedIterable<CloudLink> list(String resourceGroupName, String privateCloudName, Context context) {
        PagedIterable<CloudLinkInner> inner = this.serviceClient().list(resourceGroupName, privateCloudName, context);
        return Utils.mapPage(inner, inner1 -> new CloudLinkImpl(inner1, this.manager()));
    }

    public Response<CloudLink> getWithResponse(
        String resourceGroupName, String privateCloudName, String cloudLinkName, Context context) {
        Response<CloudLinkInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, privateCloudName, cloudLinkName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CloudLinkImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public CloudLink get(String resourceGroupName, String privateCloudName, String cloudLinkName) {
        CloudLinkInner inner = this.serviceClient().get(resourceGroupName, privateCloudName, cloudLinkName);
        if (inner != null) {
            return new CloudLinkImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String privateCloudName, String cloudLinkName) {
        this.serviceClient().delete(resourceGroupName, privateCloudName, cloudLinkName);
    }

    public void delete(String resourceGroupName, String privateCloudName, String cloudLinkName, Context context) {
        this.serviceClient().delete(resourceGroupName, privateCloudName, cloudLinkName, context);
    }

    public CloudLink getById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String privateCloudName = Utils.getValueFromIdByName(id, "privateClouds");
        if (privateCloudName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'privateClouds'.", id)));
        }
        String cloudLinkName = Utils.getValueFromIdByName(id, "cloudLinks");
        if (cloudLinkName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'cloudLinks'.", id)));
        }
        return this.getWithResponse(resourceGroupName, privateCloudName, cloudLinkName, Context.NONE).getValue();
    }

    public Response<CloudLink> getByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String privateCloudName = Utils.getValueFromIdByName(id, "privateClouds");
        if (privateCloudName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'privateClouds'.", id)));
        }
        String cloudLinkName = Utils.getValueFromIdByName(id, "cloudLinks");
        if (cloudLinkName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'cloudLinks'.", id)));
        }
        return this.getWithResponse(resourceGroupName, privateCloudName, cloudLinkName, context);
    }

    public void deleteById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String privateCloudName = Utils.getValueFromIdByName(id, "privateClouds");
        if (privateCloudName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'privateClouds'.", id)));
        }
        String cloudLinkName = Utils.getValueFromIdByName(id, "cloudLinks");
        if (cloudLinkName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'cloudLinks'.", id)));
        }
        this.delete(resourceGroupName, privateCloudName, cloudLinkName, Context.NONE);
    }

    public void deleteByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String privateCloudName = Utils.getValueFromIdByName(id, "privateClouds");
        if (privateCloudName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'privateClouds'.", id)));
        }
        String cloudLinkName = Utils.getValueFromIdByName(id, "cloudLinks");
        if (cloudLinkName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'cloudLinks'.", id)));
        }
        this.delete(resourceGroupName, privateCloudName, cloudLinkName, context);
    }

    private CloudLinksClient serviceClient() {
        return this.innerClient;
    }

    private com.azure.resourcemanager.avs.AvsManager manager() {
        return this.serviceManager;
    }

    public CloudLinkImpl define(String name) {
        return new CloudLinkImpl(name, this.manager());
    }
}
