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

package com.azure.resourcemanager.apimanagement.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.apimanagement.fluent.ApiDiagnosticsClient;
import com.azure.resourcemanager.apimanagement.fluent.models.DiagnosticContractInner;
import com.azure.resourcemanager.apimanagement.models.ApiDiagnostics;
import com.azure.resourcemanager.apimanagement.models.ApiDiagnosticsGetEntityTagResponse;
import com.azure.resourcemanager.apimanagement.models.ApiDiagnosticsGetResponse;
import com.azure.resourcemanager.apimanagement.models.DiagnosticContract;

public final class ApiDiagnosticsImpl implements ApiDiagnostics {
    private static final ClientLogger LOGGER = new ClientLogger(ApiDiagnosticsImpl.class);

    private final ApiDiagnosticsClient innerClient;

    private final com.azure.resourcemanager.apimanagement.ApiManagementManager serviceManager;

    public ApiDiagnosticsImpl(
        ApiDiagnosticsClient innerClient, com.azure.resourcemanager.apimanagement.ApiManagementManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<DiagnosticContract> listByService(String resourceGroupName, String serviceName, String apiId) {
        PagedIterable<DiagnosticContractInner> inner =
            this.serviceClient().listByService(resourceGroupName, serviceName, apiId);
        return Utils.mapPage(inner, inner1 -> new DiagnosticContractImpl(inner1, this.manager()));
    }

    public PagedIterable<DiagnosticContract> listByService(
        String resourceGroupName,
        String serviceName,
        String apiId,
        String filter,
        Integer top,
        Integer skip,
        Context context) {
        PagedIterable<DiagnosticContractInner> inner =
            this.serviceClient().listByService(resourceGroupName, serviceName, apiId, filter, top, skip, context);
        return Utils.mapPage(inner, inner1 -> new DiagnosticContractImpl(inner1, this.manager()));
    }

    public void getEntityTag(String resourceGroupName, String serviceName, String apiId, String diagnosticId) {
        this.serviceClient().getEntityTag(resourceGroupName, serviceName, apiId, diagnosticId);
    }

    public ApiDiagnosticsGetEntityTagResponse getEntityTagWithResponse(
        String resourceGroupName, String serviceName, String apiId, String diagnosticId, Context context) {
        return this
            .serviceClient()
            .getEntityTagWithResponse(resourceGroupName, serviceName, apiId, diagnosticId, context);
    }

    public DiagnosticContract get(String resourceGroupName, String serviceName, String apiId, String diagnosticId) {
        DiagnosticContractInner inner = this.serviceClient().get(resourceGroupName, serviceName, apiId, diagnosticId);
        if (inner != null) {
            return new DiagnosticContractImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<DiagnosticContract> getWithResponse(
        String resourceGroupName, String serviceName, String apiId, String diagnosticId, Context context) {
        ApiDiagnosticsGetResponse inner =
            this.serviceClient().getWithResponse(resourceGroupName, serviceName, apiId, diagnosticId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new DiagnosticContractImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void delete(
        String resourceGroupName, String serviceName, String apiId, String diagnosticId, String ifMatch) {
        this.serviceClient().delete(resourceGroupName, serviceName, apiId, diagnosticId, ifMatch);
    }

    public Response<Void> deleteWithResponse(
        String resourceGroupName,
        String serviceName,
        String apiId,
        String diagnosticId,
        String ifMatch,
        Context context) {
        return this
            .serviceClient()
            .deleteWithResponse(resourceGroupName, serviceName, apiId, diagnosticId, ifMatch, context);
    }

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

    public Response<DiagnosticContract> 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 serviceName = Utils.getValueFromIdByName(id, "service");
        if (serviceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'service'.", id)));
        }
        String apiId = Utils.getValueFromIdByName(id, "apis");
        if (apiId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'apis'.", id)));
        }
        String diagnosticId = Utils.getValueFromIdByName(id, "diagnostics");
        if (diagnosticId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'diagnostics'.", id)));
        }
        return this.getWithResponse(resourceGroupName, serviceName, apiId, diagnosticId, 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 serviceName = Utils.getValueFromIdByName(id, "service");
        if (serviceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'service'.", id)));
        }
        String apiId = Utils.getValueFromIdByName(id, "apis");
        if (apiId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'apis'.", id)));
        }
        String diagnosticId = Utils.getValueFromIdByName(id, "diagnostics");
        if (diagnosticId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'diagnostics'.", id)));
        }
        String localIfMatch = null;
        this.deleteWithResponse(resourceGroupName, serviceName, apiId, diagnosticId, localIfMatch, Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(String id, String ifMatch, 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 serviceName = Utils.getValueFromIdByName(id, "service");
        if (serviceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'service'.", id)));
        }
        String apiId = Utils.getValueFromIdByName(id, "apis");
        if (apiId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'apis'.", id)));
        }
        String diagnosticId = Utils.getValueFromIdByName(id, "diagnostics");
        if (diagnosticId == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'diagnostics'.", id)));
        }
        return this.deleteWithResponse(resourceGroupName, serviceName, apiId, diagnosticId, ifMatch, context);
    }

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

    private com.azure.resourcemanager.apimanagement.ApiManagementManager manager() {
        return this.serviceManager;
    }

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