// 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.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.workloads.fluent.SapApplicationServerInstancesClient;
import com.azure.resourcemanager.workloads.fluent.models.OperationStatusResultInner;
import com.azure.resourcemanager.workloads.fluent.models.SapApplicationServerInstanceInner;
import com.azure.resourcemanager.workloads.models.OperationStatusResult;
import com.azure.resourcemanager.workloads.models.SapApplicationServerInstance;
import com.azure.resourcemanager.workloads.models.SapApplicationServerInstances;
import com.azure.resourcemanager.workloads.models.StopRequest;

public final class SapApplicationServerInstancesImpl implements SapApplicationServerInstances {
    private static final ClientLogger LOGGER = new ClientLogger(SapApplicationServerInstancesImpl.class);

    private final SapApplicationServerInstancesClient innerClient;

    private final com.azure.resourcemanager.workloads.WorkloadsManager serviceManager;

    public SapApplicationServerInstancesImpl(
        SapApplicationServerInstancesClient innerClient,
        com.azure.resourcemanager.workloads.WorkloadsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<SapApplicationServerInstance> getWithResponse(
        String resourceGroupName, String sapVirtualInstanceName, String applicationInstanceName, Context context) {
        Response<SapApplicationServerInstanceInner> inner =
            this
                .serviceClient()
                .getWithResponse(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new SapApplicationServerInstanceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public SapApplicationServerInstance get(
        String resourceGroupName, String sapVirtualInstanceName, String applicationInstanceName) {
        SapApplicationServerInstanceInner inner =
            this.serviceClient().get(resourceGroupName, sapVirtualInstanceName, applicationInstanceName);
        if (inner != null) {
            return new SapApplicationServerInstanceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationStatusResult delete(
        String resourceGroupName, String sapVirtualInstanceName, String applicationInstanceName) {
        OperationStatusResultInner inner =
            this.serviceClient().delete(resourceGroupName, sapVirtualInstanceName, applicationInstanceName);
        if (inner != null) {
            return new OperationStatusResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationStatusResult delete(
        String resourceGroupName, String sapVirtualInstanceName, String applicationInstanceName, Context context) {
        OperationStatusResultInner inner =
            this.serviceClient().delete(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, context);
        if (inner != null) {
            return new OperationStatusResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<SapApplicationServerInstance> list(String resourceGroupName, String sapVirtualInstanceName) {
        PagedIterable<SapApplicationServerInstanceInner> inner =
            this.serviceClient().list(resourceGroupName, sapVirtualInstanceName);
        return Utils.mapPage(inner, inner1 -> new SapApplicationServerInstanceImpl(inner1, this.manager()));
    }

    public PagedIterable<SapApplicationServerInstance> list(
        String resourceGroupName, String sapVirtualInstanceName, Context context) {
        PagedIterable<SapApplicationServerInstanceInner> inner =
            this.serviceClient().list(resourceGroupName, sapVirtualInstanceName, context);
        return Utils.mapPage(inner, inner1 -> new SapApplicationServerInstanceImpl(inner1, this.manager()));
    }

    public OperationStatusResult startInstance(
        String resourceGroupName, String sapVirtualInstanceName, String applicationInstanceName) {
        OperationStatusResultInner inner =
            this.serviceClient().startInstance(resourceGroupName, sapVirtualInstanceName, applicationInstanceName);
        if (inner != null) {
            return new OperationStatusResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationStatusResult startInstance(
        String resourceGroupName, String sapVirtualInstanceName, String applicationInstanceName, Context context) {
        OperationStatusResultInner inner =
            this
                .serviceClient()
                .startInstance(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, context);
        if (inner != null) {
            return new OperationStatusResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationStatusResult stopInstance(
        String resourceGroupName, String sapVirtualInstanceName, String applicationInstanceName) {
        OperationStatusResultInner inner =
            this.serviceClient().stopInstance(resourceGroupName, sapVirtualInstanceName, applicationInstanceName);
        if (inner != null) {
            return new OperationStatusResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationStatusResult stopInstance(
        String resourceGroupName,
        String sapVirtualInstanceName,
        String applicationInstanceName,
        StopRequest body,
        Context context) {
        OperationStatusResultInner inner =
            this
                .serviceClient()
                .stopInstance(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, body, context);
        if (inner != null) {
            return new OperationStatusResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public SapApplicationServerInstance getById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String sapVirtualInstanceName = Utils.getValueFromIdByName(id, "sapVirtualInstances");
        if (sapVirtualInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'sapVirtualInstances'.", id)));
        }
        String applicationInstanceName = Utils.getValueFromIdByName(id, "applicationInstances");
        if (applicationInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'applicationInstances'.",
                                id)));
        }
        return this
            .getWithResponse(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, Context.NONE)
            .getValue();
    }

    public Response<SapApplicationServerInstance> getByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String sapVirtualInstanceName = Utils.getValueFromIdByName(id, "sapVirtualInstances");
        if (sapVirtualInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'sapVirtualInstances'.", id)));
        }
        String applicationInstanceName = Utils.getValueFromIdByName(id, "applicationInstances");
        if (applicationInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'applicationInstances'.",
                                id)));
        }
        return this.getWithResponse(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, context);
    }

    public OperationStatusResult deleteById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String sapVirtualInstanceName = Utils.getValueFromIdByName(id, "sapVirtualInstances");
        if (sapVirtualInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'sapVirtualInstances'.", id)));
        }
        String applicationInstanceName = Utils.getValueFromIdByName(id, "applicationInstances");
        if (applicationInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'applicationInstances'.",
                                id)));
        }
        return this.delete(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, Context.NONE);
    }

    public OperationStatusResult deleteByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String sapVirtualInstanceName = Utils.getValueFromIdByName(id, "sapVirtualInstances");
        if (sapVirtualInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'sapVirtualInstances'.", id)));
        }
        String applicationInstanceName = Utils.getValueFromIdByName(id, "applicationInstances");
        if (applicationInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'applicationInstances'.",
                                id)));
        }
        return this.delete(resourceGroupName, sapVirtualInstanceName, applicationInstanceName, context);
    }

    private SapApplicationServerInstancesClient serviceClient() {
        return this.innerClient;
    }

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

    public SapApplicationServerInstanceImpl define(String name) {
        return new SapApplicationServerInstanceImpl(name, this.manager());
    }
}
