// 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.NetAppAccountInner;
import com.azure.resourcemanager.netapp.models.AccountEncryption;
import com.azure.resourcemanager.netapp.models.ActiveDirectory;
import com.azure.resourcemanager.netapp.models.ManagedServiceIdentity;
import com.azure.resourcemanager.netapp.models.NetAppAccount;
import com.azure.resourcemanager.netapp.models.NetAppAccountPatch;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class NetAppAccountImpl implements NetAppAccount, NetAppAccount.Definition, NetAppAccount.Update {
    private NetAppAccountInner 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 String etag() {
        return this.innerModel().etag();
    }

    public ManagedServiceIdentity identity() {
        return this.innerModel().identity();
    }

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

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

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

    public AccountEncryption encryption() {
        return this.innerModel().encryption();
    }

    public Boolean disableShowmount() {
        return this.innerModel().disableShowmount();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String accountName;

    private NetAppAccountPatch updateBody;

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

    public NetAppAccount create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .createOrUpdate(resourceGroupName, accountName, this.innerModel(), Context.NONE);
        return this;
    }

    public NetAppAccount create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .createOrUpdate(resourceGroupName, accountName, this.innerModel(), context);
        return this;
    }

    NetAppAccountImpl(String name, com.azure.resourcemanager.netapp.NetAppFilesManager serviceManager) {
        this.innerObject = new NetAppAccountInner();
        this.serviceManager = serviceManager;
        this.accountName = name;
    }

    public NetAppAccountImpl update() {
        this.updateBody = new NetAppAccountPatch();
        return this;
    }

    public NetAppAccount apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .update(resourceGroupName, accountName, updateBody, Context.NONE);
        return this;
    }

    public NetAppAccount apply(Context context) {
        this.innerObject =
            serviceManager.serviceClient().getAccounts().update(resourceGroupName, accountName, updateBody, context);
        return this;
    }

    NetAppAccountImpl(
        NetAppAccountInner 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");
    }

    public NetAppAccount refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .getByResourceGroupWithResponse(resourceGroupName, accountName, Context.NONE)
                .getValue();
        return this;
    }

    public NetAppAccount refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .getByResourceGroupWithResponse(resourceGroupName, accountName, context)
                .getValue();
        return this;
    }

    public void renewCredentials() {
        serviceManager.accounts().renewCredentials(resourceGroupName, accountName);
    }

    public void renewCredentials(Context context) {
        serviceManager.accounts().renewCredentials(resourceGroupName, accountName, context);
    }

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

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

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

    public NetAppAccountImpl withIdentity(ManagedServiceIdentity identity) {
        this.innerModel().withIdentity(identity);
        return this;
    }

    public NetAppAccountImpl withActiveDirectories(List<ActiveDirectory> activeDirectories) {
        if (isInCreateMode()) {
            this.innerModel().withActiveDirectories(activeDirectories);
            return this;
        } else {
            this.updateBody.withActiveDirectories(activeDirectories);
            return this;
        }
    }

    public NetAppAccountImpl withEncryption(AccountEncryption encryption) {
        if (isInCreateMode()) {
            this.innerModel().withEncryption(encryption);
            return this;
        } else {
            this.updateBody.withEncryption(encryption);
            return this;
        }
    }

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