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

package com.azure.resourcemanager.dynatrace.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.dynatrace.fluent.SingleSignOnsClient;
import com.azure.resourcemanager.dynatrace.fluent.models.DynatraceSingleSignOnResourceInner;
import com.azure.resourcemanager.dynatrace.models.DynatraceSingleSignOnResource;
import com.azure.resourcemanager.dynatrace.models.SingleSignOns;

public final class SingleSignOnsImpl implements SingleSignOns {
    private static final ClientLogger LOGGER = new ClientLogger(SingleSignOnsImpl.class);

    private final SingleSignOnsClient innerClient;

    private final com.azure.resourcemanager.dynatrace.DynatraceManager serviceManager;

    public SingleSignOnsImpl(
        SingleSignOnsClient innerClient, com.azure.resourcemanager.dynatrace.DynatraceManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public DynatraceSingleSignOnResource get(String resourceGroupName, String monitorName, String configurationName) {
        DynatraceSingleSignOnResourceInner inner =
            this.serviceClient().get(resourceGroupName, monitorName, configurationName);
        if (inner != null) {
            return new DynatraceSingleSignOnResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<DynatraceSingleSignOnResource> getWithResponse(
        String resourceGroupName, String monitorName, String configurationName, Context context) {
        Response<DynatraceSingleSignOnResourceInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, monitorName, configurationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new DynatraceSingleSignOnResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PagedIterable<DynatraceSingleSignOnResource> list(String resourceGroupName, String monitorName) {
        PagedIterable<DynatraceSingleSignOnResourceInner> inner =
            this.serviceClient().list(resourceGroupName, monitorName);
        return Utils.mapPage(inner, inner1 -> new DynatraceSingleSignOnResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<DynatraceSingleSignOnResource> list(
        String resourceGroupName, String monitorName, Context context) {
        PagedIterable<DynatraceSingleSignOnResourceInner> inner =
            this.serviceClient().list(resourceGroupName, monitorName, context);
        return Utils.mapPage(inner, inner1 -> new DynatraceSingleSignOnResourceImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.dynatrace.DynatraceManager manager() {
        return this.serviceManager;
    }

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