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

package com.azure.resourcemanager.customerinsights.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.customerinsights.fluent.RoleAssignmentsClient;
import com.azure.resourcemanager.customerinsights.fluent.models.RoleAssignmentResourceFormatInner;
import com.azure.resourcemanager.customerinsights.models.RoleAssignmentResourceFormat;
import com.azure.resourcemanager.customerinsights.models.RoleAssignments;

public final class RoleAssignmentsImpl implements RoleAssignments {
    private static final ClientLogger LOGGER = new ClientLogger(RoleAssignmentsImpl.class);

    private final RoleAssignmentsClient innerClient;

    private final com.azure.resourcemanager.customerinsights.CustomerInsightsManager serviceManager;

    public RoleAssignmentsImpl(
        RoleAssignmentsClient innerClient,
        com.azure.resourcemanager.customerinsights.CustomerInsightsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<RoleAssignmentResourceFormat> listByHub(String resourceGroupName, String hubName) {
        PagedIterable<RoleAssignmentResourceFormatInner> inner =
            this.serviceClient().listByHub(resourceGroupName, hubName);
        return Utils.mapPage(inner, inner1 -> new RoleAssignmentResourceFormatImpl(inner1, this.manager()));
    }

    public PagedIterable<RoleAssignmentResourceFormat> listByHub(
        String resourceGroupName, String hubName, Context context) {
        PagedIterable<RoleAssignmentResourceFormatInner> inner =
            this.serviceClient().listByHub(resourceGroupName, hubName, context);
        return Utils.mapPage(inner, inner1 -> new RoleAssignmentResourceFormatImpl(inner1, this.manager()));
    }

    public Response<RoleAssignmentResourceFormat> getWithResponse(
        String resourceGroupName, String hubName, String assignmentName, Context context) {
        Response<RoleAssignmentResourceFormatInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, hubName, assignmentName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RoleAssignmentResourceFormatImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public RoleAssignmentResourceFormat get(String resourceGroupName, String hubName, String assignmentName) {
        RoleAssignmentResourceFormatInner inner = this.serviceClient().get(resourceGroupName, hubName, assignmentName);
        if (inner != null) {
            return new RoleAssignmentResourceFormatImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Void> deleteWithResponse(
        String resourceGroupName, String hubName, String assignmentName, Context context) {
        return this.serviceClient().deleteWithResponse(resourceGroupName, hubName, assignmentName, context);
    }

    public void delete(String resourceGroupName, String hubName, String assignmentName) {
        this.serviceClient().delete(resourceGroupName, hubName, assignmentName);
    }

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

    public Response<RoleAssignmentResourceFormat> 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 hubName = Utils.getValueFromIdByName(id, "hubs");
        if (hubName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'hubs'.", id)));
        }
        String assignmentName = Utils.getValueFromIdByName(id, "roleAssignments");
        if (assignmentName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'roleAssignments'.", id)));
        }
        return this.getWithResponse(resourceGroupName, hubName, assignmentName, 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 hubName = Utils.getValueFromIdByName(id, "hubs");
        if (hubName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'hubs'.", id)));
        }
        String assignmentName = Utils.getValueFromIdByName(id, "roleAssignments");
        if (assignmentName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'roleAssignments'.", id)));
        }
        this.deleteWithResponse(resourceGroupName, hubName, assignmentName, Context.NONE);
    }

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

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

    private com.azure.resourcemanager.customerinsights.CustomerInsightsManager manager() {
        return this.serviceManager;
    }

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