// 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.ContactProfilesClient;
import com.azure.resourcemanager.orbital.fluent.models.ContactProfileInner;
import com.azure.resourcemanager.orbital.models.ContactProfile;
import com.azure.resourcemanager.orbital.models.ContactProfiles;

public final class ContactProfilesImpl implements ContactProfiles {
    private static final ClientLogger LOGGER = new ClientLogger(ContactProfilesImpl.class);

    private final ContactProfilesClient innerClient;

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

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

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

    public ContactProfile getByResourceGroup(String resourceGroupName, String contactProfileName) {
        ContactProfileInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, contactProfileName);
        if (inner != null) {
            return new ContactProfileImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public PagedIterable<ContactProfile> list() {
        PagedIterable<ContactProfileInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new ContactProfileImpl(inner1, this.manager()));
    }

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

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

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

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

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

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

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

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