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

package com.azure.resourcemanager.networkfunction.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.networkfunction.fluent.models.AzureTrafficCollectorInner;
import com.azure.resourcemanager.networkfunction.models.AzureTrafficCollector;
import com.azure.resourcemanager.networkfunction.models.ProvisioningState;
import com.azure.resourcemanager.networkfunction.models.ResourceReference;
import com.azure.resourcemanager.networkfunction.models.TagsObject;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class AzureTrafficCollectorImpl
    implements AzureTrafficCollector, AzureTrafficCollector.Definition, AzureTrafficCollector.Update {
    private AzureTrafficCollectorInner innerObject;

    private final com.azure.resourcemanager.networkfunction.AzureTrafficCollectorManager 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 String etag() {
        return this.innerModel().etag();
    }

    public SystemData systemData() {
        return this.innerModel().systemData();
    }

    public List<ResourceReference> collectorPolicies() {
        List<ResourceReference> inner = this.innerModel().collectorPolicies();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

    public ResourceReference virtualHub() {
        return this.innerModel().virtualHub();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.networkfunction.AzureTrafficCollectorManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String azureTrafficCollectorName;

    private TagsObject updateParameters;

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

    public AzureTrafficCollector create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAzureTrafficCollectors()
                .createOrUpdate(resourceGroupName, azureTrafficCollectorName, this.innerModel(), Context.NONE);
        return this;
    }

    public AzureTrafficCollector create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAzureTrafficCollectors()
                .createOrUpdate(resourceGroupName, azureTrafficCollectorName, this.innerModel(), context);
        return this;
    }

    AzureTrafficCollectorImpl(
        String name, com.azure.resourcemanager.networkfunction.AzureTrafficCollectorManager serviceManager) {
        this.innerObject = new AzureTrafficCollectorInner();
        this.serviceManager = serviceManager;
        this.azureTrafficCollectorName = name;
    }

    public AzureTrafficCollectorImpl update() {
        this.updateParameters = new TagsObject();
        return this;
    }

    public AzureTrafficCollector apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAzureTrafficCollectors()
                .updateTagsWithResponse(resourceGroupName, azureTrafficCollectorName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public AzureTrafficCollector apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAzureTrafficCollectors()
                .updateTagsWithResponse(resourceGroupName, azureTrafficCollectorName, updateParameters, context)
                .getValue();
        return this;
    }

    AzureTrafficCollectorImpl(
        AzureTrafficCollectorInner innerObject,
        com.azure.resourcemanager.networkfunction.AzureTrafficCollectorManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.azureTrafficCollectorName = Utils.getValueFromIdByName(innerObject.id(), "azureTrafficCollectors");
    }

    public AzureTrafficCollector refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAzureTrafficCollectors()
                .getByResourceGroupWithResponse(resourceGroupName, azureTrafficCollectorName, Context.NONE)
                .getValue();
        return this;
    }

    public AzureTrafficCollector refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAzureTrafficCollectors()
                .getByResourceGroupWithResponse(resourceGroupName, azureTrafficCollectorName, context)
                .getValue();
        return this;
    }

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

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

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

    public AzureTrafficCollectorImpl withVirtualHub(ResourceReference virtualHub) {
        this.innerModel().withVirtualHub(virtualHub);
        return this;
    }

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