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

package com.azure.resourcemanager.connectedvmware.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.connectedvmware.fluent.GuestAgentsClient;
import com.azure.resourcemanager.connectedvmware.fluent.models.GuestAgentInner;
import com.azure.resourcemanager.connectedvmware.models.GuestAgent;
import com.azure.resourcemanager.connectedvmware.models.GuestAgents;

public final class GuestAgentsImpl implements GuestAgents {
    private static final ClientLogger LOGGER = new ClientLogger(GuestAgentsImpl.class);

    private final GuestAgentsClient innerClient;

    private final com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager serviceManager;

    public GuestAgentsImpl(
        GuestAgentsClient innerClient,
        com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public GuestAgent get(String resourceGroupName, String virtualMachineName, String name) {
        GuestAgentInner inner = this.serviceClient().get(resourceGroupName, virtualMachineName, name);
        if (inner != null) {
            return new GuestAgentImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<GuestAgent> getWithResponse(
        String resourceGroupName, String virtualMachineName, String name, Context context) {
        Response<GuestAgentInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, virtualMachineName, name, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new GuestAgentImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String virtualMachineName, String name) {
        this.serviceClient().delete(resourceGroupName, virtualMachineName, name);
    }

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

    public PagedIterable<GuestAgent> listByVm(String resourceGroupName, String virtualMachineName) {
        PagedIterable<GuestAgentInner> inner = this.serviceClient().listByVm(resourceGroupName, virtualMachineName);
        return Utils.mapPage(inner, inner1 -> new GuestAgentImpl(inner1, this.manager()));
    }

    public PagedIterable<GuestAgent> listByVm(String resourceGroupName, String virtualMachineName, Context context) {
        PagedIterable<GuestAgentInner> inner =
            this.serviceClient().listByVm(resourceGroupName, virtualMachineName, context);
        return Utils.mapPage(inner, inner1 -> new GuestAgentImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager manager() {
        return this.serviceManager;
    }

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