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

package com.azure.resourcemanager.deploymentmanager.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.deploymentmanager.fluent.models.ServiceTopologyResourceInner;
import com.azure.resourcemanager.deploymentmanager.models.ServiceTopologyResource;
import java.util.Collections;
import java.util.Map;

public final class ServiceTopologyResourceImpl
    implements ServiceTopologyResource, ServiceTopologyResource.Definition, ServiceTopologyResource.Update {
    private ServiceTopologyResourceInner innerObject;

    private final com.azure.resourcemanager.deploymentmanager.DeploymentManager 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 artifactSourceId() {
        return this.innerModel().artifactSourceId();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.deploymentmanager.DeploymentManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String serviceTopologyName;

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

    public ServiceTopologyResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServiceTopologies()
                .createOrUpdateWithResponse(resourceGroupName, serviceTopologyName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public ServiceTopologyResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServiceTopologies()
                .createOrUpdateWithResponse(resourceGroupName, serviceTopologyName, this.innerModel(), context)
                .getValue();
        return this;
    }

    ServiceTopologyResourceImpl(
        String name, com.azure.resourcemanager.deploymentmanager.DeploymentManager serviceManager) {
        this.innerObject = new ServiceTopologyResourceInner();
        this.serviceManager = serviceManager;
        this.serviceTopologyName = name;
    }

    public ServiceTopologyResourceImpl update() {
        return this;
    }

    public ServiceTopologyResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServiceTopologies()
                .createOrUpdateWithResponse(resourceGroupName, serviceTopologyName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public ServiceTopologyResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServiceTopologies()
                .createOrUpdateWithResponse(resourceGroupName, serviceTopologyName, this.innerModel(), context)
                .getValue();
        return this;
    }

    ServiceTopologyResourceImpl(
        ServiceTopologyResourceInner innerObject,
        com.azure.resourcemanager.deploymentmanager.DeploymentManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.serviceTopologyName = Utils.getValueFromIdByName(innerObject.id(), "serviceTopologies");
    }

    public ServiceTopologyResource refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServiceTopologies()
                .getByResourceGroupWithResponse(resourceGroupName, serviceTopologyName, Context.NONE)
                .getValue();
        return this;
    }

    public ServiceTopologyResource refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServiceTopologies()
                .getByResourceGroupWithResponse(resourceGroupName, serviceTopologyName, context)
                .getValue();
        return this;
    }

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

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

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

    public ServiceTopologyResourceImpl withArtifactSourceId(String artifactSourceId) {
        this.innerModel().withArtifactSourceId(artifactSourceId);
        return this;
    }
}
