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

package com.azure.resourcemanager.fluidrelay.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.fluidrelay.fluent.FluidRelayServersClient;
import com.azure.resourcemanager.fluidrelay.fluent.models.FluidRelayServerInner;
import com.azure.resourcemanager.fluidrelay.fluent.models.FluidRelayServerKeysInner;
import com.azure.resourcemanager.fluidrelay.models.FluidRelayServer;
import com.azure.resourcemanager.fluidrelay.models.FluidRelayServerKeys;
import com.azure.resourcemanager.fluidrelay.models.FluidRelayServers;
import com.azure.resourcemanager.fluidrelay.models.RegenerateKeyRequest;

public final class FluidRelayServersImpl implements FluidRelayServers {
    private static final ClientLogger LOGGER = new ClientLogger(FluidRelayServersImpl.class);

    private final FluidRelayServersClient innerClient;

    private final com.azure.resourcemanager.fluidrelay.FluidRelayManager serviceManager;

    public FluidRelayServersImpl(
        FluidRelayServersClient innerClient, com.azure.resourcemanager.fluidrelay.FluidRelayManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public FluidRelayServer getByResourceGroup(String resourceGroup, String fluidRelayServerName) {
        FluidRelayServerInner inner = this.serviceClient().getByResourceGroup(resourceGroup, fluidRelayServerName);
        if (inner != null) {
            return new FluidRelayServerImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<FluidRelayServer> getByResourceGroupWithResponse(
        String resourceGroup, String fluidRelayServerName, Context context) {
        Response<FluidRelayServerInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroup, fluidRelayServerName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new FluidRelayServerImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void deleteByResourceGroup(String resourceGroup, String fluidRelayServerName) {
        this.serviceClient().delete(resourceGroup, fluidRelayServerName);
    }

    public Response<Void> deleteWithResponse(String resourceGroup, String fluidRelayServerName, Context context) {
        return this.serviceClient().deleteWithResponse(resourceGroup, fluidRelayServerName, context);
    }

    public FluidRelayServerKeys regenerateKey(
        String resourceGroup, String fluidRelayServerName, RegenerateKeyRequest parameters) {
        FluidRelayServerKeysInner inner =
            this.serviceClient().regenerateKey(resourceGroup, fluidRelayServerName, parameters);
        if (inner != null) {
            return new FluidRelayServerKeysImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<FluidRelayServerKeys> regenerateKeyWithResponse(
        String resourceGroup, String fluidRelayServerName, RegenerateKeyRequest parameters, Context context) {
        Response<FluidRelayServerKeysInner> inner =
            this.serviceClient().regenerateKeyWithResponse(resourceGroup, fluidRelayServerName, parameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new FluidRelayServerKeysImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public FluidRelayServerKeys listKeys(String resourceGroup, String fluidRelayServerName) {
        FluidRelayServerKeysInner inner = this.serviceClient().listKeys(resourceGroup, fluidRelayServerName);
        if (inner != null) {
            return new FluidRelayServerKeysImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<FluidRelayServerKeys> listKeysWithResponse(
        String resourceGroup, String fluidRelayServerName, Context context) {
        Response<FluidRelayServerKeysInner> inner =
            this.serviceClient().listKeysWithResponse(resourceGroup, fluidRelayServerName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new FluidRelayServerKeysImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PagedIterable<FluidRelayServer> list() {
        PagedIterable<FluidRelayServerInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new FluidRelayServerImpl(inner1, this.manager()));
    }

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

    public PagedIterable<FluidRelayServer> listByResourceGroup(String resourceGroup) {
        PagedIterable<FluidRelayServerInner> inner = this.serviceClient().listByResourceGroup(resourceGroup);
        return Utils.mapPage(inner, inner1 -> new FluidRelayServerImpl(inner1, this.manager()));
    }

    public PagedIterable<FluidRelayServer> listByResourceGroup(String resourceGroup, Context context) {
        PagedIterable<FluidRelayServerInner> inner = this.serviceClient().listByResourceGroup(resourceGroup, context);
        return Utils.mapPage(inner, inner1 -> new FluidRelayServerImpl(inner1, this.manager()));
    }

    public FluidRelayServer getById(String id) {
        String resourceGroup = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroup == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String fluidRelayServerName = Utils.getValueFromIdByName(id, "fluidRelayServers");
        if (fluidRelayServerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'fluidRelayServers'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroup, fluidRelayServerName, Context.NONE).getValue();
    }

    public Response<FluidRelayServer> getByIdWithResponse(String id, Context context) {
        String resourceGroup = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroup == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String fluidRelayServerName = Utils.getValueFromIdByName(id, "fluidRelayServers");
        if (fluidRelayServerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'fluidRelayServers'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroup, fluidRelayServerName, context);
    }

    public void deleteById(String id) {
        String resourceGroup = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroup == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String fluidRelayServerName = Utils.getValueFromIdByName(id, "fluidRelayServers");
        if (fluidRelayServerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'fluidRelayServers'.", id)));
        }
        this.deleteWithResponse(resourceGroup, fluidRelayServerName, Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(String id, Context context) {
        String resourceGroup = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroup == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String fluidRelayServerName = Utils.getValueFromIdByName(id, "fluidRelayServers");
        if (fluidRelayServerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'fluidRelayServers'.", id)));
        }
        return this.deleteWithResponse(resourceGroup, fluidRelayServerName, context);
    }

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

    private com.azure.resourcemanager.fluidrelay.FluidRelayManager manager() {
        return this.serviceManager;
    }

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