// 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.ElasticSansClient;
import com.azure.resourcemanager.elasticsan.fluent.models.ElasticSanInner;
import com.azure.resourcemanager.elasticsan.models.ElasticSan;
import com.azure.resourcemanager.elasticsan.models.ElasticSans;

public final class ElasticSansImpl implements ElasticSans {
    private static final ClientLogger LOGGER = new ClientLogger(ElasticSansImpl.class);

    private final ElasticSansClient innerClient;

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

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

    public PagedIterable<ElasticSan> list() {
        PagedIterable<ElasticSanInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new ElasticSanImpl(inner1, this.manager()));
    }

    public PagedIterable<ElasticSan> list(Context context) {
        PagedIterable<ElasticSanInner> inner = this.serviceClient().list(context);
        return Utils.mapPage(inner, inner1 -> new ElasticSanImpl(inner1, this.manager()));
    }

    public PagedIterable<ElasticSan> listByResourceGroup(String resourceGroupName) {
        PagedIterable<ElasticSanInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new ElasticSanImpl(inner1, this.manager()));
    }

    public PagedIterable<ElasticSan> listByResourceGroup(String resourceGroupName, Context context) {
        PagedIterable<ElasticSanInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new ElasticSanImpl(inner1, this.manager()));
    }

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

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

    public Response<ElasticSan> getByResourceGroupWithResponse(
        String resourceGroupName, String elasticSanName, Context context) {
        Response<ElasticSanInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, elasticSanName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ElasticSanImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ElasticSan getByResourceGroup(String resourceGroupName, String elasticSanName) {
        ElasticSanInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, elasticSanName);
        if (inner != null) {
            return new ElasticSanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public ElasticSan 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)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, elasticSanName, Context.NONE).getValue();
    }

    public Response<ElasticSan> 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)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, elasticSanName, 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)));
        }
        this.delete(resourceGroupName, elasticSanName, 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)));
        }
        this.delete(resourceGroupName, elasticSanName, context);
    }

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

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

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