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

package com.azure.resourcemanager.machinelearningservices.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.machinelearningservices.fluent.models.ServiceResourceInner;
import com.azure.resourcemanager.machinelearningservices.models.CreateServiceRequest;
import com.azure.resourcemanager.machinelearningservices.models.CreateServiceRequestEnvironmentImageRequest;
import com.azure.resourcemanager.machinelearningservices.models.CreateServiceRequestKeys;
import com.azure.resourcemanager.machinelearningservices.models.Identity;
import com.azure.resourcemanager.machinelearningservices.models.ServiceResource;
import com.azure.resourcemanager.machinelearningservices.models.ServiceResponseBase;
import com.azure.resourcemanager.machinelearningservices.models.Sku;
import java.util.Collections;
import java.util.Map;

public final class ServiceResourceImpl implements ServiceResource, ServiceResource.Definition, ServiceResource.Update {
    private ServiceResourceInner innerObject;

    private final com.azure.resourcemanager.machinelearningservices.MachineLearningServicesManager 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 ServiceResponseBase properties() {
        return this.innerModel().properties();
    }

    public Identity identity() {
        return this.innerModel().identity();
    }

    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 ServiceResourceInner innerModel() {
        return this.innerObject;
    }

    private com.azure.resourcemanager.machinelearningservices.MachineLearningServicesManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String workspaceName;

    private String serviceName;

    private CreateServiceRequest createProperties;

    private CreateServiceRequest updateProperties;

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

    public ServiceResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineLearningServices()
                .createOrUpdate(resourceGroupName, workspaceName, serviceName, createProperties, Context.NONE);
        return this;
    }

    public ServiceResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineLearningServices()
                .createOrUpdate(resourceGroupName, workspaceName, serviceName, createProperties, context);
        return this;
    }

    ServiceResourceImpl(
        String name, com.azure.resourcemanager.machinelearningservices.MachineLearningServicesManager serviceManager) {
        this.innerObject = new ServiceResourceInner();
        this.serviceManager = serviceManager;
        this.serviceName = name;
        this.createProperties = new CreateServiceRequest();
    }

    public ServiceResourceImpl update() {
        this.updateProperties = new CreateServiceRequest();
        return this;
    }

    public ServiceResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineLearningServices()
                .createOrUpdate(resourceGroupName, workspaceName, serviceName, updateProperties, Context.NONE);
        return this;
    }

    public ServiceResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineLearningServices()
                .createOrUpdate(resourceGroupName, workspaceName, serviceName, updateProperties, context);
        return this;
    }

    ServiceResourceImpl(
        ServiceResourceInner innerObject,
        com.azure.resourcemanager.machinelearningservices.MachineLearningServicesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.workspaceName = Utils.getValueFromIdByName(innerObject.id(), "workspaces");
        this.serviceName = Utils.getValueFromIdByName(innerObject.id(), "services");
    }

    public ServiceResource refresh() {
        Boolean localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineLearningServices()
                .getWithResponse(resourceGroupName, workspaceName, serviceName, localExpand, Context.NONE)
                .getValue();
        return this;
    }

    public ServiceResource refresh(Context context) {
        Boolean localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineLearningServices()
                .getWithResponse(resourceGroupName, workspaceName, serviceName, localExpand, context)
                .getValue();
        return this;
    }

    public ServiceResourceImpl withRegion(Region location) {
        this.createProperties.withLocation(location.toString());
        return this;
    }

    public ServiceResourceImpl withRegion(String location) {
        this.createProperties.withLocation(location);
        return this;
    }

    public ServiceResourceImpl withDescription(String description) {
        if (isInCreateMode()) {
            this.createProperties.withDescription(description);
            return this;
        } else {
            this.updateProperties.withDescription(description);
            return this;
        }
    }

    public ServiceResourceImpl withKvTags(Map<String, String> kvTags) {
        if (isInCreateMode()) {
            this.createProperties.withKvTags(kvTags);
            return this;
        } else {
            this.updateProperties.withKvTags(kvTags);
            return this;
        }
    }

    public ServiceResourceImpl withProperties(Map<String, String> properties) {
        if (isInCreateMode()) {
            this.createProperties.withProperties(properties);
            return this;
        } else {
            this.updateProperties.withProperties(properties);
            return this;
        }
    }

    public ServiceResourceImpl withKeys(CreateServiceRequestKeys keys) {
        if (isInCreateMode()) {
            this.createProperties.withKeys(keys);
            return this;
        } else {
            this.updateProperties.withKeys(keys);
            return this;
        }
    }

    public ServiceResourceImpl withEnvironmentImageRequest(
        CreateServiceRequestEnvironmentImageRequest environmentImageRequest) {
        if (isInCreateMode()) {
            this.createProperties.withEnvironmentImageRequest(environmentImageRequest);
            return this;
        } else {
            this.updateProperties.withEnvironmentImageRequest(environmentImageRequest);
            return this;
        }
    }

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