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

package com.azure.resourcemanager.cosmosdbforpostgresql.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.cosmosdbforpostgresql.fluent.RolesClient;
import com.azure.resourcemanager.cosmosdbforpostgresql.fluent.models.RoleInner;
import com.azure.resourcemanager.cosmosdbforpostgresql.models.Role;
import com.azure.resourcemanager.cosmosdbforpostgresql.models.Roles;

public final class RolesImpl implements Roles {
    private static final ClientLogger LOGGER = new ClientLogger(RolesImpl.class);

    private final RolesClient innerClient;

    private final com.azure.resourcemanager.cosmosdbforpostgresql.CosmosDBForPostgreSqlManager serviceManager;

    public RolesImpl(
        RolesClient innerClient,
        com.azure.resourcemanager.cosmosdbforpostgresql.CosmosDBForPostgreSqlManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<Role> getWithResponse(
        String resourceGroupName, String clusterName, String roleName, Context context) {
        Response<RoleInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, clusterName, roleName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RoleImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Role get(String resourceGroupName, String clusterName, String roleName) {
        RoleInner inner = this.serviceClient().get(resourceGroupName, clusterName, roleName);
        if (inner != null) {
            return new RoleImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String clusterName, String roleName) {
        this.serviceClient().delete(resourceGroupName, clusterName, roleName);
    }

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

    public PagedIterable<Role> listByCluster(String resourceGroupName, String clusterName) {
        PagedIterable<RoleInner> inner = this.serviceClient().listByCluster(resourceGroupName, clusterName);
        return Utils.mapPage(inner, inner1 -> new RoleImpl(inner1, this.manager()));
    }

    public PagedIterable<Role> listByCluster(String resourceGroupName, String clusterName, Context context) {
        PagedIterable<RoleInner> inner = this.serviceClient().listByCluster(resourceGroupName, clusterName, context);
        return Utils.mapPage(inner, inner1 -> new RoleImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.cosmosdbforpostgresql.CosmosDBForPostgreSqlManager manager() {
        return this.serviceManager;
    }

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