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

package com.azure.resourcemanager.maps.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.maps.fluent.CreatorsClient;
import com.azure.resourcemanager.maps.fluent.models.CreatorInner;
import com.azure.resourcemanager.maps.models.Creator;
import com.azure.resourcemanager.maps.models.Creators;

public final class CreatorsImpl implements Creators {
    private static final ClientLogger LOGGER = new ClientLogger(CreatorsImpl.class);

    private final CreatorsClient innerClient;

    private final com.azure.resourcemanager.maps.AzureMapsManager serviceManager;

    public CreatorsImpl(CreatorsClient innerClient, com.azure.resourcemanager.maps.AzureMapsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Creator> listByAccount(String resourceGroupName, String accountName) {
        PagedIterable<CreatorInner> inner = this.serviceClient().listByAccount(resourceGroupName, accountName);
        return Utils.mapPage(inner, inner1 -> new CreatorImpl(inner1, this.manager()));
    }

    public PagedIterable<Creator> listByAccount(String resourceGroupName, String accountName, Context context) {
        PagedIterable<CreatorInner> inner = this.serviceClient().listByAccount(resourceGroupName, accountName, context);
        return Utils.mapPage(inner, inner1 -> new CreatorImpl(inner1, this.manager()));
    }

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

    public void delete(String resourceGroupName, String accountName, String creatorName) {
        this.serviceClient().delete(resourceGroupName, accountName, creatorName);
    }

    public Response<Creator> getWithResponse(
        String resourceGroupName, String accountName, String creatorName, Context context) {
        Response<CreatorInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, accountName, creatorName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CreatorImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Creator get(String resourceGroupName, String accountName, String creatorName) {
        CreatorInner inner = this.serviceClient().get(resourceGroupName, accountName, creatorName);
        if (inner != null) {
            return new CreatorImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.maps.AzureMapsManager manager() {
        return this.serviceManager;
    }

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