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

package com.azure.resourcemanager.devcenter.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.devcenter.fluent.DevBoxDefinitionsClient;
import com.azure.resourcemanager.devcenter.fluent.models.DevBoxDefinitionInner;
import com.azure.resourcemanager.devcenter.models.DevBoxDefinition;
import com.azure.resourcemanager.devcenter.models.DevBoxDefinitions;

public final class DevBoxDefinitionsImpl implements DevBoxDefinitions {
    private static final ClientLogger LOGGER = new ClientLogger(DevBoxDefinitionsImpl.class);

    private final DevBoxDefinitionsClient innerClient;

    private final com.azure.resourcemanager.devcenter.DevCenterManager serviceManager;

    public DevBoxDefinitionsImpl(
        DevBoxDefinitionsClient innerClient, com.azure.resourcemanager.devcenter.DevCenterManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<DevBoxDefinition> listByDevCenter(String resourceGroupName, String devCenterName) {
        PagedIterable<DevBoxDefinitionInner> inner =
            this.serviceClient().listByDevCenter(resourceGroupName, devCenterName);
        return Utils.mapPage(inner, inner1 -> new DevBoxDefinitionImpl(inner1, this.manager()));
    }

    public PagedIterable<DevBoxDefinition> listByDevCenter(
        String resourceGroupName, String devCenterName, Integer top, Context context) {
        PagedIterable<DevBoxDefinitionInner> inner =
            this.serviceClient().listByDevCenter(resourceGroupName, devCenterName, top, context);
        return Utils.mapPage(inner, inner1 -> new DevBoxDefinitionImpl(inner1, this.manager()));
    }

    public Response<DevBoxDefinition> getWithResponse(
        String resourceGroupName, String devCenterName, String devBoxDefinitionName, Context context) {
        Response<DevBoxDefinitionInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, devCenterName, devBoxDefinitionName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new DevBoxDefinitionImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public DevBoxDefinition get(String resourceGroupName, String devCenterName, String devBoxDefinitionName) {
        DevBoxDefinitionInner inner = this.serviceClient().get(resourceGroupName, devCenterName, devBoxDefinitionName);
        if (inner != null) {
            return new DevBoxDefinitionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String devCenterName, String devBoxDefinitionName) {
        this.serviceClient().delete(resourceGroupName, devCenterName, devBoxDefinitionName);
    }

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

    public PagedIterable<DevBoxDefinition> listByProject(String resourceGroupName, String projectName) {
        PagedIterable<DevBoxDefinitionInner> inner = this.serviceClient().listByProject(resourceGroupName, projectName);
        return Utils.mapPage(inner, inner1 -> new DevBoxDefinitionImpl(inner1, this.manager()));
    }

    public PagedIterable<DevBoxDefinition> listByProject(
        String resourceGroupName, String projectName, Integer top, Context context) {
        PagedIterable<DevBoxDefinitionInner> inner =
            this.serviceClient().listByProject(resourceGroupName, projectName, top, context);
        return Utils.mapPage(inner, inner1 -> new DevBoxDefinitionImpl(inner1, this.manager()));
    }

    public Response<DevBoxDefinition> getByProjectWithResponse(
        String resourceGroupName, String projectName, String devBoxDefinitionName, Context context) {
        Response<DevBoxDefinitionInner> inner =
            this
                .serviceClient()
                .getByProjectWithResponse(resourceGroupName, projectName, devBoxDefinitionName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new DevBoxDefinitionImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public DevBoxDefinition getByProject(String resourceGroupName, String projectName, String devBoxDefinitionName) {
        DevBoxDefinitionInner inner =
            this.serviceClient().getByProject(resourceGroupName, projectName, devBoxDefinitionName);
        if (inner != null) {
            return new DevBoxDefinitionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.devcenter.DevCenterManager manager() {
        return this.serviceManager;
    }

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