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

package com.azure.resourcemanager.databox.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.databox.fluent.JobsClient;
import com.azure.resourcemanager.databox.fluent.models.JobResourceInner;
import com.azure.resourcemanager.databox.fluent.models.ShipmentPickUpResponseInner;
import com.azure.resourcemanager.databox.fluent.models.UnencryptedCredentialsInner;
import com.azure.resourcemanager.databox.models.CancellationReason;
import com.azure.resourcemanager.databox.models.JobResource;
import com.azure.resourcemanager.databox.models.Jobs;
import com.azure.resourcemanager.databox.models.MarkDevicesShippedRequest;
import com.azure.resourcemanager.databox.models.ShipmentPickUpRequest;
import com.azure.resourcemanager.databox.models.ShipmentPickUpResponse;
import com.azure.resourcemanager.databox.models.UnencryptedCredentials;

public final class JobsImpl implements Jobs {
    private static final ClientLogger LOGGER = new ClientLogger(JobsImpl.class);

    private final JobsClient innerClient;

    private final com.azure.resourcemanager.databox.DataBoxManager serviceManager;

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

    public PagedIterable<JobResource> list() {
        PagedIterable<JobResourceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new JobResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<JobResource> list(String skipToken, Context context) {
        PagedIterable<JobResourceInner> inner = this.serviceClient().list(skipToken, context);
        return Utils.mapPage(inner, inner1 -> new JobResourceImpl(inner1, this.manager()));
    }

    public Response<Void> markDevicesShippedWithResponse(
        String jobName,
        String resourceGroupName,
        MarkDevicesShippedRequest markDevicesShippedRequest,
        Context context) {
        return this
            .serviceClient()
            .markDevicesShippedWithResponse(jobName, resourceGroupName, markDevicesShippedRequest, context);
    }

    public void markDevicesShipped(
        String jobName, String resourceGroupName, MarkDevicesShippedRequest markDevicesShippedRequest) {
        this.serviceClient().markDevicesShipped(jobName, resourceGroupName, markDevicesShippedRequest);
    }

    public PagedIterable<JobResource> listByResourceGroup(String resourceGroupName) {
        PagedIterable<JobResourceInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new JobResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<JobResource> listByResourceGroup(String resourceGroupName, String skipToken, Context context) {
        PagedIterable<JobResourceInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, skipToken, context);
        return Utils.mapPage(inner, inner1 -> new JobResourceImpl(inner1, this.manager()));
    }

    public Response<JobResource> getByResourceGroupWithResponse(
        String resourceGroupName, String jobName, String expand, Context context) {
        Response<JobResourceInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, jobName, expand, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new JobResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public JobResource getByResourceGroup(String resourceGroupName, String jobName) {
        JobResourceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, jobName);
        if (inner != null) {
            return new JobResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public Response<ShipmentPickUpResponse> bookShipmentPickUpWithResponse(
        String resourceGroupName, String jobName, ShipmentPickUpRequest shipmentPickUpRequest, Context context) {
        Response<ShipmentPickUpResponseInner> inner =
            this
                .serviceClient()
                .bookShipmentPickUpWithResponse(resourceGroupName, jobName, shipmentPickUpRequest, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ShipmentPickUpResponseImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ShipmentPickUpResponse bookShipmentPickUp(
        String resourceGroupName, String jobName, ShipmentPickUpRequest shipmentPickUpRequest) {
        ShipmentPickUpResponseInner inner =
            this.serviceClient().bookShipmentPickUp(resourceGroupName, jobName, shipmentPickUpRequest);
        if (inner != null) {
            return new ShipmentPickUpResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Void> cancelWithResponse(
        String resourceGroupName, String jobName, CancellationReason cancellationReason, Context context) {
        return this.serviceClient().cancelWithResponse(resourceGroupName, jobName, cancellationReason, context);
    }

    public void cancel(String resourceGroupName, String jobName, CancellationReason cancellationReason) {
        this.serviceClient().cancel(resourceGroupName, jobName, cancellationReason);
    }

    public PagedIterable<UnencryptedCredentials> listCredentials(String resourceGroupName, String jobName) {
        PagedIterable<UnencryptedCredentialsInner> inner =
            this.serviceClient().listCredentials(resourceGroupName, jobName);
        return Utils.mapPage(inner, inner1 -> new UnencryptedCredentialsImpl(inner1, this.manager()));
    }

    public PagedIterable<UnencryptedCredentials> listCredentials(
        String resourceGroupName, String jobName, Context context) {
        PagedIterable<UnencryptedCredentialsInner> inner =
            this.serviceClient().listCredentials(resourceGroupName, jobName, context);
        return Utils.mapPage(inner, inner1 -> new UnencryptedCredentialsImpl(inner1, this.manager()));
    }

    public JobResource 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 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)));
        }
        String localExpand = null;
        return this.getByResourceGroupWithResponse(resourceGroupName, jobName, localExpand, Context.NONE).getValue();
    }

    public Response<JobResource> getByIdWithResponse(String id, String 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 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.getByResourceGroupWithResponse(resourceGroupName, jobName, 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 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, 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 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, jobName, context);
    }

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

    private com.azure.resourcemanager.databox.DataBoxManager manager() {
        return this.serviceManager;
    }

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