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

package com.azure.resourcemanager.kusto.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.kusto.fluent.ScriptsClient;
import com.azure.resourcemanager.kusto.fluent.models.CheckNameResultInner;
import com.azure.resourcemanager.kusto.fluent.models.ScriptInner;
import com.azure.resourcemanager.kusto.models.CheckNameResult;
import com.azure.resourcemanager.kusto.models.Script;
import com.azure.resourcemanager.kusto.models.ScriptCheckNameRequest;
import com.azure.resourcemanager.kusto.models.Scripts;

public final class ScriptsImpl implements Scripts {
    private static final ClientLogger LOGGER = new ClientLogger(ScriptsImpl.class);

    private final ScriptsClient innerClient;

    private final com.azure.resourcemanager.kusto.KustoManager serviceManager;

    public ScriptsImpl(ScriptsClient innerClient, com.azure.resourcemanager.kusto.KustoManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Script> listByDatabase(String resourceGroupName, String clusterName, String databaseName) {
        PagedIterable<ScriptInner> inner =
            this.serviceClient().listByDatabase(resourceGroupName, clusterName, databaseName);
        return Utils.mapPage(inner, inner1 -> new ScriptImpl(inner1, this.manager()));
    }

    public PagedIterable<Script> listByDatabase(
        String resourceGroupName, String clusterName, String databaseName, Context context) {
        PagedIterable<ScriptInner> inner =
            this.serviceClient().listByDatabase(resourceGroupName, clusterName, databaseName, context);
        return Utils.mapPage(inner, inner1 -> new ScriptImpl(inner1, this.manager()));
    }

    public Response<Script> getWithResponse(
        String resourceGroupName, String clusterName, String databaseName, String scriptName, Context context) {
        Response<ScriptInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, clusterName, databaseName, scriptName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ScriptImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Script get(String resourceGroupName, String clusterName, String databaseName, String scriptName) {
        ScriptInner inner = this.serviceClient().get(resourceGroupName, clusterName, databaseName, scriptName);
        if (inner != null) {
            return new ScriptImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String clusterName, String databaseName, String scriptName) {
        this.serviceClient().delete(resourceGroupName, clusterName, databaseName, scriptName);
    }

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

    public Response<CheckNameResult> checkNameAvailabilityWithResponse(
        String resourceGroupName,
        String clusterName,
        String databaseName,
        ScriptCheckNameRequest scriptName,
        Context context) {
        Response<CheckNameResultInner> inner =
            this
                .serviceClient()
                .checkNameAvailabilityWithResponse(resourceGroupName, clusterName, databaseName, scriptName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CheckNameResultImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public CheckNameResult checkNameAvailability(
        String resourceGroupName, String clusterName, String databaseName, ScriptCheckNameRequest scriptName) {
        CheckNameResultInner inner =
            this.serviceClient().checkNameAvailability(resourceGroupName, clusterName, databaseName, scriptName);
        if (inner != null) {
            return new CheckNameResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.kusto.KustoManager manager() {
        return this.serviceManager;
    }

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