// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.healthcareapis.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.healthcareapis.fluent.IotConnectorsClient;
import com.azure.resourcemanager.healthcareapis.fluent.models.IotConnectorInner;
import com.azure.resourcemanager.healthcareapis.models.IotConnector;
import com.azure.resourcemanager.healthcareapis.models.IotConnectors;

public final class IotConnectorsImpl implements IotConnectors {
    private static final ClientLogger LOGGER = new ClientLogger(IotConnectorsImpl.class);

    private final IotConnectorsClient innerClient;

    private final com.azure.resourcemanager.healthcareapis.HealthcareApisManager serviceManager;

    public IotConnectorsImpl(
        IotConnectorsClient innerClient,
        com.azure.resourcemanager.healthcareapis.HealthcareApisManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<IotConnector> listByWorkspace(String resourceGroupName, String workspaceName) {
        PagedIterable<IotConnectorInner> inner = this.serviceClient().listByWorkspace(resourceGroupName, workspaceName);
        return Utils.mapPage(inner, inner1 -> new IotConnectorImpl(inner1, this.manager()));
    }

    public PagedIterable<IotConnector> listByWorkspace(
        String resourceGroupName, String workspaceName, Context context) {
        PagedIterable<IotConnectorInner> inner =
            this.serviceClient().listByWorkspace(resourceGroupName, workspaceName, context);
        return Utils.mapPage(inner, inner1 -> new IotConnectorImpl(inner1, this.manager()));
    }

    public IotConnector get(String resourceGroupName, String workspaceName, String iotConnectorName) {
        IotConnectorInner inner = this.serviceClient().get(resourceGroupName, workspaceName, iotConnectorName);
        if (inner != null) {
            return new IotConnectorImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<IotConnector> getWithResponse(
        String resourceGroupName, String workspaceName, String iotConnectorName, Context context) {
        Response<IotConnectorInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, workspaceName, iotConnectorName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new IotConnectorImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String iotConnectorName, String workspaceName) {
        this.serviceClient().delete(resourceGroupName, iotConnectorName, workspaceName);
    }

    public void delete(String resourceGroupName, String iotConnectorName, String workspaceName, Context context) {
        this.serviceClient().delete(resourceGroupName, iotConnectorName, workspaceName, context);
    }

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

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

    public void 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 iotConnectorName = Utils.getValueFromIdByName(id, "iotconnectors");
        if (iotConnectorName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'iotconnectors'.", id)));
        }
        String workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        this.delete(resourceGroupName, iotConnectorName, workspaceName, Context.NONE);
    }

    public void 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 iotConnectorName = Utils.getValueFromIdByName(id, "iotconnectors");
        if (iotConnectorName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'iotconnectors'.", id)));
        }
        String workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        this.delete(resourceGroupName, iotConnectorName, workspaceName, context);
    }

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

    private com.azure.resourcemanager.healthcareapis.HealthcareApisManager manager() {
        return this.serviceManager;
    }

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