// 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.PredictionsClient;
import com.azure.resourcemanager.customerinsights.fluent.models.PredictionModelStatusInner;
import com.azure.resourcemanager.customerinsights.fluent.models.PredictionResourceFormatInner;
import com.azure.resourcemanager.customerinsights.fluent.models.PredictionTrainingResultsInner;
import com.azure.resourcemanager.customerinsights.models.PredictionModelStatus;
import com.azure.resourcemanager.customerinsights.models.PredictionResourceFormat;
import com.azure.resourcemanager.customerinsights.models.PredictionTrainingResults;
import com.azure.resourcemanager.customerinsights.models.Predictions;

public final class PredictionsImpl implements Predictions {
    private static final ClientLogger LOGGER = new ClientLogger(PredictionsImpl.class);

    private final PredictionsClient innerClient;

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

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

    public Response<PredictionResourceFormat> getWithResponse(
        String resourceGroupName, String hubName, String predictionName, Context context) {
        Response<PredictionResourceFormatInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, hubName, predictionName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new PredictionResourceFormatImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PredictionResourceFormat get(String resourceGroupName, String hubName, String predictionName) {
        PredictionResourceFormatInner inner = this.serviceClient().get(resourceGroupName, hubName, predictionName);
        if (inner != null) {
            return new PredictionResourceFormatImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public Response<PredictionTrainingResults> getTrainingResultsWithResponse(
        String resourceGroupName, String hubName, String predictionName, Context context) {
        Response<PredictionTrainingResultsInner> inner =
            this.serviceClient().getTrainingResultsWithResponse(resourceGroupName, hubName, predictionName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new PredictionTrainingResultsImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PredictionTrainingResults getTrainingResults(
        String resourceGroupName, String hubName, String predictionName) {
        PredictionTrainingResultsInner inner =
            this.serviceClient().getTrainingResults(resourceGroupName, hubName, predictionName);
        if (inner != null) {
            return new PredictionTrainingResultsImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<PredictionModelStatus> getModelStatusWithResponse(
        String resourceGroupName, String hubName, String predictionName, Context context) {
        Response<PredictionModelStatusInner> inner =
            this.serviceClient().getModelStatusWithResponse(resourceGroupName, hubName, predictionName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new PredictionModelStatusImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public PredictionModelStatus getModelStatus(String resourceGroupName, String hubName, String predictionName) {
        PredictionModelStatusInner inner =
            this.serviceClient().getModelStatus(resourceGroupName, hubName, predictionName);
        if (inner != null) {
            return new PredictionModelStatusImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Void> modelStatusWithResponse(
        String resourceGroupName,
        String hubName,
        String predictionName,
        PredictionModelStatusInner parameters,
        Context context) {
        return this
            .serviceClient()
            .modelStatusWithResponse(resourceGroupName, hubName, predictionName, parameters, context);
    }

    public void modelStatus(
        String resourceGroupName, String hubName, String predictionName, PredictionModelStatusInner parameters) {
        this.serviceClient().modelStatus(resourceGroupName, hubName, predictionName, parameters);
    }

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

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

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

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

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

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

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