// 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.NetworkFabricInner;
import com.azure.resourcemanager.managednetworkfabric.models.ManagementNetworkConfiguration;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkFabric;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkFabricOperationalState;
import com.azure.resourcemanager.managednetworkfabric.models.NetworkFabricPatchParameters;
import com.azure.resourcemanager.managednetworkfabric.models.ProvisioningState;
import com.azure.resourcemanager.managednetworkfabric.models.TerminalServerConfiguration;
import com.azure.resourcemanager.managednetworkfabric.models.TerminalServerPatchableProperties;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class NetworkFabricImpl implements NetworkFabric, NetworkFabric.Definition, NetworkFabric.Update {
    private NetworkFabricInner 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 networkFabricSku() {
        return this.innerModel().networkFabricSku();
    }

    public int rackCount() {
        return this.innerModel().rackCount();
    }

    public int serverCountPerRack() {
        return this.innerModel().serverCountPerRack();
    }

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

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

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

    public int fabricAsn() {
        return this.innerModel().fabricAsn();
    }

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

    public TerminalServerConfiguration terminalServerConfiguration() {
        return this.innerModel().terminalServerConfiguration();
    }

    public ManagementNetworkConfiguration managementNetworkConfiguration() {
        return this.innerModel().managementNetworkConfiguration();
    }

    public NetworkFabricOperationalState operationalState() {
        return this.innerModel().operationalState();
    }

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

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String networkFabricName;

    private NetworkFabricPatchParameters updateBody;

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

    public NetworkFabric create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkFabrics()
                .create(resourceGroupName, networkFabricName, this.innerModel(), Context.NONE);
        return this;
    }

    public NetworkFabric create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkFabrics()
                .create(resourceGroupName, networkFabricName, this.innerModel(), context);
        return this;
    }

    NetworkFabricImpl(
        String name, com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager) {
        this.innerObject = new NetworkFabricInner();
        this.serviceManager = serviceManager;
        this.networkFabricName = name;
    }

    public NetworkFabricImpl update() {
        this.updateBody = new NetworkFabricPatchParameters();
        return this;
    }

    public NetworkFabric apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkFabrics()
                .update(resourceGroupName, networkFabricName, updateBody, Context.NONE);
        return this;
    }

    public NetworkFabric apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkFabrics()
                .update(resourceGroupName, networkFabricName, updateBody, context);
        return this;
    }

    NetworkFabricImpl(
        NetworkFabricInner innerObject,
        com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.networkFabricName = Utils.getValueFromIdByName(innerObject.id(), "networkFabrics");
    }

    public NetworkFabric refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkFabrics()
                .getByResourceGroupWithResponse(resourceGroupName, networkFabricName, Context.NONE)
                .getValue();
        return this;
    }

    public NetworkFabric refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getNetworkFabrics()
                .getByResourceGroupWithResponse(resourceGroupName, networkFabricName, context)
                .getValue();
        return this;
    }

    public void provision() {
        serviceManager.networkFabrics().provision(resourceGroupName, networkFabricName);
    }

    public void provision(Context context) {
        serviceManager.networkFabrics().provision(resourceGroupName, networkFabricName, context);
    }

    public void deprovision() {
        serviceManager.networkFabrics().deprovision(resourceGroupName, networkFabricName);
    }

    public void deprovision(Context context) {
        serviceManager.networkFabrics().deprovision(resourceGroupName, networkFabricName, context);
    }

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

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

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

    public NetworkFabricImpl withNetworkFabricSku(String networkFabricSku) {
        this.innerModel().withNetworkFabricSku(networkFabricSku);
        return this;
    }

    public NetworkFabricImpl withRackCount(int rackCount) {
        this.innerModel().withRackCount(rackCount);
        return this;
    }

    public NetworkFabricImpl withServerCountPerRack(int serverCountPerRack) {
        this.innerModel().withServerCountPerRack(serverCountPerRack);
        return this;
    }

    public NetworkFabricImpl withIpv4Prefix(String ipv4Prefix) {
        this.innerModel().withIpv4Prefix(ipv4Prefix);
        return this;
    }

    public NetworkFabricImpl withIpv6Prefix(String ipv6Prefix) {
        this.innerModel().withIpv6Prefix(ipv6Prefix);
        return this;
    }

    public NetworkFabricImpl withFabricAsn(int fabricAsn) {
        this.innerModel().withFabricAsn(fabricAsn);
        return this;
    }

    public NetworkFabricImpl withNetworkFabricControllerId(String networkFabricControllerId) {
        this.innerModel().withNetworkFabricControllerId(networkFabricControllerId);
        return this;
    }

    public NetworkFabricImpl withTerminalServerConfiguration(TerminalServerConfiguration terminalServerConfiguration) {
        this.innerModel().withTerminalServerConfiguration(terminalServerConfiguration);
        return this;
    }

    public NetworkFabricImpl withManagementNetworkConfiguration(
        ManagementNetworkConfiguration managementNetworkConfiguration) {
        this.innerModel().withManagementNetworkConfiguration(managementNetworkConfiguration);
        return this;
    }

    public NetworkFabricImpl withAnnotation(String annotation) {
        if (isInCreateMode()) {
            this.innerModel().withAnnotation(annotation);
            return this;
        } else {
            this.updateBody.withAnnotation(annotation);
            return this;
        }
    }

    public NetworkFabricImpl withTerminalServerConfiguration(
        TerminalServerPatchableProperties terminalServerConfiguration) {
        this.updateBody.withTerminalServerConfiguration(terminalServerConfiguration);
        return this;
    }

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