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

package com.azure.resourcemanager.desktopvirtualization.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.desktopvirtualization.fluent.HostPoolsClient;
import com.azure.resourcemanager.desktopvirtualization.fluent.models.HostPoolInner;
import com.azure.resourcemanager.desktopvirtualization.fluent.models.RegistrationInfoInner;
import com.azure.resourcemanager.desktopvirtualization.models.HostPool;
import com.azure.resourcemanager.desktopvirtualization.models.HostPools;
import com.azure.resourcemanager.desktopvirtualization.models.RegistrationInfo;

public final class HostPoolsImpl implements HostPools {
    private static final ClientLogger LOGGER = new ClientLogger(HostPoolsImpl.class);

    private final HostPoolsClient innerClient;

    private final com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager serviceManager;

    public HostPoolsImpl(
        HostPoolsClient innerClient,
        com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

    public HostPool getByResourceGroup(String resourceGroupName, String hostPoolName) {
        HostPoolInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, hostPoolName);
        if (inner != null) {
            return new HostPoolImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public PagedIterable<HostPool> listByResourceGroup(
        String resourceGroupName, Integer pageSize, Boolean isDescending, Integer initialSkip, Context context) {
        PagedIterable<HostPoolInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, pageSize, isDescending, initialSkip, context);
        return Utils.mapPage(inner, inner1 -> new HostPoolImpl(inner1, this.manager()));
    }

    public PagedIterable<HostPool> list() {
        PagedIterable<HostPoolInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new HostPoolImpl(inner1, this.manager()));
    }

    public PagedIterable<HostPool> list(Integer pageSize, Boolean isDescending, Integer initialSkip, Context context) {
        PagedIterable<HostPoolInner> inner = this.serviceClient().list(pageSize, isDescending, initialSkip, context);
        return Utils.mapPage(inner, inner1 -> new HostPoolImpl(inner1, this.manager()));
    }

    public Response<RegistrationInfo> retrieveRegistrationTokenWithResponse(
        String resourceGroupName, String hostPoolName, Context context) {
        Response<RegistrationInfoInner> inner =
            this.serviceClient().retrieveRegistrationTokenWithResponse(resourceGroupName, hostPoolName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RegistrationInfoImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public RegistrationInfo retrieveRegistrationToken(String resourceGroupName, String hostPoolName) {
        RegistrationInfoInner inner = this.serviceClient().retrieveRegistrationToken(resourceGroupName, hostPoolName);
        if (inner != null) {
            return new RegistrationInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

    private com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager manager() {
        return this.serviceManager;
    }

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