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

package com.azure.resourcemanager.orbital.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.orbital.fluent.SpacecraftsClient;
import com.azure.resourcemanager.orbital.fluent.models.AvailableContactsInner;
import com.azure.resourcemanager.orbital.fluent.models.SpacecraftInner;
import com.azure.resourcemanager.orbital.models.AvailableContacts;
import com.azure.resourcemanager.orbital.models.ContactParameters;
import com.azure.resourcemanager.orbital.models.Spacecraft;
import com.azure.resourcemanager.orbital.models.Spacecrafts;

public final class SpacecraftsImpl implements Spacecrafts {
    private static final ClientLogger LOGGER = new ClientLogger(SpacecraftsImpl.class);

    private final SpacecraftsClient innerClient;

    private final com.azure.resourcemanager.orbital.OrbitalManager serviceManager;

    public SpacecraftsImpl(
        SpacecraftsClient innerClient, com.azure.resourcemanager.orbital.OrbitalManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Spacecraft> list() {
        PagedIterable<SpacecraftInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new SpacecraftImpl(inner1, this.manager()));
    }

    public PagedIterable<Spacecraft> list(String skiptoken, Context context) {
        PagedIterable<SpacecraftInner> inner = this.serviceClient().list(skiptoken, context);
        return Utils.mapPage(inner, inner1 -> new SpacecraftImpl(inner1, this.manager()));
    }

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

    public PagedIterable<Spacecraft> listByResourceGroup(String resourceGroupName, String skiptoken, Context context) {
        PagedIterable<SpacecraftInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, skiptoken, context);
        return Utils.mapPage(inner, inner1 -> new SpacecraftImpl(inner1, this.manager()));
    }

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

    public Spacecraft getByResourceGroup(String resourceGroupName, String spacecraftName) {
        SpacecraftInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, spacecraftName);
        if (inner != null) {
            return new SpacecraftImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public PagedIterable<AvailableContacts> listAvailableContacts(
        String resourceGroupName, String spacecraftName, ContactParameters parameters) {
        PagedIterable<AvailableContactsInner> inner =
            this.serviceClient().listAvailableContacts(resourceGroupName, spacecraftName, parameters);
        return Utils.mapPage(inner, inner1 -> new AvailableContactsImpl(inner1, this.manager()));
    }

    public PagedIterable<AvailableContacts> listAvailableContacts(
        String resourceGroupName, String spacecraftName, ContactParameters parameters, Context context) {
        PagedIterable<AvailableContactsInner> inner =
            this.serviceClient().listAvailableContacts(resourceGroupName, spacecraftName, parameters, context);
        return Utils.mapPage(inner, inner1 -> new AvailableContactsImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.orbital.OrbitalManager manager() {
        return this.serviceManager;
    }

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