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

package com.azure.resourcemanager.nginx.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.nginx.fluent.models.NginxDeploymentInner;
import com.azure.resourcemanager.nginx.models.IdentityProperties;
import com.azure.resourcemanager.nginx.models.NginxDeployment;
import com.azure.resourcemanager.nginx.models.NginxDeploymentProperties;
import com.azure.resourcemanager.nginx.models.NginxDeploymentUpdateParameters;
import com.azure.resourcemanager.nginx.models.NginxDeploymentUpdateProperties;
import com.azure.resourcemanager.nginx.models.ResourceSku;
import java.util.Collections;
import java.util.Map;

public final class NginxDeploymentImpl implements NginxDeployment, NginxDeployment.Definition, NginxDeployment.Update {
    private NginxDeploymentInner innerObject;

    private final com.azure.resourcemanager.nginx.NginxManager 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 IdentityProperties identity() {
        return this.innerModel().identity();
    }

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

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

    private com.azure.resourcemanager.nginx.NginxManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String deploymentName;

    private NginxDeploymentUpdateParameters updateBody;

    public NginxDeploymentImpl withExistingResourceGroup(String resourceGroupName) {
        this.resourceGroupName = resourceGroupName;
        return this;
    }

    public NginxDeployment create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeployments()
                .createOrUpdate(resourceGroupName, deploymentName, this.innerModel(), Context.NONE);
        return this;
    }

    public NginxDeployment create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeployments()
                .createOrUpdate(resourceGroupName, deploymentName, this.innerModel(), context);
        return this;
    }

    NginxDeploymentImpl(String name, com.azure.resourcemanager.nginx.NginxManager serviceManager) {
        this.innerObject = new NginxDeploymentInner();
        this.serviceManager = serviceManager;
        this.deploymentName = name;
    }

    public NginxDeploymentImpl update() {
        this.updateBody = new NginxDeploymentUpdateParameters();
        return this;
    }

    public NginxDeployment apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeployments()
                .update(resourceGroupName, deploymentName, updateBody, Context.NONE);
        return this;
    }

    public NginxDeployment apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeployments()
                .update(resourceGroupName, deploymentName, updateBody, context);
        return this;
    }

    NginxDeploymentImpl(NginxDeploymentInner innerObject, com.azure.resourcemanager.nginx.NginxManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.deploymentName = Utils.getValueFromIdByName(innerObject.id(), "nginxDeployments");
    }

    public NginxDeployment refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeployments()
                .getByResourceGroupWithResponse(resourceGroupName, deploymentName, Context.NONE)
                .getValue();
        return this;
    }

    public NginxDeployment refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDeployments()
                .getByResourceGroupWithResponse(resourceGroupName, deploymentName, context)
                .getValue();
        return this;
    }

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

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

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

    public NginxDeploymentImpl withIdentity(IdentityProperties identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateBody.withIdentity(identity);
            return this;
        }
    }

    public NginxDeploymentImpl withProperties(NginxDeploymentProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public NginxDeploymentImpl withSku(ResourceSku sku) {
        if (isInCreateMode()) {
            this.innerModel().withSku(sku);
            return this;
        } else {
            this.updateBody.withSku(sku);
            return this;
        }
    }

    public NginxDeploymentImpl withProperties(NginxDeploymentUpdateProperties properties) {
        this.updateBody.withProperties(properties);
        return this;
    }

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