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

package azure.resourcemanager.resources.implementation;

import azure.resourcemanager.resources.fluent.models.TopLevelTrackedResourceInner;
import azure.resourcemanager.resources.models.NotificationDetails;
import azure.resourcemanager.resources.models.TopLevelTrackedResource;
import azure.resourcemanager.resources.models.TopLevelTrackedResourceProperties;
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 java.util.Collections;
import java.util.Map;

public final class TopLevelTrackedResourceImpl
    implements TopLevelTrackedResource, TopLevelTrackedResource.Definition, TopLevelTrackedResource.Update {
    private TopLevelTrackedResourceInner innerObject;

    private final azure.resourcemanager.resources.ResourcesManager 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 TopLevelTrackedResourceProperties properties() {
        return this.innerModel().properties();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private azure.resourcemanager.resources.ResourcesManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String topLevelTrackedResourceName;

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

    public TopLevelTrackedResource create() {
        this.innerObject = serviceManager.serviceClient()
            .getTopLevels()
            .createOrReplace(resourceGroupName, topLevelTrackedResourceName, this.innerModel(), Context.NONE);
        return this;
    }

    public TopLevelTrackedResource create(Context context) {
        this.innerObject = serviceManager.serviceClient()
            .getTopLevels()
            .createOrReplace(resourceGroupName, topLevelTrackedResourceName, this.innerModel(), context);
        return this;
    }

    TopLevelTrackedResourceImpl(String name, azure.resourcemanager.resources.ResourcesManager serviceManager) {
        this.innerObject = new TopLevelTrackedResourceInner();
        this.serviceManager = serviceManager;
        this.topLevelTrackedResourceName = name;
    }

    public TopLevelTrackedResourceImpl update() {
        return this;
    }

    public TopLevelTrackedResource apply() {
        this.innerObject = serviceManager.serviceClient()
            .getTopLevels()
            .update(resourceGroupName, topLevelTrackedResourceName, this.innerModel(), Context.NONE);
        return this;
    }

    public TopLevelTrackedResource apply(Context context) {
        this.innerObject = serviceManager.serviceClient()
            .getTopLevels()
            .update(resourceGroupName, topLevelTrackedResourceName, this.innerModel(), context);
        return this;
    }

    TopLevelTrackedResourceImpl(TopLevelTrackedResourceInner innerObject,
        azure.resourcemanager.resources.ResourcesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = ResourceManagerUtils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.topLevelTrackedResourceName
            = ResourceManagerUtils.getValueFromIdByName(innerObject.id(), "topLevelTrackedResources");
    }

    public TopLevelTrackedResource refresh() {
        this.innerObject = serviceManager.serviceClient()
            .getTopLevels()
            .getByResourceGroupWithResponse(resourceGroupName, topLevelTrackedResourceName, Context.NONE)
            .getValue();
        return this;
    }

    public TopLevelTrackedResource refresh(Context context) {
        this.innerObject = serviceManager.serviceClient()
            .getTopLevels()
            .getByResourceGroupWithResponse(resourceGroupName, topLevelTrackedResourceName, context)
            .getValue();
        return this;
    }

    public Response<Void> actionSyncWithResponse(NotificationDetails body, Context context) {
        return serviceManager.topLevels()
            .actionSyncWithResponse(resourceGroupName, topLevelTrackedResourceName, body, context);
    }

    public void actionSync(NotificationDetails body) {
        serviceManager.topLevels().actionSync(resourceGroupName, topLevelTrackedResourceName, body);
    }

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

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

    public TopLevelTrackedResourceImpl withTags(Map<String, String> tags) {
        this.innerModel().withTags(tags);
        return this;
    }

    public TopLevelTrackedResourceImpl withProperties(TopLevelTrackedResourceProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }
}
