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

package com.azure.resourcemanager.communication.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.communication.fluent.EmailServicesClient;
import com.azure.resourcemanager.communication.fluent.models.EmailServiceResourceInner;
import com.azure.resourcemanager.communication.models.EmailServiceResource;
import com.azure.resourcemanager.communication.models.EmailServices;
import java.util.Collections;
import java.util.List;

public final class EmailServicesImpl implements EmailServices {
    private static final ClientLogger LOGGER = new ClientLogger(EmailServicesImpl.class);

    private final EmailServicesClient innerClient;

    private final com.azure.resourcemanager.communication.CommunicationManager serviceManager;

    public EmailServicesImpl(
        EmailServicesClient innerClient, com.azure.resourcemanager.communication.CommunicationManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

    public EmailServiceResource getByResourceGroup(String resourceGroupName, String emailServiceName) {
        EmailServiceResourceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, emailServiceName);
        if (inner != null) {
            return new EmailServiceResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public PagedIterable<EmailServiceResource> list() {
        PagedIterable<EmailServiceResourceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new EmailServiceResourceImpl(inner1, this.manager()));
    }

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

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

    public PagedIterable<EmailServiceResource> listByResourceGroup(String resourceGroupName, Context context) {
        PagedIterable<EmailServiceResourceInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new EmailServiceResourceImpl(inner1, this.manager()));
    }

    public Response<List<String>> listVerifiedExchangeOnlineDomainsWithResponse(Context context) {
        return this.serviceClient().listVerifiedExchangeOnlineDomainsWithResponse(context);
    }

    public List<String> listVerifiedExchangeOnlineDomains() {
        List<String> inner = this.serviceClient().listVerifiedExchangeOnlineDomains();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

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

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

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

    private com.azure.resourcemanager.communication.CommunicationManager manager() {
        return this.serviceManager;
    }

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