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

package com.azure.resourcemanager.devtestlabs.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.devtestlabs.fluent.models.VirtualNetworkInner;
import com.azure.resourcemanager.devtestlabs.models.ExternalSubnet;
import com.azure.resourcemanager.devtestlabs.models.Subnet;
import com.azure.resourcemanager.devtestlabs.models.SubnetOverride;
import com.azure.resourcemanager.devtestlabs.models.VirtualNetwork;
import com.azure.resourcemanager.devtestlabs.models.VirtualNetworkFragment;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class VirtualNetworkImpl implements VirtualNetwork, VirtualNetwork.Definition, VirtualNetwork.Update {
    private VirtualNetworkInner innerObject;

    private final com.azure.resourcemanager.devtestlabs.DevTestLabsManager 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 List<Subnet> allowedSubnets() {
        List<Subnet> inner = this.innerModel().allowedSubnets();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

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

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

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

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

    public OffsetDateTime createdDate() {
        return this.innerModel().createdDate();
    }

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

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

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

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

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

    private com.azure.resourcemanager.devtestlabs.DevTestLabsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String labName;

    private String name;

    private VirtualNetworkFragment updateVirtualNetwork;

    public VirtualNetworkImpl withExistingLab(String resourceGroupName, String labName) {
        this.resourceGroupName = resourceGroupName;
        this.labName = labName;
        return this;
    }

    public VirtualNetwork create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworks()
                .createOrUpdate(resourceGroupName, labName, name, this.innerModel(), Context.NONE);
        return this;
    }

    public VirtualNetwork create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworks()
                .createOrUpdate(resourceGroupName, labName, name, this.innerModel(), context);
        return this;
    }

    VirtualNetworkImpl(String name, com.azure.resourcemanager.devtestlabs.DevTestLabsManager serviceManager) {
        this.innerObject = new VirtualNetworkInner();
        this.serviceManager = serviceManager;
        this.name = name;
    }

    public VirtualNetworkImpl update() {
        this.updateVirtualNetwork = new VirtualNetworkFragment();
        return this;
    }

    public VirtualNetwork apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworks()
                .updateWithResponse(resourceGroupName, labName, name, updateVirtualNetwork, Context.NONE)
                .getValue();
        return this;
    }

    public VirtualNetwork apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworks()
                .updateWithResponse(resourceGroupName, labName, name, updateVirtualNetwork, context)
                .getValue();
        return this;
    }

    VirtualNetworkImpl(
        VirtualNetworkInner innerObject, com.azure.resourcemanager.devtestlabs.DevTestLabsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.labName = Utils.getValueFromIdByName(innerObject.id(), "labs");
        this.name = Utils.getValueFromIdByName(innerObject.id(), "virtualnetworks");
    }

    public VirtualNetwork refresh() {
        String localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworks()
                .getWithResponse(resourceGroupName, labName, name, localExpand, Context.NONE)
                .getValue();
        return this;
    }

    public VirtualNetwork refresh(Context context) {
        String localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworks()
                .getWithResponse(resourceGroupName, labName, name, localExpand, context)
                .getValue();
        return this;
    }

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

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

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

    public VirtualNetworkImpl withAllowedSubnets(List<Subnet> allowedSubnets) {
        this.innerModel().withAllowedSubnets(allowedSubnets);
        return this;
    }

    public VirtualNetworkImpl withDescription(String description) {
        this.innerModel().withDescription(description);
        return this;
    }

    public VirtualNetworkImpl withExternalProviderResourceId(String externalProviderResourceId) {
        this.innerModel().withExternalProviderResourceId(externalProviderResourceId);
        return this;
    }

    public VirtualNetworkImpl withSubnetOverrides(List<SubnetOverride> subnetOverrides) {
        this.innerModel().withSubnetOverrides(subnetOverrides);
        return this;
    }

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