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

package tsptest.armversioned.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 java.util.Collections;
import java.util.Map;
import tsptest.armversioned.fluent.models.TopLevelArmResourceInner;
import tsptest.armversioned.models.TopLevelArmResource;
import tsptest.armversioned.models.TopLevelArmResourceProperties;

public final class TopLevelArmResourceImpl
    implements TopLevelArmResource, TopLevelArmResource.Definition, TopLevelArmResource.Update {
    private TopLevelArmResourceInner innerObject;

    private final tsptest.armversioned.ArmVersionedManager 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 TopLevelArmResourceProperties 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 TopLevelArmResourceInner innerModel() {
        return this.innerObject;
    }

    private tsptest.armversioned.ArmVersionedManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String topLevelArmResourcePropertiesName;

    private String createParameter;

    private String createNewParameter;

    private String updateParameter;

    private String updateNewParameter;

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

    public TopLevelArmResource create() {
        this.innerObject = serviceManager.serviceClient()
            .getTopLevelArmResources()
            .createOrUpdate(resourceGroupName, topLevelArmResourcePropertiesName, this.innerModel(), createParameter,
                createNewParameter, Context.NONE);
        return this;
    }

    public TopLevelArmResource create(Context context) {
        this.innerObject = serviceManager.serviceClient()
            .getTopLevelArmResources()
            .createOrUpdate(resourceGroupName, topLevelArmResourcePropertiesName, this.innerModel(), createParameter,
                createNewParameter, context);
        return this;
    }

    TopLevelArmResourceImpl(String name, tsptest.armversioned.ArmVersionedManager serviceManager) {
        this.innerObject = new TopLevelArmResourceInner();
        this.serviceManager = serviceManager;
        this.topLevelArmResourcePropertiesName = name;
        this.createParameter = null;
        this.createNewParameter = null;
    }

    public TopLevelArmResourceImpl update() {
        this.updateParameter = null;
        this.updateNewParameter = null;
        return this;
    }

    public TopLevelArmResource apply() {
        this.innerObject = serviceManager.serviceClient()
            .getTopLevelArmResources()
            .createOrUpdate(resourceGroupName, topLevelArmResourcePropertiesName, this.innerModel(), updateParameter,
                updateNewParameter, Context.NONE);
        return this;
    }

    public TopLevelArmResource apply(Context context) {
        this.innerObject = serviceManager.serviceClient()
            .getTopLevelArmResources()
            .createOrUpdate(resourceGroupName, topLevelArmResourcePropertiesName, this.innerModel(), updateParameter,
                updateNewParameter, context);
        return this;
    }

    TopLevelArmResourceImpl(TopLevelArmResourceInner innerObject,
        tsptest.armversioned.ArmVersionedManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = ResourceManagerUtils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.topLevelArmResourcePropertiesName
            = ResourceManagerUtils.getValueFromIdByName(innerObject.id(), "topLevelArmResources");
    }

    public TopLevelArmResource refresh() {
        String localParameter = null;
        String localNewParameter = null;
        this.innerObject = serviceManager.serviceClient()
            .getTopLevelArmResources()
            .getByResourceGroupWithResponse(resourceGroupName, topLevelArmResourcePropertiesName, localParameter,
                localNewParameter, Context.NONE)
            .getValue();
        return this;
    }

    public TopLevelArmResource refresh(Context context) {
        String localParameter = null;
        String localNewParameter = null;
        this.innerObject = serviceManager.serviceClient()
            .getTopLevelArmResources()
            .getByResourceGroupWithResponse(resourceGroupName, topLevelArmResourcePropertiesName, localParameter,
                localNewParameter, context)
            .getValue();
        return this;
    }

    public Response<Void> actionWithResponse(String parameter, String newParameter, Context context) {
        return serviceManager.topLevelArmResources()
            .actionWithResponse(resourceGroupName, topLevelArmResourcePropertiesName, parameter, newParameter, context);
    }

    public Response<Void> actionWithResponse(String parameter, Context context) {
        return serviceManager.topLevelArmResources()
            .actionWithResponse(resourceGroupName, topLevelArmResourcePropertiesName, parameter, context);
    }

    public void action() {
        serviceManager.topLevelArmResources().action(resourceGroupName, topLevelArmResourcePropertiesName);
    }

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

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

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

    public TopLevelArmResourceImpl withProperties(TopLevelArmResourceProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public TopLevelArmResourceImpl withParameter(String parameter) {
        if (isInCreateMode()) {
            this.createParameter = parameter;
            return this;
        } else {
            this.updateParameter = parameter;
            return this;
        }
    }

    public TopLevelArmResourceImpl withNewParameter(String newParameter) {
        if (isInCreateMode()) {
            this.createNewParameter = newParameter;
            return this;
        } else {
            this.updateNewParameter = newParameter;
            return this;
        }
    }

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