// 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.SapCentralInstancesClient;
import com.azure.resourcemanager.workloads.fluent.models.OperationStatusResultInner;
import com.azure.resourcemanager.workloads.fluent.models.SapCentralServerInstanceInner;
import com.azure.resourcemanager.workloads.models.OperationStatusResult;
import com.azure.resourcemanager.workloads.models.SapCentralInstances;
import com.azure.resourcemanager.workloads.models.SapCentralServerInstance;
import com.azure.resourcemanager.workloads.models.StopRequest;

public final class SapCentralInstancesImpl implements SapCentralInstances {
    private static final ClientLogger LOGGER = new ClientLogger(SapCentralInstancesImpl.class);

    private final SapCentralInstancesClient innerClient;

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

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

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

    public SapCentralServerInstance get(
        String resourceGroupName, String sapVirtualInstanceName, String centralInstanceName) {
        SapCentralServerInstanceInner inner =
            this.serviceClient().get(resourceGroupName, sapVirtualInstanceName, centralInstanceName);
        if (inner != null) {
            return new SapCentralServerInstanceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

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

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

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

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

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

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

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

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