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

package com.azure.resourcemanager.devhub.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.devhub.fluent.WorkflowsClient;
import com.azure.resourcemanager.devhub.fluent.models.DeleteWorkflowResponseInner;
import com.azure.resourcemanager.devhub.fluent.models.WorkflowInner;
import com.azure.resourcemanager.devhub.models.DeleteWorkflowResponse;
import com.azure.resourcemanager.devhub.models.Workflow;
import com.azure.resourcemanager.devhub.models.Workflows;

public final class WorkflowsImpl implements Workflows {
    private static final ClientLogger LOGGER = new ClientLogger(WorkflowsImpl.class);

    private final WorkflowsClient innerClient;

    private final com.azure.resourcemanager.devhub.DevHubManager serviceManager;

    public WorkflowsImpl(WorkflowsClient innerClient, com.azure.resourcemanager.devhub.DevHubManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Workflow> list() {
        PagedIterable<WorkflowInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new WorkflowImpl(inner1, this.manager()));
    }

    public PagedIterable<Workflow> list(Context context) {
        PagedIterable<WorkflowInner> inner = this.serviceClient().list(context);
        return Utils.mapPage(inner, inner1 -> new WorkflowImpl(inner1, this.manager()));
    }

    public PagedIterable<Workflow> listByResourceGroup(String resourceGroupName) {
        PagedIterable<WorkflowInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new WorkflowImpl(inner1, this.manager()));
    }

    public PagedIterable<Workflow> listByResourceGroup(
        String resourceGroupName, String managedClusterResource, Context context) {
        PagedIterable<WorkflowInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, managedClusterResource, context);
        return Utils.mapPage(inner, inner1 -> new WorkflowImpl(inner1, this.manager()));
    }

    public Response<Workflow> getByResourceGroupWithResponse(
        String resourceGroupName, String workflowName, Context context) {
        Response<WorkflowInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, workflowName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new WorkflowImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Workflow getByResourceGroup(String resourceGroupName, String workflowName) {
        WorkflowInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, workflowName);
        if (inner != null) {
            return new WorkflowImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<DeleteWorkflowResponse> deleteByResourceGroupWithResponse(
        String resourceGroupName, String workflowName, Context context) {
        Response<DeleteWorkflowResponseInner> inner =
            this.serviceClient().deleteWithResponse(resourceGroupName, workflowName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new DeleteWorkflowResponseImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public DeleteWorkflowResponse deleteByResourceGroup(String resourceGroupName, String workflowName) {
        DeleteWorkflowResponseInner inner = this.serviceClient().delete(resourceGroupName, workflowName);
        if (inner != null) {
            return new DeleteWorkflowResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.devhub.DevHubManager manager() {
        return this.serviceManager;
    }

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