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

package com.azure.resourcemanager.edgeorder.implementation;

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.edgeorder.fluent.models.AddressProperties;
import com.azure.resourcemanager.edgeorder.fluent.models.OrderItemResourceInner;
import com.azure.resourcemanager.edgeorder.models.AddressDetails;
import com.azure.resourcemanager.edgeorder.models.CancellationReason;
import com.azure.resourcemanager.edgeorder.models.OrderItemDetails;
import com.azure.resourcemanager.edgeorder.models.OrderItemResource;
import com.azure.resourcemanager.edgeorder.models.OrderItemUpdateParameter;
import com.azure.resourcemanager.edgeorder.models.Preferences;
import com.azure.resourcemanager.edgeorder.models.ReturnOrderItemDetails;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class OrderItemResourceImpl
    implements OrderItemResource, OrderItemResource.Definition, OrderItemResource.Update {
    private OrderItemResourceInner innerObject;

    private final com.azure.resourcemanager.edgeorder.EdgeOrderManager serviceManager;

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

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

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

    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 SystemData systemData() {
        return this.innerModel().systemData();
    }

    public OrderItemDetails orderItemDetails() {
        return this.innerModel().orderItemDetails();
    }

    public AddressDetails addressDetails() {
        return this.innerModel().addressDetails();
    }

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

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

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

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

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

    private com.azure.resourcemanager.edgeorder.EdgeOrderManager manager() {
        return this.serviceManager;
    }

    private String orderItemName;

    private String resourceGroupName;

    private String updateIfMatch;

    private OrderItemUpdateParameter updateOrderItemUpdateParameter;

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

    public OrderItemResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .createOrderItem(orderItemName, resourceGroupName, this.innerModel(), Context.NONE);
        return this;
    }

    public OrderItemResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .createOrderItem(orderItemName, resourceGroupName, this.innerModel(), context);
        return this;
    }

    OrderItemResourceImpl(String name, com.azure.resourcemanager.edgeorder.EdgeOrderManager serviceManager) {
        this.innerObject = new OrderItemResourceInner();
        this.serviceManager = serviceManager;
        this.orderItemName = name;
    }

    public OrderItemResourceImpl update() {
        this.updateIfMatch = null;
        this.updateOrderItemUpdateParameter = new OrderItemUpdateParameter();
        return this;
    }

    public OrderItemResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .updateOrderItem(
                    orderItemName, resourceGroupName, updateOrderItemUpdateParameter, updateIfMatch, Context.NONE);
        return this;
    }

    public OrderItemResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .updateOrderItem(
                    orderItemName, resourceGroupName, updateOrderItemUpdateParameter, updateIfMatch, context);
        return this;
    }

    OrderItemResourceImpl(
        OrderItemResourceInner innerObject, com.azure.resourcemanager.edgeorder.EdgeOrderManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.orderItemName = Utils.getValueFromIdByName(innerObject.id(), "orderItems");
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
    }

    public OrderItemResource refresh() {
        String localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .getOrderItemByNameWithResponse(orderItemName, resourceGroupName, localExpand, Context.NONE)
                .getValue();
        return this;
    }

    public OrderItemResource refresh(Context context) {
        String localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .getOrderItemByNameWithResponse(orderItemName, resourceGroupName, localExpand, context)
                .getValue();
        return this;
    }

    public void cancelOrderItem(CancellationReason cancellationReason) {
        serviceManager.resourceProviders().cancelOrderItem(orderItemName, resourceGroupName, cancellationReason);
    }

    public Response<Void> cancelOrderItemWithResponse(CancellationReason cancellationReason, Context context) {
        return serviceManager
            .resourceProviders()
            .cancelOrderItemWithResponse(orderItemName, resourceGroupName, cancellationReason, context);
    }

    public void returnOrderItem(ReturnOrderItemDetails returnOrderItemDetails) {
        serviceManager.resourceProviders().returnOrderItem(orderItemName, resourceGroupName, returnOrderItemDetails);
    }

    public void returnOrderItem(ReturnOrderItemDetails returnOrderItemDetails, Context context) {
        serviceManager
            .resourceProviders()
            .returnOrderItem(orderItemName, resourceGroupName, returnOrderItemDetails, context);
    }

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

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

    public OrderItemResourceImpl withOrderItemDetails(OrderItemDetails orderItemDetails) {
        this.innerModel().withOrderItemDetails(orderItemDetails);
        return this;
    }

    public OrderItemResourceImpl withAddressDetails(AddressDetails addressDetails) {
        this.innerModel().withAddressDetails(addressDetails);
        return this;
    }

    public OrderItemResourceImpl withOrderId(String orderId) {
        this.innerModel().withOrderId(orderId);
        return this;
    }

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

    public OrderItemResourceImpl withForwardAddress(AddressProperties forwardAddress) {
        this.updateOrderItemUpdateParameter.withForwardAddress(forwardAddress);
        return this;
    }

    public OrderItemResourceImpl withPreferences(Preferences preferences) {
        this.updateOrderItemUpdateParameter.withPreferences(preferences);
        return this;
    }

    public OrderItemResourceImpl withNotificationEmailList(List<String> notificationEmailList) {
        this.updateOrderItemUpdateParameter.withNotificationEmailList(notificationEmailList);
        return this;
    }

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

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