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

package com.azure.resourcemanager.hybridcompute.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.hybridcompute.fluent.MachineExtensionsClient;
import com.azure.resourcemanager.hybridcompute.fluent.models.MachineExtensionInner;
import com.azure.resourcemanager.hybridcompute.models.MachineExtension;
import com.azure.resourcemanager.hybridcompute.models.MachineExtensions;

public final class MachineExtensionsImpl implements MachineExtensions {
    private static final ClientLogger LOGGER = new ClientLogger(MachineExtensionsImpl.class);

    private final MachineExtensionsClient innerClient;

    private final com.azure.resourcemanager.hybridcompute.HybridComputeManager serviceManager;

    public MachineExtensionsImpl(
        MachineExtensionsClient innerClient,
        com.azure.resourcemanager.hybridcompute.HybridComputeManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public void delete(String resourceGroupName, String machineName, String extensionName) {
        this.serviceClient().delete(resourceGroupName, machineName, extensionName);
    }

    public void delete(String resourceGroupName, String machineName, String extensionName, Context context) {
        this.serviceClient().delete(resourceGroupName, machineName, extensionName, context);
    }

    public Response<MachineExtension> getWithResponse(
        String resourceGroupName, String machineName, String extensionName, Context context) {
        Response<MachineExtensionInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, machineName, extensionName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new MachineExtensionImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public MachineExtension get(String resourceGroupName, String machineName, String extensionName) {
        MachineExtensionInner inner = this.serviceClient().get(resourceGroupName, machineName, extensionName);
        if (inner != null) {
            return new MachineExtensionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<MachineExtension> list(String resourceGroupName, String machineName) {
        PagedIterable<MachineExtensionInner> inner = this.serviceClient().list(resourceGroupName, machineName);
        return Utils.mapPage(inner, inner1 -> new MachineExtensionImpl(inner1, this.manager()));
    }

    public PagedIterable<MachineExtension> list(
        String resourceGroupName, String machineName, String expand, Context context) {
        PagedIterable<MachineExtensionInner> inner =
            this.serviceClient().list(resourceGroupName, machineName, expand, context);
        return Utils.mapPage(inner, inner1 -> new MachineExtensionImpl(inner1, this.manager()));
    }

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

    public Response<MachineExtension> 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 machineName = Utils.getValueFromIdByName(id, "machines");
        if (machineName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'machines'.", id)));
        }
        String extensionName = Utils.getValueFromIdByName(id, "extensions");
        if (extensionName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'extensions'.", id)));
        }
        return this.getWithResponse(resourceGroupName, machineName, extensionName, 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 machineName = Utils.getValueFromIdByName(id, "machines");
        if (machineName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'machines'.", id)));
        }
        String extensionName = Utils.getValueFromIdByName(id, "extensions");
        if (extensionName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'extensions'.", id)));
        }
        this.delete(resourceGroupName, machineName, extensionName, Context.NONE);
    }

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

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

    private com.azure.resourcemanager.hybridcompute.HybridComputeManager manager() {
        return this.serviceManager;
    }

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