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

package com.azure.resourcemanager.networkcloud.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.networkcloud.fluent.ConsolesClient;
import com.azure.resourcemanager.networkcloud.fluent.models.ConsoleInner;
import com.azure.resourcemanager.networkcloud.models.Console;
import com.azure.resourcemanager.networkcloud.models.Consoles;

public final class ConsolesImpl implements Consoles {
    private static final ClientLogger LOGGER = new ClientLogger(ConsolesImpl.class);

    private final ConsolesClient innerClient;

    private final com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager;

    public ConsolesImpl(
        ConsolesClient innerClient, com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Console> listByVirtualMachine(String resourceGroupName, String virtualMachineName) {
        PagedIterable<ConsoleInner> inner =
            this.serviceClient().listByVirtualMachine(resourceGroupName, virtualMachineName);
        return Utils.mapPage(inner, inner1 -> new ConsoleImpl(inner1, this.manager()));
    }

    public PagedIterable<Console> listByVirtualMachine(
        String resourceGroupName, String virtualMachineName, Context context) {
        PagedIterable<ConsoleInner> inner =
            this.serviceClient().listByVirtualMachine(resourceGroupName, virtualMachineName, context);
        return Utils.mapPage(inner, inner1 -> new ConsoleImpl(inner1, this.manager()));
    }

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

    public Console get(String resourceGroupName, String virtualMachineName, String consoleName) {
        ConsoleInner inner = this.serviceClient().get(resourceGroupName, virtualMachineName, consoleName);
        if (inner != null) {
            return new ConsoleImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.networkcloud.NetworkCloudManager manager() {
        return this.serviceManager;
    }

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