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

package com.azure.resourcemanager.scvmm.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.scvmm.fluent.InventoryItemsClient;
import com.azure.resourcemanager.scvmm.fluent.models.InventoryItemInner;
import com.azure.resourcemanager.scvmm.models.InventoryItem;
import com.azure.resourcemanager.scvmm.models.InventoryItems;

public final class InventoryItemsImpl implements InventoryItems {
    private static final ClientLogger LOGGER = new ClientLogger(InventoryItemsImpl.class);

    private final InventoryItemsClient innerClient;

    private final com.azure.resourcemanager.scvmm.ScvmmManager serviceManager;

    public InventoryItemsImpl(
        InventoryItemsClient innerClient, com.azure.resourcemanager.scvmm.ScvmmManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public InventoryItem get(String resourceGroupName, String vmmServerName, String inventoryItemName) {
        InventoryItemInner inner = this.serviceClient().get(resourceGroupName, vmmServerName, inventoryItemName);
        if (inner != null) {
            return new InventoryItemImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<InventoryItem> getWithResponse(
        String resourceGroupName, String vmmServerName, String inventoryItemName, Context context) {
        Response<InventoryItemInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, vmmServerName, inventoryItemName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new InventoryItemImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String vmmServerName, String inventoryItemName) {
        this.serviceClient().delete(resourceGroupName, vmmServerName, inventoryItemName);
    }

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

    public PagedIterable<InventoryItem> listByVmmServer(String resourceGroupName, String vmmServerName) {
        PagedIterable<InventoryItemInner> inner =
            this.serviceClient().listByVmmServer(resourceGroupName, vmmServerName);
        return Utils.mapPage(inner, inner1 -> new InventoryItemImpl(inner1, this.manager()));
    }

    public PagedIterable<InventoryItem> listByVmmServer(
        String resourceGroupName, String vmmServerName, Context context) {
        PagedIterable<InventoryItemInner> inner =
            this.serviceClient().listByVmmServer(resourceGroupName, vmmServerName, context);
        return Utils.mapPage(inner, inner1 -> new InventoryItemImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.scvmm.ScvmmManager manager() {
        return this.serviceManager;
    }

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