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

package com.azure.resourcemanager.synapse.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.synapse.fluent.KeysClient;
import com.azure.resourcemanager.synapse.fluent.models.KeyInner;
import com.azure.resourcemanager.synapse.models.Key;
import com.azure.resourcemanager.synapse.models.Keys;

public final class KeysImpl implements Keys {
    private static final ClientLogger LOGGER = new ClientLogger(KeysImpl.class);

    private final KeysClient innerClient;

    private final com.azure.resourcemanager.synapse.SynapseManager serviceManager;

    public KeysImpl(KeysClient innerClient, com.azure.resourcemanager.synapse.SynapseManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Key> listByWorkspace(String resourceGroupName, String workspaceName) {
        PagedIterable<KeyInner> inner = this.serviceClient().listByWorkspace(resourceGroupName, workspaceName);
        return Utils.mapPage(inner, inner1 -> new KeyImpl(inner1, this.manager()));
    }

    public PagedIterable<Key> listByWorkspace(String resourceGroupName, String workspaceName, Context context) {
        PagedIterable<KeyInner> inner = this.serviceClient().listByWorkspace(resourceGroupName, workspaceName, context);
        return Utils.mapPage(inner, inner1 -> new KeyImpl(inner1, this.manager()));
    }

    public Response<Key> getWithResponse(
        String resourceGroupName, String workspaceName, String keyName, Context context) {
        Response<KeyInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, workspaceName, keyName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new KeyImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Key get(String resourceGroupName, String workspaceName, String keyName) {
        KeyInner inner = this.serviceClient().get(resourceGroupName, workspaceName, keyName);
        if (inner != null) {
            return new KeyImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Key> deleteWithResponse(
        String resourceGroupName, String workspaceName, String keyName, Context context) {
        Response<KeyInner> inner =
            this.serviceClient().deleteWithResponse(resourceGroupName, workspaceName, keyName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new KeyImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Key delete(String resourceGroupName, String workspaceName, String keyName) {
        KeyInner inner = this.serviceClient().delete(resourceGroupName, workspaceName, keyName);
        if (inner != null) {
            return new KeyImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.synapse.SynapseManager manager() {
        return this.serviceManager;
    }

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