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

package com.azure.resourcemanager.networkcloud.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.networkcloud.fluent.L3NetworksClient;
import com.azure.resourcemanager.networkcloud.fluent.models.L3NetworkInner;
import com.azure.resourcemanager.networkcloud.models.L3Network;
import com.azure.resourcemanager.networkcloud.models.L3Networks;

public final class L3NetworksImpl implements L3Networks {
    private static final ClientLogger LOGGER = new ClientLogger(L3NetworksImpl.class);

    private final L3NetworksClient innerClient;

    private final com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager;

    public L3NetworksImpl(
        L3NetworksClient innerClient, com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<L3Network> list() {
        PagedIterable<L3NetworkInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new L3NetworkImpl(inner1, this.manager()));
    }

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

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

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

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

    public L3Network getByResourceGroup(String resourceGroupName, String l3NetworkName) {
        L3NetworkInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, l3NetworkName);
        if (inner != null) {
            return new L3NetworkImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.networkcloud.NetworkCloudManager manager() {
        return this.serviceManager;
    }

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