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

package com.azure.resourcemanager.mobilenetwork.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.mobilenetwork.fluent.SlicesClient;
import com.azure.resourcemanager.mobilenetwork.fluent.models.SliceInner;
import com.azure.resourcemanager.mobilenetwork.models.Slice;
import com.azure.resourcemanager.mobilenetwork.models.Slices;

public final class SlicesImpl implements Slices {
    private static final ClientLogger LOGGER = new ClientLogger(SlicesImpl.class);

    private final SlicesClient innerClient;

    private final com.azure.resourcemanager.mobilenetwork.MobileNetworkManager serviceManager;

    public SlicesImpl(
        SlicesClient innerClient, com.azure.resourcemanager.mobilenetwork.MobileNetworkManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

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

    public Response<Slice> getWithResponse(
        String resourceGroupName, String mobileNetworkName, String sliceName, Context context) {
        Response<SliceInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, mobileNetworkName, sliceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new SliceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Slice get(String resourceGroupName, String mobileNetworkName, String sliceName) {
        SliceInner inner = this.serviceClient().get(resourceGroupName, mobileNetworkName, sliceName);
        if (inner != null) {
            return new SliceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<Slice> listByMobileNetwork(String resourceGroupName, String mobileNetworkName) {
        PagedIterable<SliceInner> inner =
            this.serviceClient().listByMobileNetwork(resourceGroupName, mobileNetworkName);
        return Utils.mapPage(inner, inner1 -> new SliceImpl(inner1, this.manager()));
    }

    public PagedIterable<Slice> listByMobileNetwork(
        String resourceGroupName, String mobileNetworkName, Context context) {
        PagedIterable<SliceInner> inner =
            this.serviceClient().listByMobileNetwork(resourceGroupName, mobileNetworkName, context);
        return Utils.mapPage(inner, inner1 -> new SliceImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.mobilenetwork.MobileNetworkManager manager() {
        return this.serviceManager;
    }

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