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

package com.azure.resourcemanager.agrifood.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.agrifood.fluent.models.FarmBeatsInner;
import com.azure.resourcemanager.agrifood.fluent.models.PrivateEndpointConnectionInner;
import com.azure.resourcemanager.agrifood.models.FarmBeats;
import com.azure.resourcemanager.agrifood.models.FarmBeatsUpdateProperties;
import com.azure.resourcemanager.agrifood.models.FarmBeatsUpdateRequestModel;
import com.azure.resourcemanager.agrifood.models.Identity;
import com.azure.resourcemanager.agrifood.models.PrivateEndpointConnection;
import com.azure.resourcemanager.agrifood.models.ProvisioningState;
import com.azure.resourcemanager.agrifood.models.PublicNetworkAccess;
import com.azure.resourcemanager.agrifood.models.SensorIntegration;
import java.util.Collections;
import java.util.Map;

public final class FarmBeatsImpl implements FarmBeats, FarmBeats.Definition, FarmBeats.Update {
    private FarmBeatsInner innerObject;

    private final com.azure.resourcemanager.agrifood.AgriFoodManager 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 Identity identity() {
        return this.innerModel().identity();
    }

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

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

    public SensorIntegration sensorIntegration() {
        return this.innerModel().sensorIntegration();
    }

    public PublicNetworkAccess publicNetworkAccess() {
        return this.innerModel().publicNetworkAccess();
    }

    public PrivateEndpointConnection privateEndpointConnections() {
        PrivateEndpointConnectionInner inner = this.innerModel().privateEndpointConnections();
        if (inner != null) {
            return new PrivateEndpointConnectionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.agrifood.AgriFoodManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String farmBeatsResourceName;

    private FarmBeatsUpdateRequestModel updateBody;

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

    public FarmBeats create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFarmBeatsModels()
                .createOrUpdateWithResponse(resourceGroupName, farmBeatsResourceName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public FarmBeats create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFarmBeatsModels()
                .createOrUpdateWithResponse(resourceGroupName, farmBeatsResourceName, this.innerModel(), context)
                .getValue();
        return this;
    }

    FarmBeatsImpl(String name, com.azure.resourcemanager.agrifood.AgriFoodManager serviceManager) {
        this.innerObject = new FarmBeatsInner();
        this.serviceManager = serviceManager;
        this.farmBeatsResourceName = name;
    }

    public FarmBeatsImpl update() {
        this.updateBody = new FarmBeatsUpdateRequestModel();
        return this;
    }

    public FarmBeats apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFarmBeatsModels()
                .update(resourceGroupName, farmBeatsResourceName, updateBody, Context.NONE);
        return this;
    }

    public FarmBeats apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFarmBeatsModels()
                .update(resourceGroupName, farmBeatsResourceName, updateBody, context);
        return this;
    }

    FarmBeatsImpl(FarmBeatsInner innerObject, com.azure.resourcemanager.agrifood.AgriFoodManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.farmBeatsResourceName = Utils.getValueFromIdByName(innerObject.id(), "farmBeats");
    }

    public FarmBeats refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFarmBeatsModels()
                .getByResourceGroupWithResponse(resourceGroupName, farmBeatsResourceName, Context.NONE)
                .getValue();
        return this;
    }

    public FarmBeats refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getFarmBeatsModels()
                .getByResourceGroupWithResponse(resourceGroupName, farmBeatsResourceName, context)
                .getValue();
        return this;
    }

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

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

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

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

    public FarmBeatsImpl withSensorIntegration(SensorIntegration sensorIntegration) {
        this.innerModel().withSensorIntegration(sensorIntegration);
        return this;
    }

    public FarmBeatsImpl withPublicNetworkAccess(PublicNetworkAccess publicNetworkAccess) {
        this.innerModel().withPublicNetworkAccess(publicNetworkAccess);
        return this;
    }

    public FarmBeatsImpl withProperties(FarmBeatsUpdateProperties properties) {
        this.updateBody.withProperties(properties);
        return this;
    }

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