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

package com.azure.resourcemanager.securityinsights.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.securityinsights.fluent.FileImportsClient;
import com.azure.resourcemanager.securityinsights.fluent.models.FileImportInner;
import com.azure.resourcemanager.securityinsights.models.FileImport;
import com.azure.resourcemanager.securityinsights.models.FileImports;

public final class FileImportsImpl implements FileImports {
    private static final ClientLogger LOGGER = new ClientLogger(FileImportsImpl.class);

    private final FileImportsClient innerClient;

    private final com.azure.resourcemanager.securityinsights.SecurityInsightsManager serviceManager;

    public FileImportsImpl(
        FileImportsClient innerClient,
        com.azure.resourcemanager.securityinsights.SecurityInsightsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<FileImport> list(String resourceGroupName, String workspaceName) {
        PagedIterable<FileImportInner> inner = this.serviceClient().list(resourceGroupName, workspaceName);
        return Utils.mapPage(inner, inner1 -> new FileImportImpl(inner1, this.manager()));
    }

    public PagedIterable<FileImport> list(
        String resourceGroupName,
        String workspaceName,
        String filter,
        String orderby,
        Integer top,
        String skipToken,
        Context context) {
        PagedIterable<FileImportInner> inner =
            this.serviceClient().list(resourceGroupName, workspaceName, filter, orderby, top, skipToken, context);
        return Utils.mapPage(inner, inner1 -> new FileImportImpl(inner1, this.manager()));
    }

    public FileImport get(String resourceGroupName, String workspaceName, String fileImportId) {
        FileImportInner inner = this.serviceClient().get(resourceGroupName, workspaceName, fileImportId);
        if (inner != null) {
            return new FileImportImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<FileImport> getWithResponse(
        String resourceGroupName, String workspaceName, String fileImportId, Context context) {
        Response<FileImportInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, workspaceName, fileImportId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new FileImportImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public FileImport delete(String resourceGroupName, String workspaceName, String fileImportId) {
        FileImportInner inner = this.serviceClient().delete(resourceGroupName, workspaceName, fileImportId);
        if (inner != null) {
            return new FileImportImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public FileImport delete(String resourceGroupName, String workspaceName, String fileImportId, Context context) {
        FileImportInner inner = this.serviceClient().delete(resourceGroupName, workspaceName, fileImportId, context);
        if (inner != null) {
            return new FileImportImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.securityinsights.SecurityInsightsManager manager() {
        return this.serviceManager;
    }

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