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

package com.azure.resourcemanager.netapp.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.netapp.fluent.models.VolumeQuotaRuleInner;
import com.azure.resourcemanager.netapp.models.ProvisioningState;
import com.azure.resourcemanager.netapp.models.Type;
import com.azure.resourcemanager.netapp.models.VolumeQuotaRule;
import com.azure.resourcemanager.netapp.models.VolumeQuotaRulePatch;
import java.util.Collections;
import java.util.Map;

public final class VolumeQuotaRuleImpl implements VolumeQuotaRule, VolumeQuotaRule.Definition, VolumeQuotaRule.Update {
    private VolumeQuotaRuleInner innerObject;

    private final com.azure.resourcemanager.netapp.NetAppFilesManager 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 ProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

    public Long quotaSizeInKiBs() {
        return this.innerModel().quotaSizeInKiBs();
    }

    public Type quotaType() {
        return this.innerModel().quotaType();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.netapp.NetAppFilesManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String accountName;

    private String poolName;

    private String volumeName;

    private String volumeQuotaRuleName;

    private VolumeQuotaRulePatch updateBody;

    public VolumeQuotaRuleImpl withExistingVolume(
        String resourceGroupName, String accountName, String poolName, String volumeName) {
        this.resourceGroupName = resourceGroupName;
        this.accountName = accountName;
        this.poolName = poolName;
        this.volumeName = volumeName;
        return this;
    }

    public VolumeQuotaRule create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVolumeQuotaRules()
                .create(
                    resourceGroupName,
                    accountName,
                    poolName,
                    volumeName,
                    volumeQuotaRuleName,
                    this.innerModel(),
                    Context.NONE);
        return this;
    }

    public VolumeQuotaRule create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVolumeQuotaRules()
                .create(
                    resourceGroupName,
                    accountName,
                    poolName,
                    volumeName,
                    volumeQuotaRuleName,
                    this.innerModel(),
                    context);
        return this;
    }

    VolumeQuotaRuleImpl(String name, com.azure.resourcemanager.netapp.NetAppFilesManager serviceManager) {
        this.innerObject = new VolumeQuotaRuleInner();
        this.serviceManager = serviceManager;
        this.volumeQuotaRuleName = name;
    }

    public VolumeQuotaRuleImpl update() {
        this.updateBody = new VolumeQuotaRulePatch();
        return this;
    }

    public VolumeQuotaRule apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVolumeQuotaRules()
                .update(
                    resourceGroupName,
                    accountName,
                    poolName,
                    volumeName,
                    volumeQuotaRuleName,
                    updateBody,
                    Context.NONE);
        return this;
    }

    public VolumeQuotaRule apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVolumeQuotaRules()
                .update(resourceGroupName, accountName, poolName, volumeName, volumeQuotaRuleName, updateBody, context);
        return this;
    }

    VolumeQuotaRuleImpl(
        VolumeQuotaRuleInner innerObject, com.azure.resourcemanager.netapp.NetAppFilesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.accountName = Utils.getValueFromIdByName(innerObject.id(), "netAppAccounts");
        this.poolName = Utils.getValueFromIdByName(innerObject.id(), "capacityPools");
        this.volumeName = Utils.getValueFromIdByName(innerObject.id(), "volumes");
        this.volumeQuotaRuleName = Utils.getValueFromIdByName(innerObject.id(), "volumeQuotaRules");
    }

    public VolumeQuotaRule refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVolumeQuotaRules()
                .getWithResponse(
                    resourceGroupName, accountName, poolName, volumeName, volumeQuotaRuleName, Context.NONE)
                .getValue();
        return this;
    }

    public VolumeQuotaRule refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getVolumeQuotaRules()
                .getWithResponse(resourceGroupName, accountName, poolName, volumeName, volumeQuotaRuleName, context)
                .getValue();
        return this;
    }

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

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

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

    public VolumeQuotaRuleImpl withQuotaSizeInKiBs(Long quotaSizeInKiBs) {
        if (isInCreateMode()) {
            this.innerModel().withQuotaSizeInKiBs(quotaSizeInKiBs);
            return this;
        } else {
            this.updateBody.withQuotaSizeInKiBs(quotaSizeInKiBs);
            return this;
        }
    }

    public VolumeQuotaRuleImpl withQuotaType(Type quotaType) {
        this.innerModel().withQuotaType(quotaType);
        return this;
    }

    public VolumeQuotaRuleImpl withQuotaTarget(String quotaTarget) {
        this.innerModel().withQuotaTarget(quotaTarget);
        return this;
    }

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