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

package com.azure.resourcemanager.machinelearning.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.machinelearning.fluent.ModelVersionsClient;
import com.azure.resourcemanager.machinelearning.fluent.models.ModelVersionInner;
import com.azure.resourcemanager.machinelearning.models.ListViewType;
import com.azure.resourcemanager.machinelearning.models.ModelVersion;
import com.azure.resourcemanager.machinelearning.models.ModelVersions;

public final class ModelVersionsImpl implements ModelVersions {
    private static final ClientLogger LOGGER = new ClientLogger(ModelVersionsImpl.class);

    private final ModelVersionsClient innerClient;

    private final com.azure.resourcemanager.machinelearning.MachineLearningManager serviceManager;

    public ModelVersionsImpl(
        ModelVersionsClient innerClient,
        com.azure.resourcemanager.machinelearning.MachineLearningManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<ModelVersion> list(String resourceGroupName, String workspaceName, String name) {
        PagedIterable<ModelVersionInner> inner = this.serviceClient().list(resourceGroupName, workspaceName, name);
        return Utils.mapPage(inner, inner1 -> new ModelVersionImpl(inner1, this.manager()));
    }

    public PagedIterable<ModelVersion> list(
        String resourceGroupName,
        String workspaceName,
        String name,
        String skip,
        String orderBy,
        Integer top,
        String version,
        String description,
        Integer offset,
        String tags,
        String properties,
        String feed,
        ListViewType listViewType,
        Context context) {
        PagedIterable<ModelVersionInner> inner =
            this
                .serviceClient()
                .list(
                    resourceGroupName,
                    workspaceName,
                    name,
                    skip,
                    orderBy,
                    top,
                    version,
                    description,
                    offset,
                    tags,
                    properties,
                    feed,
                    listViewType,
                    context);
        return Utils.mapPage(inner, inner1 -> new ModelVersionImpl(inner1, this.manager()));
    }

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

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

    public Response<ModelVersion> getWithResponse(
        String resourceGroupName, String workspaceName, String name, String version, Context context) {
        Response<ModelVersionInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, workspaceName, name, version, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ModelVersionImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ModelVersion get(String resourceGroupName, String workspaceName, String name, String version) {
        ModelVersionInner inner = this.serviceClient().get(resourceGroupName, workspaceName, name, version);
        if (inner != null) {
            return new ModelVersionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.machinelearning.MachineLearningManager manager() {
        return this.serviceManager;
    }

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