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

package com.azure.resourcemanager.devtestlabs.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.devtestlabs.fluent.LabsClient;
import com.azure.resourcemanager.devtestlabs.fluent.models.GenerateUploadUriResponseInner;
import com.azure.resourcemanager.devtestlabs.fluent.models.LabInner;
import com.azure.resourcemanager.devtestlabs.fluent.models.LabVhdInner;
import com.azure.resourcemanager.devtestlabs.models.ExportResourceUsageParameters;
import com.azure.resourcemanager.devtestlabs.models.GenerateUploadUriParameter;
import com.azure.resourcemanager.devtestlabs.models.GenerateUploadUriResponse;
import com.azure.resourcemanager.devtestlabs.models.ImportLabVirtualMachineRequest;
import com.azure.resourcemanager.devtestlabs.models.Lab;
import com.azure.resourcemanager.devtestlabs.models.LabVhd;
import com.azure.resourcemanager.devtestlabs.models.LabVirtualMachineCreationParameter;
import com.azure.resourcemanager.devtestlabs.models.Labs;

public final class LabsImpl implements Labs {
    private static final ClientLogger LOGGER = new ClientLogger(LabsImpl.class);

    private final LabsClient innerClient;

    private final com.azure.resourcemanager.devtestlabs.DevTestLabsManager serviceManager;

    public LabsImpl(LabsClient innerClient, com.azure.resourcemanager.devtestlabs.DevTestLabsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Lab> list() {
        PagedIterable<LabInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new LabImpl(inner1, this.manager()));
    }

    public PagedIterable<Lab> list(String expand, String filter, Integer top, String orderby, Context context) {
        PagedIterable<LabInner> inner = this.serviceClient().list(expand, filter, top, orderby, context);
        return Utils.mapPage(inner, inner1 -> new LabImpl(inner1, this.manager()));
    }

    public PagedIterable<Lab> listByResourceGroup(String resourceGroupName) {
        PagedIterable<LabInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new LabImpl(inner1, this.manager()));
    }

    public PagedIterable<Lab> listByResourceGroup(
        String resourceGroupName, String expand, String filter, Integer top, String orderby, Context context) {
        PagedIterable<LabInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, expand, filter, top, orderby, context);
        return Utils.mapPage(inner, inner1 -> new LabImpl(inner1, this.manager()));
    }

    public Response<Lab> getByResourceGroupWithResponse(
        String resourceGroupName, String name, String expand, Context context) {
        Response<LabInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, name, expand, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new LabImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Lab getByResourceGroup(String resourceGroupName, String name) {
        LabInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, name);
        if (inner != null) {
            return new LabImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

    public void createEnvironment(
        String resourceGroupName, String name, LabVirtualMachineCreationParameter labVirtualMachineCreationParameter) {
        this.serviceClient().createEnvironment(resourceGroupName, name, labVirtualMachineCreationParameter);
    }

    public void createEnvironment(
        String resourceGroupName,
        String name,
        LabVirtualMachineCreationParameter labVirtualMachineCreationParameter,
        Context context) {
        this.serviceClient().createEnvironment(resourceGroupName, name, labVirtualMachineCreationParameter, context);
    }

    public void exportResourceUsage(
        String resourceGroupName, String name, ExportResourceUsageParameters exportResourceUsageParameters) {
        this.serviceClient().exportResourceUsage(resourceGroupName, name, exportResourceUsageParameters);
    }

    public void exportResourceUsage(
        String resourceGroupName,
        String name,
        ExportResourceUsageParameters exportResourceUsageParameters,
        Context context) {
        this.serviceClient().exportResourceUsage(resourceGroupName, name, exportResourceUsageParameters, context);
    }

    public Response<GenerateUploadUriResponse> generateUploadUriWithResponse(
        String resourceGroupName, String name, GenerateUploadUriParameter generateUploadUriParameter, Context context) {
        Response<GenerateUploadUriResponseInner> inner =
            this
                .serviceClient()
                .generateUploadUriWithResponse(resourceGroupName, name, generateUploadUriParameter, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new GenerateUploadUriResponseImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public GenerateUploadUriResponse generateUploadUri(
        String resourceGroupName, String name, GenerateUploadUriParameter generateUploadUriParameter) {
        GenerateUploadUriResponseInner inner =
            this.serviceClient().generateUploadUri(resourceGroupName, name, generateUploadUriParameter);
        if (inner != null) {
            return new GenerateUploadUriResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void importVirtualMachine(
        String resourceGroupName, String name, ImportLabVirtualMachineRequest importLabVirtualMachineRequest) {
        this.serviceClient().importVirtualMachine(resourceGroupName, name, importLabVirtualMachineRequest);
    }

    public void importVirtualMachine(
        String resourceGroupName,
        String name,
        ImportLabVirtualMachineRequest importLabVirtualMachineRequest,
        Context context) {
        this.serviceClient().importVirtualMachine(resourceGroupName, name, importLabVirtualMachineRequest, context);
    }

    public PagedIterable<LabVhd> listVhds(String resourceGroupName, String name) {
        PagedIterable<LabVhdInner> inner = this.serviceClient().listVhds(resourceGroupName, name);
        return Utils.mapPage(inner, inner1 -> new LabVhdImpl(inner1, this.manager()));
    }

    public PagedIterable<LabVhd> listVhds(String resourceGroupName, String name, Context context) {
        PagedIterable<LabVhdInner> inner = this.serviceClient().listVhds(resourceGroupName, name, context);
        return Utils.mapPage(inner, inner1 -> new LabVhdImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.devtestlabs.DevTestLabsManager manager() {
        return this.serviceManager;
    }

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