// 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.SapVirtualInstancesClient;
import com.azure.resourcemanager.workloads.fluent.models.OperationStatusResultInner;
import com.azure.resourcemanager.workloads.fluent.models.SapVirtualInstanceInner;
import com.azure.resourcemanager.workloads.models.OperationStatusResult;
import com.azure.resourcemanager.workloads.models.SapVirtualInstance;
import com.azure.resourcemanager.workloads.models.SapVirtualInstances;
import com.azure.resourcemanager.workloads.models.StopRequest;

public final class SapVirtualInstancesImpl implements SapVirtualInstances {
    private static final ClientLogger LOGGER = new ClientLogger(SapVirtualInstancesImpl.class);

    private final SapVirtualInstancesClient innerClient;

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

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

    public Response<SapVirtualInstance> getByResourceGroupWithResponse(
        String resourceGroupName, String sapVirtualInstanceName, Context context) {
        Response<SapVirtualInstanceInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, sapVirtualInstanceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new SapVirtualInstanceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public SapVirtualInstance getByResourceGroup(String resourceGroupName, String sapVirtualInstanceName) {
        SapVirtualInstanceInner inner =
            this.serviceClient().getByResourceGroup(resourceGroupName, sapVirtualInstanceName);
        if (inner != null) {
            return new SapVirtualInstanceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public PagedIterable<SapVirtualInstance> listByResourceGroup(String resourceGroupName) {
        PagedIterable<SapVirtualInstanceInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new SapVirtualInstanceImpl(inner1, this.manager()));
    }

    public PagedIterable<SapVirtualInstance> listByResourceGroup(String resourceGroupName, Context context) {
        PagedIterable<SapVirtualInstanceInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new SapVirtualInstanceImpl(inner1, this.manager()));
    }

    public PagedIterable<SapVirtualInstance> list() {
        PagedIterable<SapVirtualInstanceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new SapVirtualInstanceImpl(inner1, this.manager()));
    }

    public PagedIterable<SapVirtualInstance> list(Context context) {
        PagedIterable<SapVirtualInstanceInner> inner = this.serviceClient().list(context);
        return Utils.mapPage(inner, inner1 -> new SapVirtualInstanceImpl(inner1, this.manager()));
    }

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

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

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

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

    public SapVirtualInstance 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)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, sapVirtualInstanceName, Context.NONE).getValue();
    }

    public Response<SapVirtualInstance> 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)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, sapVirtualInstanceName, 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)));
        }
        return this.delete(resourceGroupName, sapVirtualInstanceName, 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)));
        }
        return this.delete(resourceGroupName, sapVirtualInstanceName, context);
    }

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

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

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