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

package com.azure.resourcemanager.notificationhubs.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.notificationhubs.fluent.models.NamespaceResourceInner;
import com.azure.resourcemanager.notificationhubs.models.NamespaceCreateOrUpdateParameters;
import com.azure.resourcemanager.notificationhubs.models.NamespacePatchParameters;
import com.azure.resourcemanager.notificationhubs.models.NamespaceResource;
import com.azure.resourcemanager.notificationhubs.models.NamespaceType;
import com.azure.resourcemanager.notificationhubs.models.Sku;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;

public final class NamespaceResourceImpl
    implements NamespaceResource, NamespaceResource.Definition, NamespaceResource.Update {
    private NamespaceResourceInner innerObject;

    private final com.azure.resourcemanager.notificationhubs.NotificationHubsManager 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 Sku sku() {
        return this.innerModel().sku();
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

    public NamespaceType namespaceType() {
        return this.innerModel().namespaceType();
    }

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

    private com.azure.resourcemanager.notificationhubs.NotificationHubsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String namespaceName;

    private NamespaceCreateOrUpdateParameters createParameters;

    private NamespacePatchParameters updateParameters;

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

    public NamespaceResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNamespaces()
                .createOrUpdateWithResponse(resourceGroupName, namespaceName, createParameters, Context.NONE)
                .getValue();
        return this;
    }

    public NamespaceResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNamespaces()
                .createOrUpdateWithResponse(resourceGroupName, namespaceName, createParameters, context)
                .getValue();
        return this;
    }

    NamespaceResourceImpl(
        String name, com.azure.resourcemanager.notificationhubs.NotificationHubsManager serviceManager) {
        this.innerObject = new NamespaceResourceInner();
        this.serviceManager = serviceManager;
        this.namespaceName = name;
        this.createParameters = new NamespaceCreateOrUpdateParameters();
    }

    public NamespaceResourceImpl update() {
        this.updateParameters = new NamespacePatchParameters();
        return this;
    }

    public NamespaceResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNamespaces()
                .patchWithResponse(resourceGroupName, namespaceName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public NamespaceResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNamespaces()
                .patchWithResponse(resourceGroupName, namespaceName, updateParameters, context)
                .getValue();
        return this;
    }

    NamespaceResourceImpl(
        NamespaceResourceInner innerObject,
        com.azure.resourcemanager.notificationhubs.NotificationHubsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.namespaceName = Utils.getValueFromIdByName(innerObject.id(), "namespaces");
    }

    public NamespaceResource refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNamespaces()
                .getByResourceGroupWithResponse(resourceGroupName, namespaceName, Context.NONE)
                .getValue();
        return this;
    }

    public NamespaceResource refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNamespaces()
                .getByResourceGroupWithResponse(resourceGroupName, namespaceName, context)
                .getValue();
        return this;
    }

    public NamespaceResourceImpl withLocation(Region location) {
        this.createParameters.withLocation(location.toString());
        return this;
    }

    public NamespaceResourceImpl withLocation(String location) {
        this.createParameters.withLocation(location);
        return this;
    }

    public NamespaceResourceImpl withTags(Map<String, String> tags) {
        if (isInCreateMode()) {
            this.createParameters.withTags(tags);
            return this;
        } else {
            this.updateParameters.withTags(tags);
            return this;
        }
    }

    public NamespaceResourceImpl withSku(Sku sku) {
        if (isInCreateMode()) {
            this.createParameters.withSku(sku);
            return this;
        } else {
            this.updateParameters.withSku(sku);
            return this;
        }
    }

    public NamespaceResourceImpl withNamePropertiesName(String namePropertiesName) {
        this.createParameters.withNamePropertiesName(namePropertiesName);
        return this;
    }

    public NamespaceResourceImpl withProvisioningState(String provisioningState) {
        this.createParameters.withProvisioningState(provisioningState);
        return this;
    }

    public NamespaceResourceImpl withRegion(String region) {
        this.createParameters.withRegion(region);
        return this;
    }

    public NamespaceResourceImpl withStatus(String status) {
        this.createParameters.withStatus(status);
        return this;
    }

    public NamespaceResourceImpl withCreatedAt(OffsetDateTime createdAt) {
        this.createParameters.withCreatedAt(createdAt);
        return this;
    }

    public NamespaceResourceImpl withUpdatedAt(OffsetDateTime updatedAt) {
        this.createParameters.withUpdatedAt(updatedAt);
        return this;
    }

    public NamespaceResourceImpl withServiceBusEndpoint(String serviceBusEndpoint) {
        this.createParameters.withServiceBusEndpoint(serviceBusEndpoint);
        return this;
    }

    public NamespaceResourceImpl withSubscriptionId(String subscriptionId) {
        this.createParameters.withSubscriptionId(subscriptionId);
        return this;
    }

    public NamespaceResourceImpl withScaleUnit(String scaleUnit) {
        this.createParameters.withScaleUnit(scaleUnit);
        return this;
    }

    public NamespaceResourceImpl withEnabled(Boolean enabled) {
        this.createParameters.withEnabled(enabled);
        return this;
    }

    public NamespaceResourceImpl withCritical(Boolean critical) {
        this.createParameters.withCritical(critical);
        return this;
    }

    public NamespaceResourceImpl withDataCenter(String dataCenter) {
        this.createParameters.withDataCenter(dataCenter);
        return this;
    }

    public NamespaceResourceImpl withNamespaceType(NamespaceType namespaceType) {
        this.createParameters.withNamespaceType(namespaceType);
        return this;
    }

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