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

package com.azure.resourcemanager.graphservices.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.graphservices.fluent.models.AccountResourceInner;
import com.azure.resourcemanager.graphservices.models.AccountPatchResource;
import com.azure.resourcemanager.graphservices.models.AccountResource;
import com.azure.resourcemanager.graphservices.models.AccountResourceProperties;
import com.azure.resourcemanager.graphservices.models.AccountResourceSystemData;
import java.util.Collections;
import java.util.Map;

public final class AccountResourceImpl implements AccountResource, AccountResource.Definition, AccountResource.Update {
    private AccountResourceInner innerObject;

    private final com.azure.resourcemanager.graphservices.GraphServicesManager 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 AccountResourceSystemData systemData() {
        return this.innerModel().systemData();
    }

    public AccountResourceProperties properties() {
        return this.innerModel().properties();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.graphservices.GraphServicesManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String resourceName;

    private AccountPatchResource updateAccountResource;

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

    public AccountResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .createAndUpdate(resourceGroupName, resourceName, this.innerModel(), Context.NONE);
        return this;
    }

    public AccountResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .createAndUpdate(resourceGroupName, resourceName, this.innerModel(), context);
        return this;
    }

    AccountResourceImpl(String name, com.azure.resourcemanager.graphservices.GraphServicesManager serviceManager) {
        this.innerObject = new AccountResourceInner();
        this.serviceManager = serviceManager;
        this.resourceName = name;
    }

    public AccountResourceImpl update() {
        this.updateAccountResource = new AccountPatchResource();
        return this;
    }

    public AccountResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .updateWithResponse(resourceGroupName, resourceName, updateAccountResource, Context.NONE)
                .getValue();
        return this;
    }

    public AccountResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccounts()
                .updateWithResponse(resourceGroupName, resourceName, updateAccountResource, context)
                .getValue();
        return this;
    }

    AccountResourceImpl(
        AccountResourceInner innerObject, com.azure.resourcemanager.graphservices.GraphServicesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.resourceName = Utils.getValueFromIdByName(innerObject.id(), "accounts");
    }

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

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

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

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

    public AccountResourceImpl withProperties(AccountResourceProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

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

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