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

package com.azure.resourcemanager.videoanalyzer.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.videoanalyzer.fluent.PipelineTopologiesClient;
import com.azure.resourcemanager.videoanalyzer.fluent.models.PipelineTopologyInner;
import com.azure.resourcemanager.videoanalyzer.models.PipelineTopologies;
import com.azure.resourcemanager.videoanalyzer.models.PipelineTopology;

public final class PipelineTopologiesImpl implements PipelineTopologies {
    private static final ClientLogger LOGGER = new ClientLogger(PipelineTopologiesImpl.class);

    private final PipelineTopologiesClient innerClient;

    private final com.azure.resourcemanager.videoanalyzer.VideoAnalyzerManager serviceManager;

    public PipelineTopologiesImpl(
        PipelineTopologiesClient innerClient,
        com.azure.resourcemanager.videoanalyzer.VideoAnalyzerManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<PipelineTopology> list(String resourceGroupName, String accountName) {
        PagedIterable<PipelineTopologyInner> inner = this.serviceClient().list(resourceGroupName, accountName);
        return Utils.mapPage(inner, inner1 -> new PipelineTopologyImpl(inner1, this.manager()));
    }

    public PagedIterable<PipelineTopology> list(
        String resourceGroupName, String accountName, String filter, Integer top, Context context) {
        PagedIterable<PipelineTopologyInner> inner =
            this.serviceClient().list(resourceGroupName, accountName, filter, top, context);
        return Utils.mapPage(inner, inner1 -> new PipelineTopologyImpl(inner1, this.manager()));
    }

    public PipelineTopology get(String resourceGroupName, String accountName, String pipelineTopologyName) {
        PipelineTopologyInner inner = this.serviceClient().get(resourceGroupName, accountName, pipelineTopologyName);
        if (inner != null) {
            return new PipelineTopologyImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<PipelineTopology> getWithResponse(
        String resourceGroupName, String accountName, String pipelineTopologyName, Context context) {
        Response<PipelineTopologyInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, accountName, pipelineTopologyName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new PipelineTopologyImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String accountName, String pipelineTopologyName) {
        this.serviceClient().delete(resourceGroupName, accountName, pipelineTopologyName);
    }

    public Response<Void> deleteWithResponse(
        String resourceGroupName, String accountName, String pipelineTopologyName, Context context) {
        return this.serviceClient().deleteWithResponse(resourceGroupName, accountName, pipelineTopologyName, context);
    }

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

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

    public void deleteById(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 accountName = Utils.getValueFromIdByName(id, "videoAnalyzers");
        if (accountName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'videoAnalyzers'.", id)));
        }
        String pipelineTopologyName = Utils.getValueFromIdByName(id, "pipelineTopologies");
        if (pipelineTopologyName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'pipelineTopologies'.", id)));
        }
        this.deleteWithResponse(resourceGroupName, accountName, pipelineTopologyName, Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(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 accountName = Utils.getValueFromIdByName(id, "videoAnalyzers");
        if (accountName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'videoAnalyzers'.", id)));
        }
        String pipelineTopologyName = Utils.getValueFromIdByName(id, "pipelineTopologies");
        if (pipelineTopologyName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'pipelineTopologies'.", id)));
        }
        return this.deleteWithResponse(resourceGroupName, accountName, pipelineTopologyName, context);
    }

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

    private com.azure.resourcemanager.videoanalyzer.VideoAnalyzerManager manager() {
        return this.serviceManager;
    }

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