// 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.ContactsClient;
import com.azure.resourcemanager.orbital.fluent.models.ContactInner;
import com.azure.resourcemanager.orbital.models.Contact;
import com.azure.resourcemanager.orbital.models.Contacts;

public final class ContactsImpl implements Contacts {
    private static final ClientLogger LOGGER = new ClientLogger(ContactsImpl.class);

    private final ContactsClient innerClient;

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

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

    public PagedIterable<Contact> list(String resourceGroupName, String spacecraftName) {
        PagedIterable<ContactInner> inner = this.serviceClient().list(resourceGroupName, spacecraftName);
        return Utils.mapPage(inner, inner1 -> new ContactImpl(inner1, this.manager()));
    }

    public PagedIterable<Contact> list(
        String resourceGroupName, String spacecraftName, String skiptoken, Context context) {
        PagedIterable<ContactInner> inner =
            this.serviceClient().list(resourceGroupName, spacecraftName, skiptoken, context);
        return Utils.mapPage(inner, inner1 -> new ContactImpl(inner1, this.manager()));
    }

    public Response<Contact> getWithResponse(
        String resourceGroupName, String spacecraftName, String contactName, Context context) {
        Response<ContactInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, spacecraftName, contactName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ContactImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Contact get(String resourceGroupName, String spacecraftName, String contactName) {
        ContactInner inner = this.serviceClient().get(resourceGroupName, spacecraftName, contactName);
        if (inner != null) {
            return new ContactImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

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

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