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

package com.azure.resourcemanager.machinelearningservices.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.machinelearningservices.fluent.MachineLearningServicesClient;
import com.azure.resourcemanager.machinelearningservices.fluent.models.ServiceResourceInner;
import com.azure.resourcemanager.machinelearningservices.models.MachineLearningServices;
import com.azure.resourcemanager.machinelearningservices.models.OrderString;
import com.azure.resourcemanager.machinelearningservices.models.ServiceResource;
import com.fasterxml.jackson.annotation.JsonIgnore;

public final class MachineLearningServicesImpl implements MachineLearningServices {
    @JsonIgnore private final ClientLogger logger = new ClientLogger(MachineLearningServicesImpl.class);

    private final MachineLearningServicesClient innerClient;

    private final com.azure.resourcemanager.machinelearningservices.MachineLearningServicesManager serviceManager;

    public MachineLearningServicesImpl(
        MachineLearningServicesClient innerClient,
        com.azure.resourcemanager.machinelearningservices.MachineLearningServicesManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

    public PagedIterable<ServiceResource> listByWorkspace(
        String resourceGroupName,
        String workspaceName,
        String skip,
        String modelId,
        String modelName,
        String tag,
        String tags,
        String properties,
        String runId,
        Boolean expand,
        OrderString orderby,
        Context context) {
        PagedIterable<ServiceResourceInner> inner =
            this
                .serviceClient()
                .listByWorkspace(
                    resourceGroupName,
                    workspaceName,
                    skip,
                    modelId,
                    modelName,
                    tag,
                    tags,
                    properties,
                    runId,
                    expand,
                    orderby,
                    context);
        return Utils.mapPage(inner, inner1 -> new ServiceResourceImpl(inner1, this.manager()));
    }

    public ServiceResource get(String resourceGroupName, String workspaceName, String serviceName) {
        ServiceResourceInner inner = this.serviceClient().get(resourceGroupName, workspaceName, serviceName);
        if (inner != null) {
            return new ServiceResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<ServiceResource> getWithResponse(
        String resourceGroupName, String workspaceName, String serviceName, Boolean expand, Context context) {
        Response<ServiceResourceInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, workspaceName, serviceName, expand, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ServiceResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

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

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

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

    public Response<ServiceResource> getByIdWithResponse(String id, Boolean expand, 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 serviceName = Utils.getValueFromIdByName(id, "services");
        if (serviceName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'services'.", id)));
        }
        return this.getWithResponse(resourceGroupName, workspaceName, serviceName, expand, 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 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 serviceName = Utils.getValueFromIdByName(id, "services");
        if (serviceName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'services'.", id)));
        }
        this.deleteWithResponse(resourceGroupName, workspaceName, serviceName, Context.NONE).getValue();
    }

    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 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 serviceName = Utils.getValueFromIdByName(id, "services");
        if (serviceName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'services'.", id)));
        }
        return this.deleteWithResponse(resourceGroupName, workspaceName, serviceName, context);
    }

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

    private com.azure.resourcemanager.machinelearningservices.MachineLearningServicesManager manager() {
        return this.serviceManager;
    }

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