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

package com.azure.resourcemanager.batchai.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.batchai.fluent.JobsClient;
import com.azure.resourcemanager.batchai.fluent.models.FileInner;
import com.azure.resourcemanager.batchai.fluent.models.JobInner;
import com.azure.resourcemanager.batchai.fluent.models.RemoteLoginInformationInner;
import com.azure.resourcemanager.batchai.models.File;
import com.azure.resourcemanager.batchai.models.Job;
import com.azure.resourcemanager.batchai.models.Jobs;
import com.azure.resourcemanager.batchai.models.RemoteLoginInformation;
import com.fasterxml.jackson.annotation.JsonIgnore;

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

    private final JobsClient innerClient;

    private final com.azure.resourcemanager.batchai.BatchAIManager serviceManager;

    public JobsImpl(JobsClient innerClient, com.azure.resourcemanager.batchai.BatchAIManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Job> listByExperiment(String resourceGroupName, String workspaceName, String experimentName) {
        PagedIterable<JobInner> inner =
            this.serviceClient().listByExperiment(resourceGroupName, workspaceName, experimentName);
        return Utils.mapPage(inner, inner1 -> new JobImpl(inner1, this.manager()));
    }

    public PagedIterable<Job> listByExperiment(
        String resourceGroupName, String workspaceName, String experimentName, Integer maxResults, Context context) {
        PagedIterable<JobInner> inner =
            this
                .serviceClient()
                .listByExperiment(resourceGroupName, workspaceName, experimentName, maxResults, context);
        return Utils.mapPage(inner, inner1 -> new JobImpl(inner1, this.manager()));
    }

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

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

    public Job get(String resourceGroupName, String workspaceName, String experimentName, String jobName) {
        JobInner inner = this.serviceClient().get(resourceGroupName, workspaceName, experimentName, jobName);
        if (inner != null) {
            return new JobImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Job> getWithResponse(
        String resourceGroupName, String workspaceName, String experimentName, String jobName, Context context) {
        Response<JobInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, workspaceName, experimentName, jobName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new JobImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PagedIterable<File> listOutputFiles(
        String resourceGroupName,
        String workspaceName,
        String experimentName,
        String jobName,
        String outputdirectoryid) {
        PagedIterable<FileInner> inner =
            this
                .serviceClient()
                .listOutputFiles(resourceGroupName, workspaceName, experimentName, jobName, outputdirectoryid);
        return Utils.mapPage(inner, inner1 -> new FileImpl(inner1, this.manager()));
    }

    public PagedIterable<File> listOutputFiles(
        String resourceGroupName,
        String workspaceName,
        String experimentName,
        String jobName,
        String outputdirectoryid,
        String directory,
        Integer linkexpiryinminutes,
        Integer maxResults,
        Context context) {
        PagedIterable<FileInner> inner =
            this
                .serviceClient()
                .listOutputFiles(
                    resourceGroupName,
                    workspaceName,
                    experimentName,
                    jobName,
                    outputdirectoryid,
                    directory,
                    linkexpiryinminutes,
                    maxResults,
                    context);
        return Utils.mapPage(inner, inner1 -> new FileImpl(inner1, this.manager()));
    }

    public PagedIterable<RemoteLoginInformation> listRemoteLoginInformation(
        String resourceGroupName, String workspaceName, String experimentName, String jobName) {
        PagedIterable<RemoteLoginInformationInner> inner =
            this.serviceClient().listRemoteLoginInformation(resourceGroupName, workspaceName, experimentName, jobName);
        return Utils.mapPage(inner, inner1 -> new RemoteLoginInformationImpl(inner1, this.manager()));
    }

    public PagedIterable<RemoteLoginInformation> listRemoteLoginInformation(
        String resourceGroupName, String workspaceName, String experimentName, String jobName, Context context) {
        PagedIterable<RemoteLoginInformationInner> inner =
            this
                .serviceClient()
                .listRemoteLoginInformation(resourceGroupName, workspaceName, experimentName, jobName, context);
        return Utils.mapPage(inner, inner1 -> new RemoteLoginInformationImpl(inner1, this.manager()));
    }

    public void terminate(String resourceGroupName, String workspaceName, String experimentName, String jobName) {
        this.serviceClient().terminate(resourceGroupName, workspaceName, experimentName, jobName);
    }

    public void terminate(
        String resourceGroupName, String workspaceName, String experimentName, String jobName, Context context) {
        this.serviceClient().terminate(resourceGroupName, workspaceName, experimentName, jobName, context);
    }

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

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

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

    private com.azure.resourcemanager.batchai.BatchAIManager manager() {
        return this.serviceManager;
    }

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