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

package com.azure.resourcemanager.labservices.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.labservices.fluent.ImagesClient;
import com.azure.resourcemanager.labservices.fluent.models.ImageInner;
import com.azure.resourcemanager.labservices.models.Image;
import com.azure.resourcemanager.labservices.models.Images;

public final class ImagesImpl implements Images {
    private static final ClientLogger LOGGER = new ClientLogger(ImagesImpl.class);

    private final ImagesClient innerClient;

    private final com.azure.resourcemanager.labservices.LabServicesManager serviceManager;

    public ImagesImpl(
        ImagesClient innerClient, com.azure.resourcemanager.labservices.LabServicesManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Image> listByLabPlan(String resourceGroupName, String labPlanName) {
        PagedIterable<ImageInner> inner = this.serviceClient().listByLabPlan(resourceGroupName, labPlanName);
        return Utils.mapPage(inner, inner1 -> new ImageImpl(inner1, this.manager()));
    }

    public PagedIterable<Image> listByLabPlan(
        String resourceGroupName, String labPlanName, String filter, Context context) {
        PagedIterable<ImageInner> inner =
            this.serviceClient().listByLabPlan(resourceGroupName, labPlanName, filter, context);
        return Utils.mapPage(inner, inner1 -> new ImageImpl(inner1, this.manager()));
    }

    public Image get(String resourceGroupName, String labPlanName, String imageName) {
        ImageInner inner = this.serviceClient().get(resourceGroupName, labPlanName, imageName);
        if (inner != null) {
            return new ImageImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Image> getWithResponse(
        String resourceGroupName, String labPlanName, String imageName, Context context) {
        Response<ImageInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, labPlanName, imageName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ImageImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.labservices.LabServicesManager manager() {
        return this.serviceManager;
    }

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