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

package com.azure.resourcemanager.securityinsights.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.securityinsights.fluent.SourceControlsOperationsClient;
import com.azure.resourcemanager.securityinsights.fluent.models.SourceControlInner;
import com.azure.resourcemanager.securityinsights.models.SourceControl;
import com.azure.resourcemanager.securityinsights.models.SourceControlsOperations;

public final class SourceControlsOperationsImpl implements SourceControlsOperations {
    private static final ClientLogger LOGGER = new ClientLogger(SourceControlsOperationsImpl.class);

    private final SourceControlsOperationsClient innerClient;

    private final com.azure.resourcemanager.securityinsights.SecurityInsightsManager serviceManager;

    public SourceControlsOperationsImpl(
        SourceControlsOperationsClient innerClient,
        com.azure.resourcemanager.securityinsights.SecurityInsightsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<SourceControl> list(String resourceGroupName, String workspaceName) {
        PagedIterable<SourceControlInner> inner = this.serviceClient().list(resourceGroupName, workspaceName);
        return Utils.mapPage(inner, inner1 -> new SourceControlImpl(inner1, this.manager()));
    }

    public PagedIterable<SourceControl> list(String resourceGroupName, String workspaceName, Context context) {
        PagedIterable<SourceControlInner> inner = this.serviceClient().list(resourceGroupName, workspaceName, context);
        return Utils.mapPage(inner, inner1 -> new SourceControlImpl(inner1, this.manager()));
    }

    public SourceControl get(String resourceGroupName, String workspaceName, String sourceControlId) {
        SourceControlInner inner = this.serviceClient().get(resourceGroupName, workspaceName, sourceControlId);
        if (inner != null) {
            return new SourceControlImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

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

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

    private com.azure.resourcemanager.securityinsights.SecurityInsightsManager manager() {
        return this.serviceManager;
    }

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