// 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.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.edgeorder.fluent.models.AddressResourceInner;
import com.azure.resourcemanager.edgeorder.models.AddressResource;
import com.azure.resourcemanager.edgeorder.models.AddressUpdateParameter;
import com.azure.resourcemanager.edgeorder.models.AddressValidationStatus;
import com.azure.resourcemanager.edgeorder.models.ContactDetails;
import com.azure.resourcemanager.edgeorder.models.ShippingAddress;
import java.util.Collections;
import java.util.Map;

public final class AddressResourceImpl implements AddressResource, AddressResource.Definition, AddressResource.Update {
    private AddressResourceInner 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 ShippingAddress shippingAddress() {
        return this.innerModel().shippingAddress();
    }

    public ContactDetails contactDetails() {
        return this.innerModel().contactDetails();
    }

    public AddressValidationStatus addressValidationStatus() {
        return this.innerModel().addressValidationStatus();
    }

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

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

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

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

    private String addressName;

    private String resourceGroupName;

    private String updateIfMatch;

    private AddressUpdateParameter updateAddressUpdateParameter;

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

    public AddressResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .createAddress(addressName, resourceGroupName, this.innerModel(), Context.NONE);
        return this;
    }

    public AddressResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .createAddress(addressName, resourceGroupName, this.innerModel(), context);
        return this;
    }

    AddressResourceImpl(String name, com.azure.resourcemanager.edgeorder.EdgeOrderManager serviceManager) {
        this.innerObject = new AddressResourceInner();
        this.serviceManager = serviceManager;
        this.addressName = name;
    }

    public AddressResourceImpl update() {
        this.updateIfMatch = null;
        this.updateAddressUpdateParameter = new AddressUpdateParameter();
        return this;
    }

    public AddressResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .updateAddress(
                    addressName, resourceGroupName, updateAddressUpdateParameter, updateIfMatch, Context.NONE);
        return this;
    }

    public AddressResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .updateAddress(addressName, resourceGroupName, updateAddressUpdateParameter, updateIfMatch, context);
        return this;
    }

    AddressResourceImpl(
        AddressResourceInner innerObject, com.azure.resourcemanager.edgeorder.EdgeOrderManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.addressName = Utils.getValueFromIdByName(innerObject.id(), "addresses");
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
    }

    public AddressResource refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .getByResourceGroupWithResponse(resourceGroupName, addressName, Context.NONE)
                .getValue();
        return this;
    }

    public AddressResource refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceProviders()
                .getByResourceGroupWithResponse(resourceGroupName, addressName, context)
                .getValue();
        return this;
    }

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

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

    public AddressResourceImpl withContactDetails(ContactDetails contactDetails) {
        if (isInCreateMode()) {
            this.innerModel().withContactDetails(contactDetails);
            return this;
        } else {
            this.updateAddressUpdateParameter.withContactDetails(contactDetails);
            return this;
        }
    }

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

    public AddressResourceImpl withShippingAddress(ShippingAddress shippingAddress) {
        if (isInCreateMode()) {
            this.innerModel().withShippingAddress(shippingAddress);
            return this;
        } else {
            this.updateAddressUpdateParameter.withShippingAddress(shippingAddress);
            return this;
        }
    }

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

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