// 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.CollectorPolicyInner;
import com.azure.resourcemanager.networkfunction.models.CollectorPolicy;
import com.azure.resourcemanager.networkfunction.models.EmissionPoliciesPropertiesFormat;
import com.azure.resourcemanager.networkfunction.models.IngestionPolicyPropertiesFormat;
import com.azure.resourcemanager.networkfunction.models.ProvisioningState;
import com.azure.resourcemanager.networkfunction.models.TagsObject;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class CollectorPolicyImpl implements CollectorPolicy, CollectorPolicy.Definition, CollectorPolicy.Update {
    private CollectorPolicyInner 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 IngestionPolicyPropertiesFormat ingestionPolicy() {
        return this.innerModel().ingestionPolicy();
    }

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

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

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

    private String resourceGroupName;

    private String azureTrafficCollectorName;

    private String collectorPolicyName;

    private TagsObject updateParameters;

    public CollectorPolicyImpl withExistingAzureTrafficCollector(
        String resourceGroupName, String azureTrafficCollectorName) {
        this.resourceGroupName = resourceGroupName;
        this.azureTrafficCollectorName = azureTrafficCollectorName;
        return this;
    }

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

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

    CollectorPolicyImpl(
        String name, com.azure.resourcemanager.networkfunction.AzureTrafficCollectorManager serviceManager) {
        this.innerObject = new CollectorPolicyInner();
        this.serviceManager = serviceManager;
        this.collectorPolicyName = name;
    }

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

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

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

    CollectorPolicyImpl(
        CollectorPolicyInner 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");
        this.collectorPolicyName = Utils.getValueFromIdByName(innerObject.id(), "collectorPolicies");
    }

    public CollectorPolicy refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCollectorPolicies()
                .getWithResponse(resourceGroupName, azureTrafficCollectorName, collectorPolicyName, Context.NONE)
                .getValue();
        return this;
    }

    public CollectorPolicy refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCollectorPolicies()
                .getWithResponse(resourceGroupName, azureTrafficCollectorName, collectorPolicyName, context)
                .getValue();
        return this;
    }

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

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

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

    public CollectorPolicyImpl withIngestionPolicy(IngestionPolicyPropertiesFormat ingestionPolicy) {
        this.innerModel().withIngestionPolicy(ingestionPolicy);
        return this;
    }

    public CollectorPolicyImpl withEmissionPolicies(List<EmissionPoliciesPropertiesFormat> emissionPolicies) {
        this.innerModel().withEmissionPolicies(emissionPolicies);
        return this;
    }

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