// 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.OnlineDeploymentDataInner;
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.OnlineDeploymentData;
import com.azure.resourcemanager.machinelearning.models.OnlineDeploymentDetails;
import com.azure.resourcemanager.machinelearning.models.PartialManagedServiceIdentity;
import com.azure.resourcemanager.machinelearning.models.PartialOnlineDeployment;
import com.azure.resourcemanager.machinelearning.models.PartialOnlineDeploymentPartialTrackedResource;
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 OnlineDeploymentDataImpl
    implements OnlineDeploymentData, OnlineDeploymentData.Definition, OnlineDeploymentData.Update {
    private OnlineDeploymentDataInner 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 OnlineDeploymentDetails 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 OnlineDeploymentDataInner 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 PartialOnlineDeploymentPartialTrackedResource updateBody;

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

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

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

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

    public OnlineDeploymentDataImpl update() {
        this.updateBody = new PartialOnlineDeploymentPartialTrackedResource();
        return this;
    }

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

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

    OnlineDeploymentDataImpl(
        OnlineDeploymentDataInner 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 OnlineDeploymentData refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getOnlineDeployments()
                .getWithResponse(resourceGroupName, workspaceName, endpointName, deploymentName, Context.NONE)
                .getValue();
        return this;
    }

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

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

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

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

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

    public OnlineDeploymentDataImpl withProperties(OnlineDeploymentDetails properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

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

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

    public OnlineDeploymentDataImpl withKind(String kind) {
        if (isInCreateMode()) {
            this.innerModel().withKind(kind);
            return this;
        } else {
            this.updateBody.withKind(kind);
            return this;
        }
    }

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

    public OnlineDeploymentDataImpl withIdentity(PartialManagedServiceIdentity identity) {
        this.updateBody.withIdentity(identity);
        return this;
    }

    public OnlineDeploymentDataImpl withProperties(PartialOnlineDeployment properties) {
        this.updateBody.withProperties(properties);
        return this;
    }

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

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