// 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.HybridRunbookWorkersClient;
import com.azure.resourcemanager.automation.fluent.models.HybridRunbookWorkerInner;
import com.azure.resourcemanager.automation.models.HybridRunbookWorker;
import com.azure.resourcemanager.automation.models.HybridRunbookWorkerMoveParameters;
import com.azure.resourcemanager.automation.models.HybridRunbookWorkers;

public final class HybridRunbookWorkersImpl implements HybridRunbookWorkers {
    private static final ClientLogger LOGGER = new ClientLogger(HybridRunbookWorkersImpl.class);

    private final HybridRunbookWorkersClient innerClient;

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

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

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

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

    public HybridRunbookWorker get(
        String resourceGroupName,
        String automationAccountName,
        String hybridRunbookWorkerGroupName,
        String hybridRunbookWorkerId) {
        HybridRunbookWorkerInner inner =
            this
                .serviceClient()
                .get(resourceGroupName, automationAccountName, hybridRunbookWorkerGroupName, hybridRunbookWorkerId);
        if (inner != null) {
            return new HybridRunbookWorkerImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void move(
        String resourceGroupName,
        String automationAccountName,
        String hybridRunbookWorkerGroupName,
        String hybridRunbookWorkerId,
        HybridRunbookWorkerMoveParameters hybridRunbookWorkerMoveParameters) {
        this
            .serviceClient()
            .move(
                resourceGroupName,
                automationAccountName,
                hybridRunbookWorkerGroupName,
                hybridRunbookWorkerId,
                hybridRunbookWorkerMoveParameters);
    }

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

    public PagedIterable<HybridRunbookWorker> listByHybridRunbookWorkerGroup(
        String resourceGroupName, String automationAccountName, String hybridRunbookWorkerGroupName) {
        PagedIterable<HybridRunbookWorkerInner> inner =
            this
                .serviceClient()
                .listByHybridRunbookWorkerGroup(resourceGroupName, automationAccountName, hybridRunbookWorkerGroupName);
        return Utils.mapPage(inner, inner1 -> new HybridRunbookWorkerImpl(inner1, this.manager()));
    }

    public PagedIterable<HybridRunbookWorker> listByHybridRunbookWorkerGroup(
        String resourceGroupName,
        String automationAccountName,
        String hybridRunbookWorkerGroupName,
        String filter,
        Context context) {
        PagedIterable<HybridRunbookWorkerInner> inner =
            this
                .serviceClient()
                .listByHybridRunbookWorkerGroup(
                    resourceGroupName, automationAccountName, hybridRunbookWorkerGroupName, filter, context);
        return Utils.mapPage(inner, inner1 -> new HybridRunbookWorkerImpl(inner1, this.manager()));
    }

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

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

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

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

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