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

package com.azure.resourcemanager.servicenetworking.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.servicenetworking.fluent.AssociationsInterfacesClient;
import com.azure.resourcemanager.servicenetworking.fluent.models.AssociationInner;
import com.azure.resourcemanager.servicenetworking.models.Association;
import com.azure.resourcemanager.servicenetworking.models.AssociationsInterfaces;

public final class AssociationsInterfacesImpl implements AssociationsInterfaces {
    private static final ClientLogger LOGGER = new ClientLogger(AssociationsInterfacesImpl.class);

    private final AssociationsInterfacesClient innerClient;

    private final com.azure.resourcemanager.servicenetworking.TrafficControllerManager serviceManager;

    public AssociationsInterfacesImpl(
        AssociationsInterfacesClient innerClient,
        com.azure.resourcemanager.servicenetworking.TrafficControllerManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Association> listByTrafficController(String resourceGroupName, String trafficControllerName) {
        PagedIterable<AssociationInner> inner =
            this.serviceClient().listByTrafficController(resourceGroupName, trafficControllerName);
        return Utils.mapPage(inner, inner1 -> new AssociationImpl(inner1, this.manager()));
    }

    public PagedIterable<Association> listByTrafficController(
        String resourceGroupName, String trafficControllerName, Context context) {
        PagedIterable<AssociationInner> inner =
            this.serviceClient().listByTrafficController(resourceGroupName, trafficControllerName, context);
        return Utils.mapPage(inner, inner1 -> new AssociationImpl(inner1, this.manager()));
    }

    public Response<Association> getWithResponse(
        String resourceGroupName, String trafficControllerName, String associationName, Context context) {
        Response<AssociationInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, trafficControllerName, associationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new AssociationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Association get(String resourceGroupName, String trafficControllerName, String associationName) {
        AssociationInner inner = this.serviceClient().get(resourceGroupName, trafficControllerName, associationName);
        if (inner != null) {
            return new AssociationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.servicenetworking.TrafficControllerManager manager() {
        return this.serviceManager;
    }

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