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

package tsptest.armresourceprovider.implementation;

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.armresourceprovider.fluent.models.ChildResourceInner;
import tsptest.armresourceprovider.models.ChildResource;
import tsptest.armresourceprovider.models.ChildResourceUpdate;
import tsptest.armresourceprovider.models.ProvisioningState;

public final class ChildResourceImpl implements ChildResource, ChildResource.Definition, ChildResource.Update {
    private ChildResourceInner innerObject;

    private final tsptest.armresourceprovider.ArmResourceProviderManager 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 ProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private tsptest.armresourceprovider.ArmResourceProviderManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String topLevelArmResourceName;

    private String childResourceName;

    private ChildResourceUpdate updateProperties;

    public ChildResourceImpl withExistingTopLevelArmResource(String resourceGroupName, String topLevelArmResourceName) {
        this.resourceGroupName = resourceGroupName;
        this.topLevelArmResourceName = topLevelArmResourceName;
        return this;
    }

    public ChildResource create() {
        this.innerObject = serviceManager.serviceClient()
            .getChildResourcesInterfaces()
            .createOrUpdate(resourceGroupName, topLevelArmResourceName, childResourceName, this.innerModel(),
                Context.NONE);
        return this;
    }

    public ChildResource create(Context context) {
        this.innerObject = serviceManager.serviceClient()
            .getChildResourcesInterfaces()
            .createOrUpdate(resourceGroupName, topLevelArmResourceName, childResourceName, this.innerModel(), context);
        return this;
    }

    ChildResourceImpl(String name, tsptest.armresourceprovider.ArmResourceProviderManager serviceManager) {
        this.innerObject = new ChildResourceInner();
        this.serviceManager = serviceManager;
        this.childResourceName = name;
    }

    public ChildResourceImpl update() {
        this.updateProperties = new ChildResourceUpdate();
        return this;
    }

    public ChildResource apply() {
        this.innerObject = serviceManager.serviceClient()
            .getChildResourcesInterfaces()
            .updateWithResponse(resourceGroupName, topLevelArmResourceName, childResourceName, updateProperties,
                Context.NONE)
            .getValue();
        return this;
    }

    public ChildResource apply(Context context) {
        this.innerObject = serviceManager.serviceClient()
            .getChildResourcesInterfaces()
            .updateWithResponse(resourceGroupName, topLevelArmResourceName, childResourceName, updateProperties,
                context)
            .getValue();
        return this;
    }

    ChildResourceImpl(ChildResourceInner innerObject,
        tsptest.armresourceprovider.ArmResourceProviderManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = ResourceManagerUtils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.topLevelArmResourceName
            = ResourceManagerUtils.getValueFromIdByName(innerObject.id(), "topLevelArmResources");
        this.childResourceName = ResourceManagerUtils.getValueFromIdByName(innerObject.id(), "childResources");
    }

    public ChildResource refresh() {
        this.innerObject = serviceManager.serviceClient()
            .getChildResourcesInterfaces()
            .getWithResponse(resourceGroupName, topLevelArmResourceName, childResourceName, Context.NONE)
            .getValue();
        return this;
    }

    public ChildResource refresh(Context context) {
        this.innerObject = serviceManager.serviceClient()
            .getChildResourcesInterfaces()
            .getWithResponse(resourceGroupName, topLevelArmResourceName, childResourceName, context)
            .getValue();
        return this;
    }

    public void actionWithoutBody() {
        serviceManager.childResourcesInterfaces()
            .actionWithoutBody(resourceGroupName, topLevelArmResourceName, childResourceName);
    }

    public void actionWithoutBody(Context context) {
        serviceManager.childResourcesInterfaces()
            .actionWithoutBody(resourceGroupName, topLevelArmResourceName, childResourceName, context);
    }

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

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

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

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