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

package com.azure.resourcemanager.workloads.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.workloads.fluent.models.SapVirtualInstanceInner;
import com.azure.resourcemanager.workloads.models.ManagedRGConfiguration;
import com.azure.resourcemanager.workloads.models.OperationStatusResult;
import com.azure.resourcemanager.workloads.models.SapConfiguration;
import com.azure.resourcemanager.workloads.models.SapEnvironmentType;
import com.azure.resourcemanager.workloads.models.SapHealthState;
import com.azure.resourcemanager.workloads.models.SapProductType;
import com.azure.resourcemanager.workloads.models.SapVirtualInstance;
import com.azure.resourcemanager.workloads.models.SapVirtualInstanceError;
import com.azure.resourcemanager.workloads.models.SapVirtualInstanceProvisioningState;
import com.azure.resourcemanager.workloads.models.SapVirtualInstanceState;
import com.azure.resourcemanager.workloads.models.SapVirtualInstanceStatus;
import com.azure.resourcemanager.workloads.models.StopRequest;
import com.azure.resourcemanager.workloads.models.UpdateSapVirtualInstanceRequest;
import com.azure.resourcemanager.workloads.models.UserAssignedServiceIdentity;
import java.util.Collections;
import java.util.Map;

public final class SapVirtualInstanceImpl
    implements SapVirtualInstance, SapVirtualInstance.Definition, SapVirtualInstance.Update {
    private SapVirtualInstanceInner innerObject;

    private final com.azure.resourcemanager.workloads.WorkloadsManager 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 UserAssignedServiceIdentity identity() {
        return this.innerModel().identity();
    }

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

    public SapEnvironmentType environment() {
        return this.innerModel().environment();
    }

    public SapProductType sapProduct() {
        return this.innerModel().sapProduct();
    }

    public SapConfiguration configuration() {
        return this.innerModel().configuration();
    }

    public ManagedRGConfiguration managedResourceGroupConfiguration() {
        return this.innerModel().managedResourceGroupConfiguration();
    }

    public SapVirtualInstanceStatus status() {
        return this.innerModel().status();
    }

    public SapHealthState health() {
        return this.innerModel().health();
    }

    public SapVirtualInstanceState state() {
        return this.innerModel().state();
    }

    public SapVirtualInstanceProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

    public SapVirtualInstanceError errors() {
        return this.innerModel().errors();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.workloads.WorkloadsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String sapVirtualInstanceName;

    private UpdateSapVirtualInstanceRequest updateBody;

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

    public SapVirtualInstance create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapVirtualInstances()
                .create(resourceGroupName, sapVirtualInstanceName, this.innerModel(), Context.NONE);
        return this;
    }

    public SapVirtualInstance create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapVirtualInstances()
                .create(resourceGroupName, sapVirtualInstanceName, this.innerModel(), context);
        return this;
    }

    SapVirtualInstanceImpl(String name, com.azure.resourcemanager.workloads.WorkloadsManager serviceManager) {
        this.innerObject = new SapVirtualInstanceInner();
        this.serviceManager = serviceManager;
        this.sapVirtualInstanceName = name;
    }

    public SapVirtualInstanceImpl update() {
        this.updateBody = new UpdateSapVirtualInstanceRequest();
        return this;
    }

    public SapVirtualInstance apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapVirtualInstances()
                .updateWithResponse(resourceGroupName, sapVirtualInstanceName, updateBody, Context.NONE)
                .getValue();
        return this;
    }

    public SapVirtualInstance apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapVirtualInstances()
                .updateWithResponse(resourceGroupName, sapVirtualInstanceName, updateBody, context)
                .getValue();
        return this;
    }

    SapVirtualInstanceImpl(
        SapVirtualInstanceInner innerObject, com.azure.resourcemanager.workloads.WorkloadsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.sapVirtualInstanceName = Utils.getValueFromIdByName(innerObject.id(), "sapVirtualInstances");
    }

    public SapVirtualInstance refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapVirtualInstances()
                .getByResourceGroupWithResponse(resourceGroupName, sapVirtualInstanceName, Context.NONE)
                .getValue();
        return this;
    }

    public SapVirtualInstance refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapVirtualInstances()
                .getByResourceGroupWithResponse(resourceGroupName, sapVirtualInstanceName, context)
                .getValue();
        return this;
    }

    public OperationStatusResult start() {
        return serviceManager.sapVirtualInstances().start(resourceGroupName, sapVirtualInstanceName);
    }

    public OperationStatusResult start(Context context) {
        return serviceManager.sapVirtualInstances().start(resourceGroupName, sapVirtualInstanceName, context);
    }

    public OperationStatusResult stop() {
        return serviceManager.sapVirtualInstances().stop(resourceGroupName, sapVirtualInstanceName);
    }

    public OperationStatusResult stop(StopRequest body, Context context) {
        return serviceManager.sapVirtualInstances().stop(resourceGroupName, sapVirtualInstanceName, body, context);
    }

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

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

    public SapVirtualInstanceImpl withEnvironment(SapEnvironmentType environment) {
        this.innerModel().withEnvironment(environment);
        return this;
    }

    public SapVirtualInstanceImpl withSapProduct(SapProductType sapProduct) {
        this.innerModel().withSapProduct(sapProduct);
        return this;
    }

    public SapVirtualInstanceImpl withConfiguration(SapConfiguration configuration) {
        this.innerModel().withConfiguration(configuration);
        return this;
    }

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

    public SapVirtualInstanceImpl withIdentity(UserAssignedServiceIdentity identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateBody.withIdentity(identity);
            return this;
        }
    }

    public SapVirtualInstanceImpl withManagedResourceGroupConfiguration(
        ManagedRGConfiguration managedResourceGroupConfiguration) {
        this.innerModel().withManagedResourceGroupConfiguration(managedResourceGroupConfiguration);
        return this;
    }

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