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

package com.azure.resourcemanager.managednetworkfabric.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.managednetworkfabric.fluent.models.NetworkPacketBrokerInner;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkPacketBroker;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkPacketBrokerPatch;
import com.azure.resourcemanager.managednetworkfabric.models.ProvisioningState;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class NetworkPacketBrokerImpl
    implements NetworkPacketBroker, NetworkPacketBroker.Definition, NetworkPacketBroker.Update {
    private NetworkPacketBrokerInner innerObject;

    private final com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager 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 String networkFabricId() {
        return this.innerModel().networkFabricId();
    }

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

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

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

    public List<String> neighborGroupIds() {
        List<String> inner = this.innerModel().neighborGroupIds();
        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 NetworkPacketBrokerInner innerModel() {
        return this.innerObject;
    }

    private com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String networkPacketBrokerName;

    private NetworkPacketBrokerPatch updateBody;

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

    public NetworkPacketBroker create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkPacketBrokers()
                .create(resourceGroupName, networkPacketBrokerName, this.innerModel(), Context.NONE);
        return this;
    }

    public NetworkPacketBroker create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkPacketBrokers()
                .create(resourceGroupName, networkPacketBrokerName, this.innerModel(), context);
        return this;
    }

    NetworkPacketBrokerImpl(
        String name, com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager) {
        this.innerObject = new NetworkPacketBrokerInner();
        this.serviceManager = serviceManager;
        this.networkPacketBrokerName = name;
    }

    public NetworkPacketBrokerImpl update() {
        this.updateBody = new NetworkPacketBrokerPatch();
        return this;
    }

    public NetworkPacketBroker apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkPacketBrokers()
                .update(resourceGroupName, networkPacketBrokerName, updateBody, Context.NONE);
        return this;
    }

    public NetworkPacketBroker apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkPacketBrokers()
                .update(resourceGroupName, networkPacketBrokerName, updateBody, context);
        return this;
    }

    NetworkPacketBrokerImpl(
        NetworkPacketBrokerInner innerObject,
        com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.networkPacketBrokerName = Utils.getValueFromIdByName(innerObject.id(), "networkPacketBrokers");
    }

    public NetworkPacketBroker refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkPacketBrokers()
                .getByResourceGroupWithResponse(resourceGroupName, networkPacketBrokerName, Context.NONE)
                .getValue();
        return this;
    }

    public NetworkPacketBroker refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkPacketBrokers()
                .getByResourceGroupWithResponse(resourceGroupName, networkPacketBrokerName, context)
                .getValue();
        return this;
    }

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

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

    public NetworkPacketBrokerImpl withNetworkFabricId(String networkFabricId) {
        this.innerModel().withNetworkFabricId(networkFabricId);
        return this;
    }

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

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