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

package com.azure.resourcemanager.databricks.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.databricks.fluent.models.PrivateEndpointConnectionInner;
import com.azure.resourcemanager.databricks.fluent.models.WorkspaceInner;
import com.azure.resourcemanager.databricks.models.CreatedBy;
import com.azure.resourcemanager.databricks.models.ManagedIdentityConfiguration;
import com.azure.resourcemanager.databricks.models.PrivateEndpointConnection;
import com.azure.resourcemanager.databricks.models.ProvisioningState;
import com.azure.resourcemanager.databricks.models.PublicNetworkAccess;
import com.azure.resourcemanager.databricks.models.RequiredNsgRules;
import com.azure.resourcemanager.databricks.models.Sku;
import com.azure.resourcemanager.databricks.models.Workspace;
import com.azure.resourcemanager.databricks.models.WorkspaceCustomParameters;
import com.azure.resourcemanager.databricks.models.WorkspacePropertiesEncryption;
import com.azure.resourcemanager.databricks.models.WorkspaceProviderAuthorization;
import com.azure.resourcemanager.databricks.models.WorkspaceUpdate;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class WorkspaceImpl implements Workspace, Workspace.Definition, Workspace.Update {
    private WorkspaceInner innerObject;

    private final com.azure.resourcemanager.databricks.AzureDatabricksManager serviceManager;

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

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

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

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

    public Map<String, String> tags() {
        Map<String, String> inner = this.innerModel().tags();
        if (inner != null) {
            return Collections.unmodifiableMap(inner);
        } else {
            return Collections.emptyMap();
        }
    }

    public Sku sku() {
        return this.innerModel().sku();
    }

    public SystemData systemData() {
        return this.innerModel().systemData();
    }

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

    public WorkspaceCustomParameters parameters() {
        return this.innerModel().parameters();
    }

    public ProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

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

    public List<WorkspaceProviderAuthorization> authorizations() {
        List<WorkspaceProviderAuthorization> inner = this.innerModel().authorizations();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

    public CreatedBy createdBy() {
        return this.innerModel().createdBy();
    }

    public CreatedBy updatedBy() {
        return this.innerModel().updatedBy();
    }

    public OffsetDateTime createdDateTime() {
        return this.innerModel().createdDateTime();
    }

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

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

    public ManagedIdentityConfiguration storageAccountIdentity() {
        return this.innerModel().storageAccountIdentity();
    }

    public ManagedIdentityConfiguration managedDiskIdentity() {
        return this.innerModel().managedDiskIdentity();
    }

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

    public WorkspacePropertiesEncryption encryption() {
        return this.innerModel().encryption();
    }

    public List<PrivateEndpointConnection> privateEndpointConnections() {
        List<PrivateEndpointConnectionInner> inner = this.innerModel().privateEndpointConnections();
        if (inner != null) {
            return Collections
                .unmodifiableList(
                    inner
                        .stream()
                        .map(inner1 -> new PrivateEndpointConnectionImpl(inner1, this.manager()))
                        .collect(Collectors.toList()));
        } else {
            return Collections.emptyList();
        }
    }

    public PublicNetworkAccess publicNetworkAccess() {
        return this.innerModel().publicNetworkAccess();
    }

    public RequiredNsgRules requiredNsgRules() {
        return this.innerModel().requiredNsgRules();
    }

    public Region region() {
        return Region.fromName(this.regionName());
    }

    public String regionName() {
        return this.location();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.databricks.AzureDatabricksManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String workspaceName;

    private WorkspaceUpdate updateParameters;

    public WorkspaceImpl withExistingResourceGroup(String resourceGroupName) {
        this.resourceGroupName = resourceGroupName;
        return this;
    }

    public Workspace create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkspaces()
                .createOrUpdate(resourceGroupName, workspaceName, this.innerModel(), Context.NONE);
        return this;
    }

    public Workspace create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkspaces()
                .createOrUpdate(resourceGroupName, workspaceName, this.innerModel(), context);
        return this;
    }

    WorkspaceImpl(String name, com.azure.resourcemanager.databricks.AzureDatabricksManager serviceManager) {
        this.innerObject = new WorkspaceInner();
        this.serviceManager = serviceManager;
        this.workspaceName = name;
    }

    public WorkspaceImpl update() {
        this.updateParameters = new WorkspaceUpdate();
        return this;
    }

    public Workspace apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkspaces()
                .update(resourceGroupName, workspaceName, updateParameters, Context.NONE);
        return this;
    }

    public Workspace apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkspaces()
                .update(resourceGroupName, workspaceName, updateParameters, context);
        return this;
    }

    WorkspaceImpl(
        WorkspaceInner innerObject, com.azure.resourcemanager.databricks.AzureDatabricksManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.workspaceName = Utils.getValueFromIdByName(innerObject.id(), "workspaces");
    }

    public Workspace refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkspaces()
                .getByResourceGroupWithResponse(resourceGroupName, workspaceName, Context.NONE)
                .getValue();
        return this;
    }

    public Workspace refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getWorkspaces()
                .getByResourceGroupWithResponse(resourceGroupName, workspaceName, context)
                .getValue();
        return this;
    }

    public WorkspaceImpl withRegion(Region location) {
        this.innerModel().withLocation(location.toString());
        return this;
    }

    public WorkspaceImpl withRegion(String location) {
        this.innerModel().withLocation(location);
        return this;
    }

    public WorkspaceImpl withManagedResourceGroupId(String managedResourceGroupId) {
        this.innerModel().withManagedResourceGroupId(managedResourceGroupId);
        return this;
    }

    public WorkspaceImpl withTags(Map<String, String> tags) {
        if (isInCreateMode()) {
            this.innerModel().withTags(tags);
            return this;
        } else {
            this.updateParameters.withTags(tags);
            return this;
        }
    }

    public WorkspaceImpl withSku(Sku sku) {
        this.innerModel().withSku(sku);
        return this;
    }

    public WorkspaceImpl withParameters(WorkspaceCustomParameters parameters) {
        this.innerModel().withParameters(parameters);
        return this;
    }

    public WorkspaceImpl withUiDefinitionUri(String uiDefinitionUri) {
        this.innerModel().withUiDefinitionUri(uiDefinitionUri);
        return this;
    }

    public WorkspaceImpl withAuthorizations(List<WorkspaceProviderAuthorization> authorizations) {
        this.innerModel().withAuthorizations(authorizations);
        return this;
    }

    public WorkspaceImpl withCreatedBy(CreatedBy createdBy) {
        this.innerModel().withCreatedBy(createdBy);
        return this;
    }

    public WorkspaceImpl withUpdatedBy(CreatedBy updatedBy) {
        this.innerModel().withUpdatedBy(updatedBy);
        return this;
    }

    public WorkspaceImpl withStorageAccountIdentity(ManagedIdentityConfiguration storageAccountIdentity) {
        this.innerModel().withStorageAccountIdentity(storageAccountIdentity);
        return this;
    }

    public WorkspaceImpl withManagedDiskIdentity(ManagedIdentityConfiguration managedDiskIdentity) {
        this.innerModel().withManagedDiskIdentity(managedDiskIdentity);
        return this;
    }

    public WorkspaceImpl withEncryption(WorkspacePropertiesEncryption encryption) {
        this.innerModel().withEncryption(encryption);
        return this;
    }

    public WorkspaceImpl withPublicNetworkAccess(PublicNetworkAccess publicNetworkAccess) {
        this.innerModel().withPublicNetworkAccess(publicNetworkAccess);
        return this;
    }

    public WorkspaceImpl withRequiredNsgRules(RequiredNsgRules requiredNsgRules) {
        this.innerModel().withRequiredNsgRules(requiredNsgRules);
        return this;
    }

    private boolean isInCreateMode() {
        return this.innerModel().id() == null;
    }
}
