// 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.util.Context;
import com.azure.resourcemanager.synapse.fluent.models.KeyInner;
import com.azure.resourcemanager.synapse.models.Key;

public final class KeyImpl implements Key, Key.Definition, Key.Update {
    private KeyInner innerObject;

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

    public String id() {
        return this.innerModel().id();
    }

    public String name() {
        return this.innerModel().name();
    }

    public String type() {
        return this.innerModel().type();
    }

    public Boolean isActiveCmk() {
        return this.innerModel().isActiveCmk();
    }

    public String keyVaultUrl() {
        return this.innerModel().keyVaultUrl();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

    public KeyInner innerModel() {
        return this.innerObject;
    }

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

    private String resourceGroupName;

    private String workspaceName;

    private String keyName;

    public KeyImpl withExistingWorkspace(String resourceGroupName, String workspaceName) {
        this.resourceGroupName = resourceGroupName;
        this.workspaceName = workspaceName;
        return this;
    }

    public Key create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getKeys()
                .createOrUpdateWithResponse(resourceGroupName, workspaceName, keyName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Key create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getKeys()
                .createOrUpdateWithResponse(resourceGroupName, workspaceName, keyName, this.innerModel(), context)
                .getValue();
        return this;
    }

    KeyImpl(String name, com.azure.resourcemanager.synapse.SynapseManager serviceManager) {
        this.innerObject = new KeyInner();
        this.serviceManager = serviceManager;
        this.keyName = name;
    }

    public KeyImpl update() {
        return this;
    }

    public Key apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getKeys()
                .createOrUpdateWithResponse(resourceGroupName, workspaceName, keyName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Key apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getKeys()
                .createOrUpdateWithResponse(resourceGroupName, workspaceName, keyName, this.innerModel(), context)
                .getValue();
        return this;
    }

    KeyImpl(KeyInner innerObject, com.azure.resourcemanager.synapse.SynapseManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.workspaceName = Utils.getValueFromIdByName(innerObject.id(), "workspaces");
        this.keyName = Utils.getValueFromIdByName(innerObject.id(), "keys");
    }

    public Key refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getKeys()
                .getWithResponse(resourceGroupName, workspaceName, keyName, Context.NONE)
                .getValue();
        return this;
    }

    public Key refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getKeys()
                .getWithResponse(resourceGroupName, workspaceName, keyName, context)
                .getValue();
        return this;
    }

    public KeyImpl withIsActiveCmk(Boolean isActiveCmk) {
        this.innerModel().withIsActiveCmk(isActiveCmk);
        return this;
    }

    public KeyImpl withKeyVaultUrl(String keyVaultUrl) {
        this.innerModel().withKeyVaultUrl(keyVaultUrl);
        return this;
    }
}
