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

package com.azure.resourcemanager.customerinsights.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.customerinsights.fluent.ProfilesClient;
import com.azure.resourcemanager.customerinsights.fluent.models.KpiDefinitionInner;
import com.azure.resourcemanager.customerinsights.fluent.models.ProfileResourceFormatInner;
import com.azure.resourcemanager.customerinsights.models.KpiDefinition;
import com.azure.resourcemanager.customerinsights.models.ProfileResourceFormat;
import com.azure.resourcemanager.customerinsights.models.Profiles;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public final class ProfilesImpl implements Profiles {
    private static final ClientLogger LOGGER = new ClientLogger(ProfilesImpl.class);

    private final ProfilesClient innerClient;

    private final com.azure.resourcemanager.customerinsights.CustomerInsightsManager serviceManager;

    public ProfilesImpl(
        ProfilesClient innerClient, com.azure.resourcemanager.customerinsights.CustomerInsightsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<ProfileResourceFormat> getWithResponse(
        String resourceGroupName, String hubName, String profileName, String localeCode, Context context) {
        Response<ProfileResourceFormatInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, hubName, profileName, localeCode, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ProfileResourceFormatImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ProfileResourceFormat get(String resourceGroupName, String hubName, String profileName) {
        ProfileResourceFormatInner inner = this.serviceClient().get(resourceGroupName, hubName, profileName);
        if (inner != null) {
            return new ProfileResourceFormatImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String hubName, String profileName) {
        this.serviceClient().delete(resourceGroupName, hubName, profileName);
    }

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

    public PagedIterable<ProfileResourceFormat> listByHub(String resourceGroupName, String hubName) {
        PagedIterable<ProfileResourceFormatInner> inner = this.serviceClient().listByHub(resourceGroupName, hubName);
        return Utils.mapPage(inner, inner1 -> new ProfileResourceFormatImpl(inner1, this.manager()));
    }

    public PagedIterable<ProfileResourceFormat> listByHub(
        String resourceGroupName, String hubName, String localeCode, Context context) {
        PagedIterable<ProfileResourceFormatInner> inner =
            this.serviceClient().listByHub(resourceGroupName, hubName, localeCode, context);
        return Utils.mapPage(inner, inner1 -> new ProfileResourceFormatImpl(inner1, this.manager()));
    }

    public Response<List<KpiDefinition>> getEnrichingKpisWithResponse(
        String resourceGroupName, String hubName, String profileName, Context context) {
        Response<List<KpiDefinitionInner>> inner =
            this.serviceClient().getEnrichingKpisWithResponse(resourceGroupName, hubName, profileName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                inner
                    .getValue()
                    .stream()
                    .map(inner1 -> new KpiDefinitionImpl(inner1, this.manager()))
                    .collect(Collectors.toList()));
        } else {
            return null;
        }
    }

    public List<KpiDefinition> getEnrichingKpis(String resourceGroupName, String hubName, String profileName) {
        List<KpiDefinitionInner> inner = this.serviceClient().getEnrichingKpis(resourceGroupName, hubName, profileName);
        if (inner != null) {
            return Collections
                .unmodifiableList(
                    inner
                        .stream()
                        .map(inner1 -> new KpiDefinitionImpl(inner1, this.manager()))
                        .collect(Collectors.toList()));
        } else {
            return Collections.emptyList();
        }
    }

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

    public Response<ProfileResourceFormat> getByIdWithResponse(String id, String localeCode, 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 hubName = Utils.getValueFromIdByName(id, "hubs");
        if (hubName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'hubs'.", id)));
        }
        String profileName = Utils.getValueFromIdByName(id, "profiles");
        if (profileName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'profiles'.", id)));
        }
        return this.getWithResponse(resourceGroupName, hubName, profileName, localeCode, 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 hubName = Utils.getValueFromIdByName(id, "hubs");
        if (hubName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'hubs'.", id)));
        }
        String profileName = Utils.getValueFromIdByName(id, "profiles");
        if (profileName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'profiles'.", id)));
        }
        String localLocaleCode = null;
        this.delete(resourceGroupName, hubName, profileName, localLocaleCode, Context.NONE);
    }

    public void deleteByIdWithResponse(String id, String localeCode, 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 hubName = Utils.getValueFromIdByName(id, "hubs");
        if (hubName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'hubs'.", id)));
        }
        String profileName = Utils.getValueFromIdByName(id, "profiles");
        if (profileName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'profiles'.", id)));
        }
        this.delete(resourceGroupName, hubName, profileName, localeCode, context);
    }

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

    private com.azure.resourcemanager.customerinsights.CustomerInsightsManager manager() {
        return this.serviceManager;
    }

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