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

package com.azure.resourcemanager.logic.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.logic.fluent.IntegrationAccountMapsClient;
import com.azure.resourcemanager.logic.fluent.models.IntegrationAccountMapInner;
import com.azure.resourcemanager.logic.fluent.models.WorkflowTriggerCallbackUrlInner;
import com.azure.resourcemanager.logic.models.GetCallbackUrlParameters;
import com.azure.resourcemanager.logic.models.IntegrationAccountMap;
import com.azure.resourcemanager.logic.models.IntegrationAccountMaps;
import com.azure.resourcemanager.logic.models.WorkflowTriggerCallbackUrl;

public final class IntegrationAccountMapsImpl implements IntegrationAccountMaps {
    private static final ClientLogger LOGGER = new ClientLogger(IntegrationAccountMapsImpl.class);

    private final IntegrationAccountMapsClient innerClient;

    private final com.azure.resourcemanager.logic.LogicManager serviceManager;

    public IntegrationAccountMapsImpl(
        IntegrationAccountMapsClient innerClient, com.azure.resourcemanager.logic.LogicManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<IntegrationAccountMap> list(String resourceGroupName, String integrationAccountName) {
        PagedIterable<IntegrationAccountMapInner> inner =
            this.serviceClient().list(resourceGroupName, integrationAccountName);
        return Utils.mapPage(inner, inner1 -> new IntegrationAccountMapImpl(inner1, this.manager()));
    }

    public PagedIterable<IntegrationAccountMap> list(
        String resourceGroupName, String integrationAccountName, Integer top, String filter, Context context) {
        PagedIterable<IntegrationAccountMapInner> inner =
            this.serviceClient().list(resourceGroupName, integrationAccountName, top, filter, context);
        return Utils.mapPage(inner, inner1 -> new IntegrationAccountMapImpl(inner1, this.manager()));
    }

    public Response<IntegrationAccountMap> getWithResponse(
        String resourceGroupName, String integrationAccountName, String mapName, Context context) {
        Response<IntegrationAccountMapInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, integrationAccountName, mapName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new IntegrationAccountMapImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public IntegrationAccountMap get(String resourceGroupName, String integrationAccountName, String mapName) {
        IntegrationAccountMapInner inner = this.serviceClient().get(resourceGroupName, integrationAccountName, mapName);
        if (inner != null) {
            return new IntegrationAccountMapImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void delete(String resourceGroupName, String integrationAccountName, String mapName) {
        this.serviceClient().delete(resourceGroupName, integrationAccountName, mapName);
    }

    public Response<WorkflowTriggerCallbackUrl> listContentCallbackUrlWithResponse(
        String resourceGroupName,
        String integrationAccountName,
        String mapName,
        GetCallbackUrlParameters listContentCallbackUrl,
        Context context) {
        Response<WorkflowTriggerCallbackUrlInner> inner =
            this
                .serviceClient()
                .listContentCallbackUrlWithResponse(
                    resourceGroupName, integrationAccountName, mapName, listContentCallbackUrl, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new WorkflowTriggerCallbackUrlImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public WorkflowTriggerCallbackUrl listContentCallbackUrl(
        String resourceGroupName,
        String integrationAccountName,
        String mapName,
        GetCallbackUrlParameters listContentCallbackUrl) {
        WorkflowTriggerCallbackUrlInner inner =
            this
                .serviceClient()
                .listContentCallbackUrl(resourceGroupName, integrationAccountName, mapName, listContentCallbackUrl);
        if (inner != null) {
            return new WorkflowTriggerCallbackUrlImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.logic.LogicManager manager() {
        return this.serviceManager;
    }

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