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

package com.azure.resourcemanager.security.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.security.fluent.ConnectorsClient;
import com.azure.resourcemanager.security.fluent.models.ConnectorSettingInner;
import com.azure.resourcemanager.security.models.ConnectorSetting;
import com.azure.resourcemanager.security.models.Connectors;

public final class ConnectorsImpl implements Connectors {
    private static final ClientLogger LOGGER = new ClientLogger(ConnectorsImpl.class);

    private final ConnectorsClient innerClient;

    private final com.azure.resourcemanager.security.SecurityManager serviceManager;

    public ConnectorsImpl(
        ConnectorsClient innerClient, com.azure.resourcemanager.security.SecurityManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<ConnectorSetting> list() {
        PagedIterable<ConnectorSettingInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new ConnectorSettingImpl(inner1, this.manager()));
    }

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

    public Response<ConnectorSetting> getWithResponse(String connectorName, Context context) {
        Response<ConnectorSettingInner> inner = this.serviceClient().getWithResponse(connectorName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ConnectorSettingImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ConnectorSetting get(String connectorName) {
        ConnectorSettingInner inner = this.serviceClient().get(connectorName);
        if (inner != null) {
            return new ConnectorSettingImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Void> deleteWithResponse(String connectorName, Context context) {
        return this.serviceClient().deleteWithResponse(connectorName, context);
    }

    public void delete(String connectorName) {
        this.serviceClient().delete(connectorName);
    }

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

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

    public void deleteById(String id) {
        String connectorName = Utils.getValueFromIdByName(id, "connectors");
        if (connectorName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'connectors'.", id)));
        }
        this.deleteWithResponse(connectorName, Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(String id, Context context) {
        String connectorName = Utils.getValueFromIdByName(id, "connectors");
        if (connectorName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'connectors'.", id)));
        }
        return this.deleteWithResponse(connectorName, context);
    }

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

    private com.azure.resourcemanager.security.SecurityManager manager() {
        return this.serviceManager;
    }

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