// 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.IntegrationAccountsClient;
import com.azure.resourcemanager.logic.fluent.models.CallbackUrlInner;
import com.azure.resourcemanager.logic.fluent.models.IntegrationAccountInner;
import com.azure.resourcemanager.logic.fluent.models.KeyVaultKeyInner;
import com.azure.resourcemanager.logic.models.CallbackUrl;
import com.azure.resourcemanager.logic.models.GetCallbackUrlParameters;
import com.azure.resourcemanager.logic.models.IntegrationAccount;
import com.azure.resourcemanager.logic.models.IntegrationAccounts;
import com.azure.resourcemanager.logic.models.KeyVaultKey;
import com.azure.resourcemanager.logic.models.ListKeyVaultKeysDefinition;
import com.azure.resourcemanager.logic.models.RegenerateActionParameter;
import com.azure.resourcemanager.logic.models.TrackingEventsDefinition;

public final class IntegrationAccountsImpl implements IntegrationAccounts {
    private static final ClientLogger LOGGER = new ClientLogger(IntegrationAccountsImpl.class);

    private final IntegrationAccountsClient innerClient;

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

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

    public PagedIterable<IntegrationAccount> list() {
        PagedIterable<IntegrationAccountInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new IntegrationAccountImpl(inner1, this.manager()));
    }

    public PagedIterable<IntegrationAccount> list(Integer top, Context context) {
        PagedIterable<IntegrationAccountInner> inner = this.serviceClient().list(top, context);
        return Utils.mapPage(inner, inner1 -> new IntegrationAccountImpl(inner1, this.manager()));
    }

    public PagedIterable<IntegrationAccount> listByResourceGroup(String resourceGroupName) {
        PagedIterable<IntegrationAccountInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new IntegrationAccountImpl(inner1, this.manager()));
    }

    public PagedIterable<IntegrationAccount> listByResourceGroup(
        String resourceGroupName, Integer top, Context context) {
        PagedIterable<IntegrationAccountInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, top, context);
        return Utils.mapPage(inner, inner1 -> new IntegrationAccountImpl(inner1, this.manager()));
    }

    public Response<IntegrationAccount> getByResourceGroupWithResponse(
        String resourceGroupName, String integrationAccountName, Context context) {
        Response<IntegrationAccountInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, integrationAccountName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new IntegrationAccountImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public IntegrationAccount getByResourceGroup(String resourceGroupName, String integrationAccountName) {
        IntegrationAccountInner inner =
            this.serviceClient().getByResourceGroup(resourceGroupName, integrationAccountName);
        if (inner != null) {
            return new IntegrationAccountImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public Response<CallbackUrl> listCallbackUrlWithResponse(
        String resourceGroupName, String integrationAccountName, GetCallbackUrlParameters parameters, Context context) {
        Response<CallbackUrlInner> inner =
            this
                .serviceClient()
                .listCallbackUrlWithResponse(resourceGroupName, integrationAccountName, parameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CallbackUrlImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public CallbackUrl listCallbackUrl(
        String resourceGroupName, String integrationAccountName, GetCallbackUrlParameters parameters) {
        CallbackUrlInner inner =
            this.serviceClient().listCallbackUrl(resourceGroupName, integrationAccountName, parameters);
        if (inner != null) {
            return new CallbackUrlImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<KeyVaultKey> listKeyVaultKeys(
        String resourceGroupName, String integrationAccountName, ListKeyVaultKeysDefinition listKeyVaultKeys) {
        PagedIterable<KeyVaultKeyInner> inner =
            this.serviceClient().listKeyVaultKeys(resourceGroupName, integrationAccountName, listKeyVaultKeys);
        return Utils.mapPage(inner, inner1 -> new KeyVaultKeyImpl(inner1, this.manager()));
    }

    public PagedIterable<KeyVaultKey> listKeyVaultKeys(
        String resourceGroupName,
        String integrationAccountName,
        ListKeyVaultKeysDefinition listKeyVaultKeys,
        Context context) {
        PagedIterable<KeyVaultKeyInner> inner =
            this.serviceClient().listKeyVaultKeys(resourceGroupName, integrationAccountName, listKeyVaultKeys, context);
        return Utils.mapPage(inner, inner1 -> new KeyVaultKeyImpl(inner1, this.manager()));
    }

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

    public void logTrackingEvents(
        String resourceGroupName, String integrationAccountName, TrackingEventsDefinition logTrackingEvents) {
        this.serviceClient().logTrackingEvents(resourceGroupName, integrationAccountName, logTrackingEvents);
    }

    public Response<IntegrationAccount> regenerateAccessKeyWithResponse(
        String resourceGroupName,
        String integrationAccountName,
        RegenerateActionParameter regenerateAccessKey,
        Context context) {
        Response<IntegrationAccountInner> inner =
            this
                .serviceClient()
                .regenerateAccessKeyWithResponse(
                    resourceGroupName, integrationAccountName, regenerateAccessKey, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new IntegrationAccountImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public IntegrationAccount regenerateAccessKey(
        String resourceGroupName, String integrationAccountName, RegenerateActionParameter regenerateAccessKey) {
        IntegrationAccountInner inner =
            this.serviceClient().regenerateAccessKey(resourceGroupName, integrationAccountName, regenerateAccessKey);
        if (inner != null) {
            return new IntegrationAccountImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public IntegrationAccount 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)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, integrationAccountName, Context.NONE).getValue();
    }

    public Response<IntegrationAccount> 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)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, integrationAccountName, 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)));
        }
        this.deleteByResourceGroupWithResponse(resourceGroupName, integrationAccountName, 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)));
        }
        return this.deleteByResourceGroupWithResponse(resourceGroupName, integrationAccountName, context);
    }

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

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

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