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

package com.azure.resourcemanager.machinelearning.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.machinelearning.fluent.models.DatastoreInner;
import com.azure.resourcemanager.machinelearning.models.Datastore;
import com.azure.resourcemanager.machinelearning.models.DatastoreProperties;
import com.azure.resourcemanager.machinelearning.models.DatastoreSecrets;

public final class DatastoreImpl implements Datastore, Datastore.Definition, Datastore.Update {
    private DatastoreInner innerObject;

    private final com.azure.resourcemanager.machinelearning.MachineLearningManager serviceManager;

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

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

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

    public DatastoreProperties properties() {
        return this.innerModel().properties();
    }

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.machinelearning.MachineLearningManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String workspaceName;

    private String name;

    private Boolean createSkipValidation;

    private Boolean updateSkipValidation;

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

    public Datastore create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDatastores()
                .createOrUpdateWithResponse(
                    resourceGroupName, workspaceName, name, this.innerModel(), createSkipValidation, Context.NONE)
                .getValue();
        return this;
    }

    public Datastore create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDatastores()
                .createOrUpdateWithResponse(
                    resourceGroupName, workspaceName, name, this.innerModel(), createSkipValidation, context)
                .getValue();
        return this;
    }

    DatastoreImpl(String name, com.azure.resourcemanager.machinelearning.MachineLearningManager serviceManager) {
        this.innerObject = new DatastoreInner();
        this.serviceManager = serviceManager;
        this.name = name;
        this.createSkipValidation = null;
    }

    public DatastoreImpl update() {
        this.updateSkipValidation = null;
        return this;
    }

    public Datastore apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDatastores()
                .createOrUpdateWithResponse(
                    resourceGroupName, workspaceName, name, this.innerModel(), updateSkipValidation, Context.NONE)
                .getValue();
        return this;
    }

    public Datastore apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDatastores()
                .createOrUpdateWithResponse(
                    resourceGroupName, workspaceName, name, this.innerModel(), updateSkipValidation, context)
                .getValue();
        return this;
    }

    DatastoreImpl(
        DatastoreInner innerObject, com.azure.resourcemanager.machinelearning.MachineLearningManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.workspaceName = Utils.getValueFromIdByName(innerObject.id(), "workspaces");
        this.name = Utils.getValueFromIdByName(innerObject.id(), "datastores");
    }

    public Datastore refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDatastores()
                .getWithResponse(resourceGroupName, workspaceName, name, Context.NONE)
                .getValue();
        return this;
    }

    public Datastore refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDatastores()
                .getWithResponse(resourceGroupName, workspaceName, name, context)
                .getValue();
        return this;
    }

    public Response<DatastoreSecrets> listSecretsWithResponse(Context context) {
        return serviceManager.datastores().listSecretsWithResponse(resourceGroupName, workspaceName, name, context);
    }

    public DatastoreSecrets listSecrets() {
        return serviceManager.datastores().listSecrets(resourceGroupName, workspaceName, name);
    }

    public DatastoreImpl withProperties(DatastoreProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public DatastoreImpl withSkipValidation(Boolean skipValidation) {
        if (isInCreateMode()) {
            this.createSkipValidation = skipValidation;
            return this;
        } else {
            this.updateSkipValidation = skipValidation;
            return this;
        }
    }

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