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

package com.azure.resourcemanager.qumulo.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.qumulo.fluent.FileSystemsClient;
import com.azure.resourcemanager.qumulo.fluent.models.FileSystemResourceInner;
import com.azure.resourcemanager.qumulo.models.FileSystemResource;
import com.azure.resourcemanager.qumulo.models.FileSystems;

public final class FileSystemsImpl implements FileSystems {
    private static final ClientLogger LOGGER = new ClientLogger(FileSystemsImpl.class);

    private final FileSystemsClient innerClient;

    private final com.azure.resourcemanager.qumulo.QumuloManager serviceManager;

    public FileSystemsImpl(
        FileSystemsClient innerClient, com.azure.resourcemanager.qumulo.QumuloManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<FileSystemResource> list() {
        PagedIterable<FileSystemResourceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new FileSystemResourceImpl(inner1, this.manager()));
    }

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

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

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

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

    public FileSystemResource getByResourceGroup(String resourceGroupName, String fileSystemName) {
        FileSystemResourceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, fileSystemName);
        if (inner != null) {
            return new FileSystemResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.qumulo.QumuloManager manager() {
        return this.serviceManager;
    }

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