// 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.SapApplicationServerInstanceInner;
import com.azure.resourcemanager.workloads.models.ApplicationServerVmDetails;
import com.azure.resourcemanager.workloads.models.LoadBalancerDetails;
import com.azure.resourcemanager.workloads.models.OperationStatusResult;
import com.azure.resourcemanager.workloads.models.SapApplicationServerInstance;
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.UpdateSapApplicationInstanceRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class SapApplicationServerInstanceImpl
    implements SapApplicationServerInstance,
        SapApplicationServerInstance.Definition,
        SapApplicationServerInstance.Update {
    private SapApplicationServerInstanceInner 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 String hostname() {
        return this.innerModel().hostname();
    }

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

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

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

    public Long gatewayPort() {
        return this.innerModel().gatewayPort();
    }

    public Long icmHttpPort() {
        return this.innerModel().icmHttpPort();
    }

    public Long icmHttpsPort() {
        return this.innerModel().icmHttpsPort();
    }

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

    public List<ApplicationServerVmDetails> vmDetails() {
        List<ApplicationServerVmDetails> 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 SapApplicationServerInstanceInner innerModel() {
        return this.innerObject;
    }

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

    private String resourceGroupName;

    private String sapVirtualInstanceName;

    private String applicationInstanceName;

    private UpdateSapApplicationInstanceRequest updateBody;

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

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

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

    SapApplicationServerInstanceImpl(String name, com.azure.resourcemanager.workloads.WorkloadsManager serviceManager) {
        this.innerObject = new SapApplicationServerInstanceInner();
        this.serviceManager = serviceManager;
        this.applicationInstanceName = name;
    }

    public SapApplicationServerInstanceImpl update() {
        this.updateBody = new UpdateSapApplicationInstanceRequest();
        return this;
    }

    public SapApplicationServerInstance apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapApplicationServerInstances()
                .update(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, updateBody, Context.NONE);
        return this;
    }

    public SapApplicationServerInstance apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapApplicationServerInstances()
                .update(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, updateBody, context);
        return this;
    }

    SapApplicationServerInstanceImpl(
        SapApplicationServerInstanceInner 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.applicationInstanceName = Utils.getValueFromIdByName(innerObject.id(), "applicationInstances");
    }

    public SapApplicationServerInstance refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapApplicationServerInstances()
                .getWithResponse(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, Context.NONE)
                .getValue();
        return this;
    }

    public SapApplicationServerInstance refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSapApplicationServerInstances()
                .getWithResponse(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, context)
                .getValue();
        return this;
    }

    public OperationStatusResult startInstance() {
        return serviceManager
            .sapApplicationServerInstances()
            .startInstance(resourceGroupName, sapVirtualInstanceName, applicationInstanceName);
    }

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

    public OperationStatusResult stopInstance() {
        return serviceManager
            .sapApplicationServerInstances()
            .stopInstance(resourceGroupName, sapVirtualInstanceName, applicationInstanceName);
    }

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

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

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

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

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