// 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.GitHubOwnersClient;
import com.azure.resourcemanager.securitydevops.fluent.models.GitHubOwnerInner;
import com.azure.resourcemanager.securitydevops.models.GitHubOwner;
import com.azure.resourcemanager.securitydevops.models.GitHubOwners;

public final class GitHubOwnersImpl implements GitHubOwners {
    private static final ClientLogger LOGGER = new ClientLogger(GitHubOwnersImpl.class);

    private final GitHubOwnersClient innerClient;

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

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

    public PagedIterable<GitHubOwner> list(String resourceGroupName, String gitHubConnectorName) {
        PagedIterable<GitHubOwnerInner> inner = this.serviceClient().list(resourceGroupName, gitHubConnectorName);
        return Utils.mapPage(inner, inner1 -> new GitHubOwnerImpl(inner1, this.manager()));
    }

    public PagedIterable<GitHubOwner> list(String resourceGroupName, String gitHubConnectorName, Context context) {
        PagedIterable<GitHubOwnerInner> inner =
            this.serviceClient().list(resourceGroupName, gitHubConnectorName, context);
        return Utils.mapPage(inner, inner1 -> new GitHubOwnerImpl(inner1, this.manager()));
    }

    public Response<GitHubOwner> getWithResponse(
        String resourceGroupName, String gitHubConnectorName, String gitHubOwnerName, Context context) {
        Response<GitHubOwnerInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, gitHubConnectorName, gitHubOwnerName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new GitHubOwnerImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public GitHubOwner get(String resourceGroupName, String gitHubConnectorName, String gitHubOwnerName) {
        GitHubOwnerInner inner = this.serviceClient().get(resourceGroupName, gitHubConnectorName, gitHubOwnerName);
        if (inner != null) {
            return new GitHubOwnerImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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