// 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.PoolsClient;
import com.azure.resourcemanager.devcenter.fluent.models.PoolInner;
import com.azure.resourcemanager.devcenter.models.Pool;
import com.azure.resourcemanager.devcenter.models.Pools;

public final class PoolsImpl implements Pools {
    private static final ClientLogger LOGGER = new ClientLogger(PoolsImpl.class);

    private final PoolsClient innerClient;

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

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

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

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

    public Response<Pool> getWithResponse(
        String resourceGroupName, String projectName, String poolName, Context context) {
        Response<PoolInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, projectName, poolName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new PoolImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Pool get(String resourceGroupName, String projectName, String poolName) {
        PoolInner inner = this.serviceClient().get(resourceGroupName, projectName, poolName);
        if (inner != null) {
            return new PoolImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String projectName, String poolName) {
        this.serviceClient().delete(resourceGroupName, projectName, poolName);
    }

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

    public void runHealthChecks(String resourceGroupName, String projectName, String poolName) {
        this.serviceClient().runHealthChecks(resourceGroupName, projectName, poolName);
    }

    public void runHealthChecks(String resourceGroupName, String projectName, String poolName, Context context) {
        this.serviceClient().runHealthChecks(resourceGroupName, projectName, poolName, context);
    }

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

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

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

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

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