// 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.AzureDevOpsOrgsClient;
import com.azure.resourcemanager.securitydevops.fluent.models.AzureDevOpsOrgInner;
import com.azure.resourcemanager.securitydevops.models.AzureDevOpsOrg;
import com.azure.resourcemanager.securitydevops.models.AzureDevOpsOrgs;

public final class AzureDevOpsOrgsImpl implements AzureDevOpsOrgs {
    private static final ClientLogger LOGGER = new ClientLogger(AzureDevOpsOrgsImpl.class);

    private final AzureDevOpsOrgsClient innerClient;

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

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

    public PagedIterable<AzureDevOpsOrg> list(String resourceGroupName, String azureDevOpsConnectorName) {
        PagedIterable<AzureDevOpsOrgInner> inner =
            this.serviceClient().list(resourceGroupName, azureDevOpsConnectorName);
        return Utils.mapPage(inner, inner1 -> new AzureDevOpsOrgImpl(inner1, this.manager()));
    }

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

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

    public AzureDevOpsOrg get(String resourceGroupName, String azureDevOpsConnectorName, String azureDevOpsOrgName) {
        AzureDevOpsOrgInner inner =
            this.serviceClient().get(resourceGroupName, azureDevOpsConnectorName, azureDevOpsOrgName);
        if (inner != null) {
            return new AzureDevOpsOrgImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public AzureDevOpsOrg 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)));
        }
        return this
            .getWithResponse(resourceGroupName, azureDevOpsConnectorName, azureDevOpsOrgName, Context.NONE)
            .getValue();
    }

    public Response<AzureDevOpsOrg> 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)));
        }
        return this.getWithResponse(resourceGroupName, azureDevOpsConnectorName, azureDevOpsOrgName, context);
    }

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

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

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