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

package com.azure.resourcemanager.hybridcontainerservice.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.hybridcontainerservice.fluent.models.VirtualNetworksInner;
import com.azure.resourcemanager.hybridcontainerservice.models.VirtualNetworks;
import com.azure.resourcemanager.hybridcontainerservice.models.VirtualNetworksExtendedLocation;
import com.azure.resourcemanager.hybridcontainerservice.models.VirtualNetworksPatch;
import com.azure.resourcemanager.hybridcontainerservice.models.VirtualNetworksProperties;
import java.util.Collections;
import java.util.Map;

public final class VirtualNetworksImpl implements VirtualNetworks, VirtualNetworks.Definition, VirtualNetworks.Update {
    private VirtualNetworksInner innerObject;

    private final com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager 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 VirtualNetworksProperties properties() {
        return this.innerModel().properties();
    }

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

    public VirtualNetworksExtendedLocation extendedLocation() {
        return this.innerModel().extendedLocation();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String virtualNetworksName;

    private VirtualNetworksPatch updateVirtualNetworks;

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

    public VirtualNetworks create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworksOperations()
                .createOrUpdate(resourceGroupName, virtualNetworksName, this.innerModel(), Context.NONE);
        return this;
    }

    public VirtualNetworks create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworksOperations()
                .createOrUpdate(resourceGroupName, virtualNetworksName, this.innerModel(), context);
        return this;
    }

    VirtualNetworksImpl(
        String name, com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager serviceManager) {
        this.innerObject = new VirtualNetworksInner();
        this.serviceManager = serviceManager;
        this.virtualNetworksName = name;
    }

    public VirtualNetworksImpl update() {
        this.updateVirtualNetworks = new VirtualNetworksPatch();
        return this;
    }

    public VirtualNetworks apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworksOperations()
                .update(resourceGroupName, virtualNetworksName, updateVirtualNetworks, Context.NONE);
        return this;
    }

    public VirtualNetworks apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworksOperations()
                .update(resourceGroupName, virtualNetworksName, updateVirtualNetworks, context);
        return this;
    }

    VirtualNetworksImpl(
        VirtualNetworksInner innerObject,
        com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.virtualNetworksName = Utils.getValueFromIdByName(innerObject.id(), "virtualNetworks");
    }

    public VirtualNetworks refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworksOperations()
                .getByResourceGroupWithResponse(resourceGroupName, virtualNetworksName, Context.NONE)
                .getValue();
        return this;
    }

    public VirtualNetworks refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworksOperations()
                .getByResourceGroupWithResponse(resourceGroupName, virtualNetworksName, context)
                .getValue();
        return this;
    }

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

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

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

    public VirtualNetworksImpl withProperties(VirtualNetworksProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public VirtualNetworksImpl withExtendedLocation(VirtualNetworksExtendedLocation extendedLocation) {
        this.innerModel().withExtendedLocation(extendedLocation);
        return this;
    }

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