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

package com.azure.resourcemanager.connectedvmware.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.connectedvmware.fluent.HostsClient;
import com.azure.resourcemanager.connectedvmware.fluent.models.HostModelInner;
import com.azure.resourcemanager.connectedvmware.models.HostModel;
import com.azure.resourcemanager.connectedvmware.models.Hosts;

public final class HostsImpl implements Hosts {
    private static final ClientLogger LOGGER = new ClientLogger(HostsImpl.class);

    private final HostsClient innerClient;

    private final com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager serviceManager;

    public HostsImpl(
        HostsClient innerClient, com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public HostModel getByResourceGroup(String resourceGroupName, String hostname) {
        HostModelInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, hostname);
        if (inner != null) {
            return new HostModelImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void delete(String resourceGroupName, String hostname, Boolean force) {
        this.serviceClient().delete(resourceGroupName, hostname, force);
    }

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

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

    public PagedIterable<HostModel> list() {
        PagedIterable<HostModelInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new HostModelImpl(inner1, this.manager()));
    }

    public PagedIterable<HostModel> list(Context context) {
        PagedIterable<HostModelInner> inner = this.serviceClient().list(context);
        return Utils.mapPage(inner, inner1 -> new HostModelImpl(inner1, this.manager()));
    }

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

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

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

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

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

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

    private com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager manager() {
        return this.serviceManager;
    }

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