// 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.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.databox.fluent.models.JobResourceInner;
import com.azure.resourcemanager.databox.models.CancellationReason;
import com.azure.resourcemanager.databox.models.CloudError;
import com.azure.resourcemanager.databox.models.JobDeliveryInfo;
import com.azure.resourcemanager.databox.models.JobDeliveryType;
import com.azure.resourcemanager.databox.models.JobDetails;
import com.azure.resourcemanager.databox.models.JobResource;
import com.azure.resourcemanager.databox.models.JobResourceUpdateParameter;
import com.azure.resourcemanager.databox.models.MarkDevicesShippedRequest;
import com.azure.resourcemanager.databox.models.ResourceIdentity;
import com.azure.resourcemanager.databox.models.ReverseShippingDetailsEditStatus;
import com.azure.resourcemanager.databox.models.ReverseTransportPreferenceEditStatus;
import com.azure.resourcemanager.databox.models.ShipmentPickUpRequest;
import com.azure.resourcemanager.databox.models.ShipmentPickUpResponse;
import com.azure.resourcemanager.databox.models.Sku;
import com.azure.resourcemanager.databox.models.StageName;
import com.azure.resourcemanager.databox.models.TransferType;
import com.azure.resourcemanager.databox.models.UnencryptedCredentials;
import com.azure.resourcemanager.databox.models.UpdateJobDetails;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;

public final class JobResourceImpl implements JobResource, JobResource.Definition, JobResource.Update {
    private JobResourceInner innerObject;

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

    public String location() {
        return this.innerModel().location();
    }

    public Map<String, String> tags() {
        Map<String, String> inner = this.innerModel().tags();
        if (inner != null) {
            return Collections.unmodifiableMap(inner);
        } else {
            return Collections.emptyMap();
        }
    }

    public String name() {
        return this.innerModel().name();
    }

    public String id() {
        return this.innerModel().id();
    }

    public String type() {
        return this.innerModel().type();
    }

    public SystemData systemData() {
        return this.innerModel().systemData();
    }

    public Sku sku() {
        return this.innerModel().sku();
    }

    public ResourceIdentity identity() {
        return this.innerModel().identity();
    }

    public TransferType transferType() {
        return this.innerModel().transferType();
    }

    public Boolean isCancellable() {
        return this.innerModel().isCancellable();
    }

    public Boolean isDeletable() {
        return this.innerModel().isDeletable();
    }

    public Boolean isShippingAddressEditable() {
        return this.innerModel().isShippingAddressEditable();
    }

    public ReverseShippingDetailsEditStatus reverseShippingDetailsUpdate() {
        return this.innerModel().reverseShippingDetailsUpdate();
    }

    public ReverseTransportPreferenceEditStatus reverseTransportPreferenceUpdate() {
        return this.innerModel().reverseTransportPreferenceUpdate();
    }

    public Boolean isPrepareToShipEnabled() {
        return this.innerModel().isPrepareToShipEnabled();
    }

    public StageName status() {
        return this.innerModel().status();
    }

    public OffsetDateTime startTime() {
        return this.innerModel().startTime();
    }

    public CloudError error() {
        return this.innerModel().error();
    }

    public JobDetails details() {
        return this.innerModel().details();
    }

    public String cancellationReason() {
        return this.innerModel().cancellationReason();
    }

    public JobDeliveryType deliveryType() {
        return this.innerModel().deliveryType();
    }

    public JobDeliveryInfo deliveryInfo() {
        return this.innerModel().deliveryInfo();
    }

    public Boolean isCancellableWithoutFee() {
        return this.innerModel().isCancellableWithoutFee();
    }

    public Region region() {
        return Region.fromName(this.regionName());
    }

    public String regionName() {
        return this.location();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

    public JobResourceInner innerModel() {
        return this.innerObject;
    }

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

    private String resourceGroupName;

    private String jobName;

    private String updateIfMatch;

    private JobResourceUpdateParameter updateJobResourceUpdateParameter;

    public JobResourceImpl withExistingResourceGroup(String resourceGroupName) {
        this.resourceGroupName = resourceGroupName;
        return this;
    }

    public JobResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getJobs()
                .create(resourceGroupName, jobName, this.innerModel(), Context.NONE);
        return this;
    }

    public JobResource create(Context context) {
        this.innerObject =
            serviceManager.serviceClient().getJobs().create(resourceGroupName, jobName, this.innerModel(), context);
        return this;
    }

