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

package com.azure.resourcemanager.elasticsan.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.elasticsan.fluent.VolumeGroupsClient;
import com.azure.resourcemanager.elasticsan.fluent.models.VolumeGroupInner;
import com.azure.resourcemanager.elasticsan.models.VolumeGroup;
import com.azure.resourcemanager.elasticsan.models.VolumeGroups;

public final class VolumeGroupsImpl implements VolumeGroups {
    private static final ClientLogger LOGGER = new ClientLogger(VolumeGroupsImpl.class);

    private final VolumeGroupsClient innerClient;

    private final com.azure.resourcemanager.elasticsan.ElasticSanManager serviceManager;

    public VolumeGroupsImpl(
        VolumeGroupsClient innerClient, com.azure.resourcemanager.elasticsan.ElasticSanManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<VolumeGroup> listByElasticSan(String resourceGroupName, String elasticSanName) {
        PagedIterable<VolumeGroupInner> inner =
            this.serviceClient().listByElasticSan(resourceGroupName, elasticSanName);
        return Utils.mapPage(inner, inner1 -> new VolumeGroupImpl(inner1, this.manager()));
    }

    public PagedIterable<VolumeGroup> listByElasticSan(
        String resourceGroupName, String elasticSanName, Context context) {
        PagedIterable<VolumeGroupInner> inner =
            this.serviceClient().listByElasticSan(resourceGroupName, elasticSanName, context);
        return Utils.mapPage(inner, inner1 -> new VolumeGroupImpl(inner1, this.manager()));
    }

    public void delete(String resourceGroupName, String elasticSanName, String volumeGroupName) {
        this.serviceClient().delete(resourceGroupName, elasticSanName, volumeGroupName);
    }

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

    public Response<VolumeGroup> getWithResponse(
        String resourceGroupName, String elasticSanName, String volumeGroupName, Context context) {
        Response<VolumeGroupInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, elasticSanName, volumeGroupName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new VolumeGroupImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public VolumeGroup get(String resourceGroupName, String elasticSanName, String volumeGroupName) {
        VolumeGroupInner inner = this.serviceClient().get(resourceGroupName, elasticSanName, volumeGroupName);
        if (inner != null) {
            return new VolumeGroupImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public VolumeGroup 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 elasticSanName = Utils.getValueFromIdByName(id, "elasticSans");
        if (elasticSanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'elasticSans'.", id)));
        }
        String volumeGroupName = Utils.getValueFromIdByName(id, "volumegroups");
        if (volumeGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'volumegroups'.", id)));
        }
        return this.getWithResponse(resourceGroupName, elasticSanName, volumeGroupName, Context.NONE).getValue();
    }

    public Response<VolumeGroup> 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 elasticSanName = Utils.getValueFromIdByName(id, "elasticSans");
        if (elasticSanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'elasticSans'.", id)));
        }
        String volumeGroupName = Utils.getValueFromIdByName(id, "volumegroups");
        if (volumeGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'volumegroups'.", id)));
        }
        return this.getWithResponse(resourceGroupName, elasticSanName, volumeGroupName, 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 elasticSanName = Utils.getValueFromIdByName(id, "elasticSans");
        if (elasticSanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'elasticSans'.", id)));
        }
        String volumeGroupName = Utils.getValueFromIdByName(id, "volumegroups");
        if (volumeGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'volumegroups'.", id)));
        }
        this.delete(resourceGroupName, elasticSanName, volumeGroupName, 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 elasticSanName = Utils.getValueFromIdByName(id, "elasticSans");
        if (elasticSanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'elasticSans'.", id)));
        }
        String volumeGroupName = Utils.getValueFromIdByName(id, "volumegroups");
        if (volumeGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'volumegroups'.", id)));
        }
        this.delete(resourceGroupName, elasticSanName, volumeGroupName, context);
    }

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

    private com.azure.resourcemanager.elasticsan.ElasticSanManager manager() {
        return this.serviceManager;
    }

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