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

package com.azure.resourcemanager.peering.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.peering.fluent.PeerAsnsClient;
import com.azure.resourcemanager.peering.fluent.models.PeerAsnInner;
import com.azure.resourcemanager.peering.models.PeerAsn;
import com.azure.resourcemanager.peering.models.PeerAsns;

public final class PeerAsnsImpl implements PeerAsns {
    private static final ClientLogger LOGGER = new ClientLogger(PeerAsnsImpl.class);

    private final PeerAsnsClient innerClient;

    private final com.azure.resourcemanager.peering.PeeringManager serviceManager;

    public PeerAsnsImpl(PeerAsnsClient innerClient, com.azure.resourcemanager.peering.PeeringManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<PeerAsn> getWithResponse(String peerAsnName, Context context) {
        Response<PeerAsnInner> inner = this.serviceClient().getWithResponse(peerAsnName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new PeerAsnImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PeerAsn get(String peerAsnName) {
        PeerAsnInner inner = this.serviceClient().get(peerAsnName);
        if (inner != null) {
            return new PeerAsnImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void delete(String peerAsnName) {
        this.serviceClient().delete(peerAsnName);
    }

    public PagedIterable<PeerAsn> list() {
        PagedIterable<PeerAsnInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new PeerAsnImpl(inner1, this.manager()));
    }

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

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

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

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

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

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

    private com.azure.resourcemanager.peering.PeeringManager manager() {
        return this.serviceManager;
    }

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