// 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.SapDatabaseInstancesClient;
import com.azure.resourcemanager.workloads.fluent.models.OperationStatusResultInner;
import com.azure.resourcemanager.workloads.fluent.models.SapDatabaseInstanceInner;
import com.azure.resourcemanager.workloads.models.OperationStatusResult;
import com.azure.resourcemanager.workloads.models.SapDatabaseInstance;
import com.azure.resourcemanager.workloads.models.SapDatabaseInstances;
import com.azure.resourcemanager.workloads.models.StopRequest;

public final class SapDatabaseInstancesImpl implements SapDatabaseInstances {
    private static final ClientLogger LOGGER = new ClientLogger(SapDatabaseInstancesImpl.class);

    private final SapDatabaseInstancesClient innerClient;

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

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

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

    public SapDatabaseInstance get(
        String resourceGroupName, String sapVirtualInstanceName, String databaseInstanceName) {
        SapDatabaseInstanceInner inner =
            this.serviceClient().get(resourceGroupName, sapVirtualInstanceName, databaseInstanceName);
        if (inner != null) {
            return new SapDatabaseInstanceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

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

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

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

    public SapDatabaseInstance 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 databaseInstanceName = Utils.getValueFromIdByName(id, "databaseInstances");
        if (databaseInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'databaseInstances'.", id)));
        }
        return this
            .getWithResponse(resourceGroupName, sapVirtualInstanceName, databaseInstanceName, Context.NONE)
            .getValue();
    }

    public Response<SapDatabaseInstance> 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 databaseInstanceName = Utils.getValueFromIdByName(id, "databaseInstances");
        if (databaseInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'databaseInstances'.", id)));
        }
        return this.getWithResponse(resourceGroupName, sapVirtualInstanceName, databaseInstanceName, 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 databaseInstanceName = Utils.getValueFromIdByName(id, "databaseInstances");
        if (databaseInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'databaseInstances'.", id)));
        }
        return this.delete(resourceGroupName, sapVirtualInstanceName, databaseInstanceName, 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 databaseInstanceName = Utils.getValueFromIdByName(id, "databaseInstances");
        if (databaseInstanceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'databaseInstances'.", id)));
        }
        return this.delete(resourceGroupName, sapVirtualInstanceName, databaseInstanceName, context);
    }

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

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

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