// 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.LabPlansClient;
import com.azure.resourcemanager.labservices.fluent.models.LabPlanInner;
import com.azure.resourcemanager.labservices.models.LabPlan;
import com.azure.resourcemanager.labservices.models.LabPlans;
import com.azure.resourcemanager.labservices.models.SaveImageBody;

public final class LabPlansImpl implements LabPlans {
    private static final ClientLogger LOGGER = new ClientLogger(LabPlansImpl.class);

    private final LabPlansClient innerClient;

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

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

    public PagedIterable<LabPlan> list() {
        PagedIterable<LabPlanInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new LabPlanImpl(inner1, this.manager()));
    }

    public PagedIterable<LabPlan> list(String filter, Context context) {
        PagedIterable<LabPlanInner> inner = this.serviceClient().list(filter, context);
        return Utils.mapPage(inner, inner1 -> new LabPlanImpl(inner1, this.manager()));
    }

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

    public PagedIterable<LabPlan> listByResourceGroup(String resourceGroupName, Context context) {
        PagedIterable<LabPlanInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new LabPlanImpl(inner1, this.manager()));
    }

    public LabPlan getByResourceGroup(String resourceGroupName, String labPlanName) {
        LabPlanInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, labPlanName);
        if (inner != null) {
            return new LabPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<LabPlan> getByResourceGroupWithResponse(
        String resourceGroupName, String labPlanName, Context context) {
        Response<LabPlanInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, labPlanName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new LabPlanImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

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

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

    public void saveImage(String resourceGroupName, String labPlanName, SaveImageBody body) {
        this.serviceClient().saveImage(resourceGroupName, labPlanName, body);
    }

    public void saveImage(String resourceGroupName, String labPlanName, SaveImageBody body, Context context) {
        this.serviceClient().saveImage(resourceGroupName, labPlanName, body, context);
    }

    public LabPlan 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)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, labPlanName, Context.NONE).getValue();
    }

    public Response<LabPlan> 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)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, labPlanName, 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 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)));
        }
        this.delete(resourceGroupName, labPlanName, 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 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)));
        }
        this.delete(resourceGroupName, labPlanName, context);
    }

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

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

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