// 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.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.machinelearning.fluent.models.CodeVersionInner;
import com.azure.resourcemanager.machinelearning.models.CodeVersion;
import com.azure.resourcemanager.machinelearning.models.CodeVersionProperties;

public final class CodeVersionImpl implements CodeVersion, CodeVersion.Definition, CodeVersion.Update {
    private CodeVersionInner 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 CodeVersionProperties properties() {
        return this.innerModel().properties();
    }

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String workspaceName;

    private String name;

    private String version;

    public CodeVersionImpl withExistingCode(String resourceGroupName, String workspaceName, String name) {
        this.resourceGroupName = resourceGroupName;
        this.workspaceName = workspaceName;
        this.name = name;
        return this;
    }

    public CodeVersion create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCodeVersions()
                .createOrUpdateWithResponse(
                    resourceGroupName, workspaceName, name, version, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public CodeVersion create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCodeVersions()
                .createOrUpdateWithResponse(resourceGroupName, workspaceName, name, version, this.innerModel(), context)
                .getValue();
        return this;
    }

    CodeVersionImpl(String name, com.azure.resourcemanager.machinelearning.MachineLearningManager serviceManager) {
        this.innerObject = new CodeVersionInner();
        this.serviceManager = serviceManager;
        this.version = name;
    }

    public CodeVersionImpl update() {
        return this;
    }

    public CodeVersion apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCodeVersions()
                .createOrUpdateWithResponse(
                    resourceGroupName, workspaceName, name, version, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public CodeVersion apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCodeVersions()
                .createOrUpdateWithResponse(resourceGroupName, workspaceName, name, version, this.innerModel(), context)
                .getValue();
        return this;
    }

    CodeVersionImpl(
        CodeVersionInner 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(), "codes");
        this.version = Utils.getValueFromIdByName(innerObject.id(), "versions");
    }

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

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

    public CodeVersionImpl withProperties(CodeVersionProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }
}
