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

package com.azure.resourcemanager.datalakestore.implementation;

import com.azure.core.util.Context;
import com.azure.resourcemanager.datalakestore.fluent.models.VirtualNetworkRuleInner;
import com.azure.resourcemanager.datalakestore.models.CreateOrUpdateVirtualNetworkRuleParameters;
import com.azure.resourcemanager.datalakestore.models.UpdateVirtualNetworkRuleParameters;
import com.azure.resourcemanager.datalakestore.models.VirtualNetworkRule;

public final class VirtualNetworkRuleImpl
    implements VirtualNetworkRule, VirtualNetworkRule.Definition, VirtualNetworkRule.Update {
    private VirtualNetworkRuleInner innerObject;

    private final com.azure.resourcemanager.datalakestore.DataLakeStoreManager serviceManager;

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.datalakestore.DataLakeStoreManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String accountName;

    private String virtualNetworkRuleName;

    private CreateOrUpdateVirtualNetworkRuleParameters createParameters;

    private UpdateVirtualNetworkRuleParameters updateParameters;

    public VirtualNetworkRuleImpl withExistingAccount(String resourceGroupName, String accountName) {
        this.resourceGroupName = resourceGroupName;
        this.accountName = accountName;
        return this;
    }

    public VirtualNetworkRule create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworkRules()
                .createOrUpdateWithResponse(
                    resourceGroupName, accountName, virtualNetworkRuleName, createParameters, Context.NONE)
                .getValue();
        return this;
    }

    public VirtualNetworkRule create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworkRules()
                .createOrUpdateWithResponse(
                    resourceGroupName, accountName, virtualNetworkRuleName, createParameters, context)
                .getValue();
        return this;
    }

    VirtualNetworkRuleImpl(String name, com.azure.resourcemanager.datalakestore.DataLakeStoreManager serviceManager) {
        this.innerObject = new VirtualNetworkRuleInner();
        this.serviceManager = serviceManager;
        this.virtualNetworkRuleName = name;
        this.createParameters = new CreateOrUpdateVirtualNetworkRuleParameters();
    }

    public VirtualNetworkRuleImpl update() {
        this.updateParameters = new UpdateVirtualNetworkRuleParameters();
        return this;
    }

    public VirtualNetworkRule apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworkRules()
                .updateWithResponse(
                    resourceGroupName, accountName, virtualNetworkRuleName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public VirtualNetworkRule apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworkRules()
                .updateWithResponse(resourceGroupName, accountName, virtualNetworkRuleName, updateParameters, context)
                .getValue();
        return this;
    }

    VirtualNetworkRuleImpl(
        VirtualNetworkRuleInner innerObject,
        com.azure.resourcemanager.datalakestore.DataLakeStoreManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.accountName = Utils.getValueFromIdByName(innerObject.id(), "accounts");
        this.virtualNetworkRuleName = Utils.getValueFromIdByName(innerObject.id(), "virtualNetworkRules");
    }

    public VirtualNetworkRule refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworkRules()
                .getWithResponse(resourceGroupName, accountName, virtualNetworkRuleName, Context.NONE)
                .getValue();
        return this;
    }

    public VirtualNetworkRule refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVirtualNetworkRules()
                .getWithResponse(resourceGroupName, accountName, virtualNetworkRuleName, context)
                .getValue();
        return this;
    }

    public VirtualNetworkRuleImpl withSubnetId(String subnetId) {
        if (isInCreateMode()) {
            this.createParameters.withSubnetId(subnetId);
            return this;
        } else {
            this.updateParameters.withSubnetId(subnetId);
            return this;
        }
    }

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