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

package com.azure.resourcemanager.healthcareapis.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.healthcareapis.fluent.DicomServicesClient;
import com.azure.resourcemanager.healthcareapis.fluent.models.DicomServiceInner;
import com.azure.resourcemanager.healthcareapis.models.DicomService;
import com.azure.resourcemanager.healthcareapis.models.DicomServices;

public final class DicomServicesImpl implements DicomServices {
    private static final ClientLogger LOGGER = new ClientLogger(DicomServicesImpl.class);

    private final DicomServicesClient innerClient;

    private final com.azure.resourcemanager.healthcareapis.HealthcareApisManager serviceManager;

    public DicomServicesImpl(
        DicomServicesClient innerClient,
        com.azure.resourcemanager.healthcareapis.HealthcareApisManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<DicomService> listByWorkspace(String resourceGroupName, String workspaceName) {
        PagedIterable<DicomServiceInner> inner = this.serviceClient().listByWorkspace(resourceGroupName, workspaceName);
        return Utils.mapPage(inner, inner1 -> new DicomServiceImpl(inner1, this.manager()));
    }

    public PagedIterable<DicomService> listByWorkspace(
        String resourceGroupName, String workspaceName, Context context) {
        PagedIterable<DicomServiceInner> inner =
            this.serviceClient().listByWorkspace(resourceGroupName, workspaceName, context);
        return Utils.mapPage(inner, inner1 -> new DicomServiceImpl(inner1, this.manager()));
    }

    public DicomService get(String resourceGroupName, String workspaceName, String dicomServiceName) {
        DicomServiceInner inner = this.serviceClient().get(resourceGroupName, workspaceName, dicomServiceName);
        if (inner != null) {
            return new DicomServiceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<DicomService> getWithResponse(
        String resourceGroupName, String workspaceName, String dicomServiceName, Context context) {
        Response<DicomServiceInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, workspaceName, dicomServiceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new DicomServiceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String dicomServiceName, String workspaceName) {
        this.serviceClient().delete(resourceGroupName, dicomServiceName, workspaceName);
    }

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

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

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

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

    private com.azure.resourcemanager.healthcareapis.HealthcareApisManager manager() {
        return this.serviceManager;
    }

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