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

package com.azure.resourcemanager.servicefabric.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.servicefabric.fluent.models.ServiceResourceInner;
import com.azure.resourcemanager.servicefabric.models.ServiceResource;
import com.azure.resourcemanager.servicefabric.models.ServiceResourceProperties;
import com.azure.resourcemanager.servicefabric.models.ServiceResourceUpdate;
import com.azure.resourcemanager.servicefabric.models.ServiceResourceUpdateProperties;
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.servicefabric.ServiceFabricManager serviceManager;

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

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

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

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

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

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

    private com.azure.resourcemanager.servicefabric.ServiceFabricManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String clusterName;

    private String applicationName;

    private String serviceName;

    private ServiceResourceUpdate updateParameters;

    public ServiceResourceImpl withExistingApplication(
        String resourceGroupName, String clusterName, String applicationName) {
        this.resourceGroupName = resourceGroupName;
        this.clusterName = clusterName;
        this.applicationName = applicationName;
        return this;
    }

    public ServiceResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServices()
                .createOrUpdate(
                    resourceGroupName, clusterName, applicationName, serviceName, this.innerModel(), Context.NONE);
        return this;
    }

    public ServiceResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServices()
                .createOrUpdate(
                    resourceGroupName, clusterName, applicationName, serviceName, this.innerModel(), context);
        return this;
    }

    ServiceResourceImpl(String name, com.azure.resourcemanager.servicefabric.ServiceFabricManager serviceManager) {
        this.innerObject = new ServiceResourceInner();
        this.serviceManager = serviceManager;
        this.serviceName = name;
    }

    public ServiceResourceImpl update() {
        this.updateParameters = new ServiceResourceUpdate();
        return this;
    }

    public ServiceResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServices()
                .update(resourceGroupName, clusterName, applicationName, serviceName, updateParameters, Context.NONE);
        return this;
    }

    public ServiceResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServices()
                .update(resourceGroupName, clusterName, applicationName, serviceName, updateParameters, context);
        return this;
    }

    ServiceResourceImpl(
        ServiceResourceInner innerObject, com.azure.resourcemanager.servicefabric.ServiceFabricManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.clusterName = Utils.getValueFromIdByName(innerObject.id(), "clusters");
        this.applicationName = Utils.getValueFromIdByName(innerObject.id(), "applications");
        this.serviceName = Utils.getValueFromIdByName(innerObject.id(), "services");
    }

    public ServiceResource refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServices()
                .getWithResponse(resourceGroupName, clusterName, applicationName, serviceName, Context.NONE)
                .getValue();
        return this;
    }

    public ServiceResource refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getServices()
                .getWithResponse(resourceGroupName, clusterName, applicationName, serviceName, context)
                .getValue();
        return this;
    }

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

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

    public ServiceResourceImpl withTags(Map<String, String> tags) {
        this.innerModel().withTags(tags);
        return this;
    }

    public ServiceResourceImpl withProperties(ServiceResourceProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public ServiceResourceImpl withProperties(ServiceResourceUpdateProperties properties) {
        this.updateParameters.withProperties(properties);
        return this;
    }
}
