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

package com.azure.resourcemanager.dataprotection.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.dataprotection.fluent.models.ResourceGuardResourceInner;
import com.azure.resourcemanager.dataprotection.models.PatchResourceGuardInput;
import com.azure.resourcemanager.dataprotection.models.ResourceGuard;
import com.azure.resourcemanager.dataprotection.models.ResourceGuardResource;
import java.util.Collections;
import java.util.Map;

public final class ResourceGuardResourceImpl
    implements ResourceGuardResource, ResourceGuardResource.Definition, ResourceGuardResource.Update {
    private ResourceGuardResourceInner innerObject;

    private final com.azure.resourcemanager.dataprotection.DataProtectionManager 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 String etag() {
        return this.innerModel().etag();
    }

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

    public ResourceGuard properties() {
        return this.innerModel().properties();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.dataprotection.DataProtectionManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String resourceGuardsName;

    private PatchResourceGuardInput updateParameters;

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

    public ResourceGuardResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceGuards()
                .putWithResponse(resourceGroupName, resourceGuardsName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public ResourceGuardResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceGuards()
                .putWithResponse(resourceGroupName, resourceGuardsName, this.innerModel(), context)
                .getValue();
        return this;
    }

    ResourceGuardResourceImpl(
        String name, com.azure.resourcemanager.dataprotection.DataProtectionManager serviceManager) {
        this.innerObject = new ResourceGuardResourceInner();
        this.serviceManager = serviceManager;
        this.resourceGuardsName = name;
    }

    public ResourceGuardResourceImpl update() {
        this.updateParameters = new PatchResourceGuardInput();
        return this;
    }

    public ResourceGuardResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceGuards()
                .patchWithResponse(resourceGroupName, resourceGuardsName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public ResourceGuardResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceGuards()
                .patchWithResponse(resourceGroupName, resourceGuardsName, updateParameters, context)
                .getValue();
        return this;
    }

    ResourceGuardResourceImpl(
        ResourceGuardResourceInner innerObject,
        com.azure.resourcemanager.dataprotection.DataProtectionManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.resourceGuardsName = Utils.getValueFromIdByName(innerObject.id(), "resourceGuards");
    }

    public ResourceGuardResource refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceGuards()
                .getByResourceGroupWithResponse(resourceGroupName, resourceGuardsName, Context.NONE)
                .getValue();
        return this;
    }

    public ResourceGuardResource refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceGuards()
                .getByResourceGroupWithResponse(resourceGroupName, resourceGuardsName, context)
                .getValue();
        return this;
    }

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

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

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

    public ResourceGuardResourceImpl withEtag(String etag) {
        this.innerModel().withEtag(etag);
        return this;
    }

    public ResourceGuardResourceImpl withProperties(ResourceGuard properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

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