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

package com.azure.resourcemanager.automation.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.automation.fluent.HybridRunbookWorkerGroupsClient;
import com.azure.resourcemanager.automation.fluent.models.HybridRunbookWorkerGroupInner;
import com.azure.resourcemanager.automation.models.HybridRunbookWorkerGroup;
import com.azure.resourcemanager.automation.models.HybridRunbookWorkerGroups;

public final class HybridRunbookWorkerGroupsImpl implements HybridRunbookWorkerGroups {
    private static final ClientLogger LOGGER = new ClientLogger(HybridRunbookWorkerGroupsImpl.class);

    private final HybridRunbookWorkerGroupsClient innerClient;

    private final com.azure.resourcemanager.automation.AutomationManager serviceManager;

    public HybridRunbookWorkerGroupsImpl(
        HybridRunbookWorkerGroupsClient innerClient,
        com.azure.resourcemanager.automation.AutomationManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public void delete(String resourceGroupName, String automationAccountName, String hybridRunbookWorkerGroupName) {
        this.serviceClient().delete(resourceGroupName, automationAccountName, hybridRunbookWorkerGroupName);
    }

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

    public HybridRunbookWorkerGroup get(
        String resourceGroupName, String automationAccountName, String hybridRunbookWorkerGroupName) {
        HybridRunbookWorkerGroupInner inner =
            this.serviceClient().get(resourceGroupName, automationAccountName, hybridRunbookWorkerGroupName);
        if (inner != null) {
            return new HybridRunbookWorkerGroupImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<HybridRunbookWorkerGroup> getWithResponse(
        String resourceGroupName, String automationAccountName, String hybridRunbookWorkerGroupName, Context context) {
        Response<HybridRunbookWorkerGroupInner> inner =
            this
                .serviceClient()
                .getWithResponse(resourceGroupName, automationAccountName, hybridRunbookWorkerGroupName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new HybridRunbookWorkerGroupImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PagedIterable<HybridRunbookWorkerGroup> listByAutomationAccount(
        String resourceGroupName, String automationAccountName) {
        PagedIterable<HybridRunbookWorkerGroupInner> inner =
            this.serviceClient().listByAutomationAccount(resourceGroupName, automationAccountName);
        return Utils.mapPage(inner, inner1 -> new HybridRunbookWorkerGroupImpl(inner1, this.manager()));
    }

    public PagedIterable<HybridRunbookWorkerGroup> listByAutomationAccount(
        String resourceGroupName, String automationAccountName, String filter, Context context) {
        PagedIterable<HybridRunbookWorkerGroupInner> inner =
            this.serviceClient().listByAutomationAccount(resourceGroupName, automationAccountName, filter, context);
        return Utils.mapPage(inner, inner1 -> new HybridRunbookWorkerGroupImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.automation.AutomationManager manager() {
        return this.serviceManager;
    }

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