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

package com.azure.resourcemanager.securitydevops.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.securitydevops.fluent.AzureDevOpsProjectsClient;
import com.azure.resourcemanager.securitydevops.fluent.models.AzureDevOpsProjectInner;
import com.azure.resourcemanager.securitydevops.models.AzureDevOpsProject;
import com.azure.resourcemanager.securitydevops.models.AzureDevOpsProjects;

public final class AzureDevOpsProjectsImpl implements AzureDevOpsProjects {
    private static final ClientLogger LOGGER = new ClientLogger(AzureDevOpsProjectsImpl.class);

    private final AzureDevOpsProjectsClient innerClient;

    private final com.azure.resourcemanager.securitydevops.SecurityDevOpsManager serviceManager;

    public AzureDevOpsProjectsImpl(
        AzureDevOpsProjectsClient innerClient,
        com.azure.resourcemanager.securitydevops.SecurityDevOpsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<AzureDevOpsProject> list(
        String resourceGroupName, String azureDevOpsConnectorName, String azureDevOpsOrgName) {
        PagedIterable<AzureDevOpsProjectInner> inner =
            this.serviceClient().list(resourceGroupName, azureDevOpsConnectorName, azureDevOpsOrgName);
        return Utils.mapPage(inner, inner1 -> new AzureDevOpsProjectImpl(inner1, this.manager()));
    }

    public PagedIterable<AzureDevOpsProject> list(
        String resourceGroupName, String azureDevOpsConnectorName, String azureDevOpsOrgName, Context context) {
        PagedIterable<AzureDevOpsProjectInner> inner =
            this.serviceClient().list(resourceGroupName, azureDevOpsConnectorName, azureDevOpsOrgName, context);
        return Utils.mapPage(inner, inner1 -> new AzureDevOpsProjectImpl(inner1, this.manager()));
    }

    public Response<AzureDevOpsProject> getWithResponse(
        String resourceGroupName,
        String azureDevOpsConnectorName,
        String azureDevOpsOrgName,
        String azureDevOpsProjectName,
        Context context) {
        Response<AzureDevOpsProjectInner> inner =
            this
                .serviceClient()
                .getWithResponse(
                    resourceGroupName, azureDevOpsConnectorName, azureDevOpsOrgName, azureDevOpsProjectName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AzureDevOpsProjectImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public AzureDevOpsProject get(
        String resourceGroupName,
        String azureDevOpsConnectorName,
        String azureDevOpsOrgName,
        String azureDevOpsProjectName) {
        AzureDevOpsProjectInner inner =
            this
                .serviceClient()
                .get(resourceGroupName, azureDevOpsConnectorName, azureDevOpsOrgName, azureDevOpsProjectName);
        if (inner != null) {
            return new AzureDevOpsProjectImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.securitydevops.SecurityDevOpsManager manager() {
        return this.serviceManager;
    }

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