// 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.EnvironmentTypesClient;
import com.azure.resourcemanager.devcenter.fluent.models.EnvironmentTypeInner;
import com.azure.resourcemanager.devcenter.models.EnvironmentType;
import com.azure.resourcemanager.devcenter.models.EnvironmentTypes;

public final class EnvironmentTypesImpl implements EnvironmentTypes {
    private static final ClientLogger LOGGER = new ClientLogger(EnvironmentTypesImpl.class);

    private final EnvironmentTypesClient innerClient;

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

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

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

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

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

    public EnvironmentType get(String resourceGroupName, String devCenterName, String environmentTypeName) {
        EnvironmentTypeInner inner = this.serviceClient().get(resourceGroupName, devCenterName, environmentTypeName);
        if (inner != null) {
            return new EnvironmentTypeImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Void> deleteWithResponse(
        String resourceGroupName, String devCenterName, String environmentTypeName, Context context) {
        return this.serviceClient().deleteWithResponse(resourceGroupName, devCenterName, environmentTypeName, context);
    }

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

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

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

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

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

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

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