    JobResourceImpl(String name, com.azure.resourcemanager.databox.DataBoxManager serviceManager) {
        this.innerObject = new JobResourceInner();
        this.serviceManager = serviceManager;
        this.jobName = name;
    }

    public JobResourceImpl update() {
        this.updateIfMatch = null;
        this.updateJobResourceUpdateParameter = new JobResourceUpdateParameter();
        return this;
    }

    public JobResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getJobs()
                .update(resourceGroupName, jobName, updateJobResourceUpdateParameter, updateIfMatch, Context.NONE);
        return this;
    }

    public JobResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getJobs()
                .update(resourceGroupName, jobName, updateJobResourceUpdateParameter, updateIfMatch, context);
        return this;
    }

    JobResourceImpl(JobResourceInner innerObject, com.azure.resourcemanager.databox.DataBoxManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.jobName = Utils.getValueFromIdByName(innerObject.id(), "jobs");
    }

    public JobResource refresh() {
        String localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getJobs()
                .getByResourceGroupWithResponse(resourceGroupName, jobName, localExpand, Context.NONE)
                .getValue();
        return this;
    }

    public JobResource refresh(Context context) {
        String localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getJobs()
                .getByResourceGroupWithResponse(resourceGroupName, jobName, localExpand, context)
                .getValue();
        return this;
    }

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

    public void markDevicesShipped(MarkDevicesShippedRequest markDevicesShippedRequest) {
        serviceManager.jobs().markDevicesShipped(jobName, resourceGroupName, markDevicesShippedRequest);
    }

    public Response<ShipmentPickUpResponse> bookShipmentPickUpWithResponse(
        ShipmentPickUpRequest shipmentPickUpRequest, Context context) {
        return serviceManager
            .jobs()
            .bookShipmentPickUpWithResponse(resourceGroupName, jobName, shipmentPickUpRequest, context);
    }

    public ShipmentPickUpResponse bookShipmentPickUp(ShipmentPickUpRequest shipmentPickUpRequest) {
        return serviceManager.jobs().bookShipmentPickUp(resourceGroupName, jobName, shipmentPickUpRequest);
    }

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

    public void cancel(CancellationReason cancellationReason) {
        serviceManager.jobs().cancel(resourceGroupName, jobName, cancellationReason);
    }

    public PagedIterable<UnencryptedCredentials> listCredentials() {
        return serviceManager.jobs().listCredentials(resourceGroupName, jobName);
    }

    public PagedIterable<UnencryptedCredentials> listCredentials(Context context) {
        return serviceManager.jobs().listCredentials(resourceGroupName, jobName, context);
    }

    public JobResourceImpl withRegion(Region location) {
        this.innerModel().withLocation(location.toString());
        return this;
    }

    public JobResourceImpl withRegion(String location) {
        this.innerModel().withLocation(location);
        return this;
    }

    public JobResourceImpl withSku(Sku sku) {
        this.innerModel().withSku(sku);
        return this;
    }

    public JobResourceImpl withTransferType(TransferType transferType) {
        this.innerModel().withTransferType(transferType);
        return this;
    }

    public JobResourceImpl withTags(Map<String, String> tags) {
        if (isInCreateMode()) {
            this.innerModel().withTags(tags);
            return this;
        } else {
            this.updateJobResourceUpdateParameter.withTags(tags);
            return this;
        }
    }

    public JobResourceImpl withIdentity(ResourceIdentity identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateJobResourceUpdateParameter.withIdentity(identity);
            return this;
        }
    }

    public JobResourceImpl withDetails(JobDetails details) {
        this.innerModel().withDetails(details);
        return this;
    }

    public JobResourceImpl withDeliveryType(JobDeliveryType deliveryType) {
        this.innerModel().withDeliveryType(deliveryType);
        return this;
    }

    public JobResourceImpl withDeliveryInfo(JobDeliveryInfo deliveryInfo) {
        this.innerModel().withDeliveryInfo(deliveryInfo);
        return this;
    }

    public JobResourceImpl withDetails(UpdateJobDetails details) {
        this.updateJobResourceUpdateParameter.withDetails(details);
        return this;
    }

    public JobResourceImpl withIfMatch(String ifMatch) {
        this.updateIfMatch = ifMatch;
        return this;
    }

    private boolean isInCreateMode() {
        return this.innerModel().id() == null;
    }
}
