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

package com.azure.resourcemanager.fluidrelay.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.fluidrelay.fluent.models.FluidRelayServerInner;
import com.azure.resourcemanager.fluidrelay.models.EncryptionProperties;
import com.azure.resourcemanager.fluidrelay.models.FluidRelayEndpoints;
import com.azure.resourcemanager.fluidrelay.models.FluidRelayServer;
import com.azure.resourcemanager.fluidrelay.models.FluidRelayServerKeys;
import com.azure.resourcemanager.fluidrelay.models.FluidRelayServerUpdate;
import com.azure.resourcemanager.fluidrelay.models.Identity;
import com.azure.resourcemanager.fluidrelay.models.ProvisioningState;
import com.azure.resourcemanager.fluidrelay.models.RegenerateKeyRequest;
import com.azure.resourcemanager.fluidrelay.models.StorageSku;
import java.util.Collections;
import java.util.Map;

public final class FluidRelayServerImpl
    implements FluidRelayServer, FluidRelayServer.Definition, FluidRelayServer.Update {
    private FluidRelayServerInner innerObject;

    private final com.azure.resourcemanager.fluidrelay.FluidRelayManager 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 SystemData systemData() {
        return this.innerModel().systemData();
    }

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

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

    public FluidRelayEndpoints fluidRelayEndpoints() {
        return this.innerModel().fluidRelayEndpoints();
    }

    public ProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

    public EncryptionProperties encryption() {
        return this.innerModel().encryption();
    }

    public StorageSku storagesku() {
        return this.innerModel().storagesku();
    }

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

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

    public String resourceGroupName() {
        return resourceGroup;
    }

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

    private com.azure.resourcemanager.fluidrelay.FluidRelayManager manager() {
        return this.serviceManager;
    }

    private String resourceGroup;

    private String fluidRelayServerName;

    private FluidRelayServerUpdate updateResource;

    public FluidRelayServerImpl withExistingResourceGroup(String resourceGroup) {
        this.resourceGroup = resourceGroup;
        return this;
    }

    public FluidRelayServer create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFluidRelayServers()
                .createOrUpdateWithResponse(resourceGroup, fluidRelayServerName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public FluidRelayServer create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFluidRelayServers()
                .createOrUpdateWithResponse(resourceGroup, fluidRelayServerName, this.innerModel(), context)
                .getValue();
        return this;
    }

    FluidRelayServerImpl(String name, com.azure.resourcemanager.fluidrelay.FluidRelayManager serviceManager) {
        this.innerObject = new FluidRelayServerInner();
        this.serviceManager = serviceManager;
        this.fluidRelayServerName = name;
    }

    public FluidRelayServerImpl update() {
        this.updateResource = new FluidRelayServerUpdate();
        return this;
    }

    public FluidRelayServer apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFluidRelayServers()
                .updateWithResponse(resourceGroup, fluidRelayServerName, updateResource, Context.NONE)
                .getValue();
        return this;
    }

    public FluidRelayServer apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFluidRelayServers()
                .updateWithResponse(resourceGroup, fluidRelayServerName, updateResource, context)
                .getValue();
        return this;
    }

    FluidRelayServerImpl(
        FluidRelayServerInner innerObject, com.azure.resourcemanager.fluidrelay.FluidRelayManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroup = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.fluidRelayServerName = Utils.getValueFromIdByName(innerObject.id(), "fluidRelayServers");
    }

    public FluidRelayServer refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFluidRelayServers()
                .getByResourceGroupWithResponse(resourceGroup, fluidRelayServerName, Context.NONE)
                .getValue();
        return this;
    }

    public FluidRelayServer refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFluidRelayServers()
                .getByResourceGroupWithResponse(resourceGroup, fluidRelayServerName, context)
                .getValue();
        return this;
    }

    public FluidRelayServerKeys regenerateKey(RegenerateKeyRequest parameters) {
        return serviceManager.fluidRelayServers().regenerateKey(resourceGroup, fluidRelayServerName, parameters);
    }

    public Response<FluidRelayServerKeys> regenerateKeyWithResponse(RegenerateKeyRequest parameters, Context context) {
        return serviceManager
            .fluidRelayServers()
            .regenerateKeyWithResponse(resourceGroup, fluidRelayServerName, parameters, context);
    }

    public FluidRelayServerKeys listKeys() {
        return serviceManager.fluidRelayServers().listKeys(resourceGroup, fluidRelayServerName);
    }

    public Response<FluidRelayServerKeys> listKeysWithResponse(Context context) {
        return serviceManager.fluidRelayServers().listKeysWithResponse(resourceGroup, fluidRelayServerName, context);
    }

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

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

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

    public FluidRelayServerImpl withIdentity(Identity identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateResource.withIdentity(identity);
            return this;
        }
    }

    public FluidRelayServerImpl withProvisioningState(ProvisioningState provisioningState) {
        this.innerModel().withProvisioningState(provisioningState);
        return this;
    }

    public FluidRelayServerImpl withEncryption(EncryptionProperties encryption) {
        if (isInCreateMode()) {
            this.innerModel().withEncryption(encryption);
            return this;
        } else {
            this.updateResource.withEncryption(encryption);
            return this;
        }
    }

    public FluidRelayServerImpl withStoragesku(StorageSku storagesku) {
        this.innerModel().withStoragesku(storagesku);
        return this;
    }

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