// 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.SapCentralServerInstanceInner;
import com.azure.resourcemanager.workloads.models.CentralServerVmDetails;
import com.azure.resourcemanager.workloads.models.EnqueueReplicationServerProperties;
import com.azure.resourcemanager.workloads.models.EnqueueServerProperties;
import com.azure.resourcemanager.workloads.models.GatewayServerProperties;
import com.azure.resourcemanager.workloads.models.LoadBalancerDetails;
import com.azure.resourcemanager.workloads.models.MessageServerProperties;
import com.azure.resourcemanager.workloads.models.OperationStatusResult;
import com.azure.resourcemanager.workloads.models.SapCentralServerInstance;
import com.azure.resourcemanager.workloads.models.SapHealthState;
import com.azure.resourcemanager.workloads.models.SapVirtualInstanceError;
import com.azure.resourcemanager.workloads.models.SapVirtualInstanceProvisioningState;
import com.azure.resourcemanager.workloads.models.SapVirtualInstanceStatus;
import com.azure.resourcemanager.workloads.models.StopRequest;
import com.azure.resourcemanager.workloads.models.UpdateSapCentralInstanceRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class SapCentralServerInstanceImpl
    implements SapCentralServerInstance, SapCentralServerInstance.Definition, SapCentralServerInstance.Update {
    private SapCentralServerInstanceInner 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 SystemData systemData() {
        return this.innerModel().systemData();
    }

    public String instanceNo() {
        return this.innerModel().instanceNo();
    }

    public String subnet() {
        return this.innerModel().subnet();
    }

    public MessageServerProperties messageServerProperties() {
        return this.innerModel().messageServerProperties();
    }

    public EnqueueServerProperties enqueueServerProperties() {
        return this.innerModel().enqueueServerProperties();
    }

    public GatewayServerProperties gatewayServerProperties() {
        return this.innerModel().gatewayServerProperties();
    }

    public EnqueueReplicationServerProperties enqueueReplicationServerProperties() {
        return this.innerModel().enqueueReplicationServerProperties();
    }

    public String kernelVersion() {
        return this.innerModel().kernelVersion();
    }

    public String kernelPatch() {
        return this.innerModel().kernelPatch();
    }

    public LoadBalancerDetails loadBalancerDetails() {
        return this.innerModel().loadBalancerDetails();
    }

    public List<CentralServerVmDetails> vmDetails() {
        List<CentralServerVmDetails> inner = this.innerModel().vmDetails();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

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

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

    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 SapCentralServerInstanceInner innerModel() {
        return this.innerObject;
    }

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

    private String resourceGroupName;

    private String sapVirtualInstanceName;

    private String centralInstanceName;

    private UpdateSapCentralInstanceRequest updateBody;

    public SapCentralServerInstanceImpl withExistingSapVirtualInstance(
        String resourceGroupName, String sapVirtualInstanceName) {
        this.resourceGroupName = resourceGroupName;
        this.sapVirtualInstanceName = sapVirtualInstanceName;
        return this;
    }

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

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

    SapCentralServerInstanceImpl(String name, com.azure.resourcemanager.workloads.WorkloadsManager serviceManager) {
        this.innerObject = new SapCentralServerInstanceInner();
        this.serviceManager = serviceManager;
        this.centralInstanceName = name;
    }

    public SapCentralServerInstanceImpl update() {
        this.updateBody = new UpdateSapCentralInstanceRequest();
        return this;
    }

    public SapCentralServerInstance apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapCentralInstances()
                .update(resourceGroupName, sapVirtualInstanceName, centralInstanceName, updateBody, Context.NONE);
        return this;
    }

    public SapCentralServerInstance apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapCentralInstances()
                .update(resourceGroupName, sapVirtualInstanceName, centralInstanceName, updateBody, context);
        return this;
    }

    SapCentralServerInstanceImpl(
        SapCentralServerInstanceInner 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");
        this.centralInstanceName = Utils.getValueFromIdByName(innerObject.id(), "centralInstances");
    }

    public SapCentralServerInstance refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapCentralInstances()
                .getWithResponse(resourceGroupName, sapVirtualInstanceName, centralInstanceName, Context.NONE)
                .getValue();
        return this;
    }

    public SapCentralServerInstance refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapCentralInstances()
                .getWithResponse(resourceGroupName, sapVirtualInstanceName, centralInstanceName, context)
                .getValue();
        return this;
    }

    public OperationStatusResult startInstance() {
        return serviceManager
            .sapCentralInstances()
            .startInstance(resourceGroupName, sapVirtualInstanceName, centralInstanceName);
    }

    public OperationStatusResult startInstance(Context context) {
        return serviceManager
            .sapCentralInstances()
            .startInstance(resourceGroupName, sapVirtualInstanceName, centralInstanceName, context);
    }

    public OperationStatusResult stopInstance() {
        return serviceManager
            .sapCentralInstances()
            .stopInstance(resourceGroupName, sapVirtualInstanceName, centralInstanceName);
    }

    public OperationStatusResult stopInstance(StopRequest body, Context context) {
        return serviceManager
            .sapCentralInstances()
            .stopInstance(resourceGroupName, sapVirtualInstanceName, centralInstanceName, body, context);
    }

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

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

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

    public SapCentralServerInstanceImpl withMessageServerProperties(MessageServerProperties messageServerProperties) {
        this.innerModel().withMessageServerProperties(messageServerProperties);
        return this;
    }

    public SapCentralServerInstanceImpl withEnqueueServerProperties(EnqueueServerProperties enqueueServerProperties) {
        this.innerModel().withEnqueueServerProperties(enqueueServerProperties);
        return this;
    }

    public SapCentralServerInstanceImpl withGatewayServerProperties(GatewayServerProperties gatewayServerProperties) {
        this.innerModel().withGatewayServerProperties(gatewayServerProperties);
        return this;
    }

    public SapCentralServerInstanceImpl withEnqueueReplicationServerProperties(
        EnqueueReplicationServerProperties enqueueReplicationServerProperties) {
        this.innerModel().withEnqueueReplicationServerProperties(enqueueReplicationServerProperties);
        return this;
    }

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