// 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.GitHubRepoesClient;
import com.azure.resourcemanager.securitydevops.fluent.models.GitHubRepoInner;
import com.azure.resourcemanager.securitydevops.models.GitHubRepo;
import com.azure.resourcemanager.securitydevops.models.GitHubRepoes;

public final class GitHubRepoesImpl implements GitHubRepoes {
    private static final ClientLogger LOGGER = new ClientLogger(GitHubRepoesImpl.class);

    private final GitHubRepoesClient innerClient;

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

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

    public PagedIterable<GitHubRepo> listByConnector(String resourceGroupName, String gitHubConnectorName) {
        PagedIterable<GitHubRepoInner> inner =
            this.serviceClient().listByConnector(resourceGroupName, gitHubConnectorName);
        return Utils.mapPage(inner, inner1 -> new GitHubRepoImpl(inner1, this.manager()));
    }

    public PagedIterable<GitHubRepo> listByConnector(
        String resourceGroupName, String gitHubConnectorName, Context context) {
        PagedIterable<GitHubRepoInner> inner =
            this.serviceClient().listByConnector(resourceGroupName, gitHubConnectorName, context);
        return Utils.mapPage(inner, inner1 -> new GitHubRepoImpl(inner1, this.manager()));
    }

    public PagedIterable<GitHubRepo> list(
        String resourceGroupName, String gitHubConnectorName, String gitHubOwnerName) {
        PagedIterable<GitHubRepoInner> inner =
            this.serviceClient().list(resourceGroupName, gitHubConnectorName, gitHubOwnerName);
        return Utils.mapPage(inner, inner1 -> new GitHubRepoImpl(inner1, this.manager()));
    }

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

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

    public GitHubRepo get(
        String resourceGroupName, String gitHubConnectorName, String gitHubOwnerName, String gitHubRepoName) {
        GitHubRepoInner inner =
            this.serviceClient().get(resourceGroupName, gitHubConnectorName, gitHubOwnerName, gitHubRepoName);
        if (inner != null) {
            return new GitHubRepoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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