// 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.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.machinelearning.fluent.models.OnlineDeploymentInner;
import com.azure.resourcemanager.machinelearning.models.DeploymentLogs;
import com.azure.resourcemanager.machinelearning.models.DeploymentLogsRequest;
import com.azure.resourcemanager.machinelearning.models.ManagedServiceIdentity;
import com.azure.resourcemanager.machinelearning.models.OnlineDeployment;
import com.azure.resourcemanager.machinelearning.models.OnlineDeploymentProperties;
import com.azure.resourcemanager.machinelearning.models.PartialMinimalTrackedResourceWithSku;
import com.azure.resourcemanager.machinelearning.models.PartialSku;
import com.azure.resourcemanager.machinelearning.models.Sku;
import java.util.Collections;
import java.util.Map;

public final class OnlineDeploymentImpl
    implements OnlineDeployment, OnlineDeployment.Definition, OnlineDeployment.Update {
    private OnlineDeploymentInner 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 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 ManagedServiceIdentity identity() {
        return this.innerModel().identity();
    }

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String workspaceName;

    private String endpointName;

    private String deploymentName;

    private PartialMinimalTrackedResourceWithSku updateBody;

    public OnlineDeploymentImpl withExistingOnlineEndpoint(
        String resourceGroupName, String workspaceName, String endpointName) {
        this.resourceGroupName = resourceGroupName;
        this.workspaceName = workspaceName;
        this.endpointName = endpointName;
        return this;
    }

    public OnlineDeployment create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getOnlineDeployments()
                .createOrUpdate(
                    resourceGroupName, workspaceName, endpointName, deploymentName, this.innerModel(), Context.NONE);
        return this;
    }

    public OnlineDeployment create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getOnlineDeployments()
                .createOrUpdate(
                    resourceGroupName, workspaceName, endpointName, deploymentName, this.innerModel(), context);
        return this;
    }

    OnlineDeploymentImpl(String name, com.azure.resourcemanager.machinelearning.MachineLearningManager serviceManager) {
        this.innerObject = new OnlineDeploymentInner();
        this.serviceManager = serviceManager;
        this.deploymentName = name;
    }

    public OnlineDeploymentImpl update() {
        this.updateBody = new PartialMinimalTrackedResourceWithSku();
        return this;
    }

    public OnlineDeployment apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getOnlineDeployments()
                .update(resourceGroupName, workspaceName, endpointName, deploymentName, updateBody, Context.NONE);
        return this;
    }

    public OnlineDeployment apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getOnlineDeployments()
                .update(resourceGroupName, workspaceName, endpointName, deploymentName, updateBody, context);
        return this;
    }

    OnlineDeploymentImpl(
        OnlineDeploymentInner 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.endpointName = Utils.getValueFromIdByName(innerObject.id(), "onlineEndpoints");
        this.deploymentName = Utils.getValueFromIdByName(innerObject.id(), "deployments");
    }

    public OnlineDeployment refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getOnlineDeployments()
                .getWithResponse(resourceGroupName, workspaceName, endpointName, deploymentName, Context.NONE)
                .getValue();
        return this;
    }

    public OnlineDeployment refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getOnlineDeployments()
                .getWithResponse(resourceGroupName, workspaceName, endpointName, deploymentName, context)
                .getValue();
        return this;
    }

    public Response<DeploymentLogs> getLogsWithResponse(DeploymentLogsRequest body, Context context) {
        return serviceManager
            .onlineDeployments()
            .getLogsWithResponse(resourceGroupName, workspaceName, endpointName, deploymentName, body, context);
    }

    public DeploymentLogs getLogs(DeploymentLogsRequest body) {
        return serviceManager
            .onlineDeployments()
            .getLogs(resourceGroupName, workspaceName, endpointName, deploymentName, body);
    }

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

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

    public OnlineDeploymentImpl withProperties(OnlineDeploymentProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

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

    public OnlineDeploymentImpl withIdentity(ManagedServiceIdentity identity) {
        this.innerModel().withIdentity(identity);
        return this;
    }

    public OnlineDeploymentImpl withKind(String kind) {
        this.innerModel().withKind(kind);
        return this;
    }

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

    public OnlineDeploymentImpl withSku(PartialSku sku) {
        this.updateBody.withSku(sku);
        return this;
    }

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