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

package com.azure.resourcemanager.deviceupdate.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.deviceupdate.fluent.models.AccountInner;
import com.azure.resourcemanager.deviceupdate.fluent.models.PrivateEndpointConnectionInner;
import com.azure.resourcemanager.deviceupdate.models.Account;
import com.azure.resourcemanager.deviceupdate.models.AccountUpdate;
import com.azure.resourcemanager.deviceupdate.models.Location;
import com.azure.resourcemanager.deviceupdate.models.ManagedServiceIdentity;
import com.azure.resourcemanager.deviceupdate.models.PrivateEndpointConnection;
import com.azure.resourcemanager.deviceupdate.models.ProvisioningState;
import com.azure.resourcemanager.deviceupdate.models.PublicNetworkAccess;
import com.azure.resourcemanager.deviceupdate.models.Sku;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class AccountImpl implements Account, Account.Definition, Account.Update {
    private AccountInner innerObject;

    private final com.azure.resourcemanager.deviceupdate.DeviceUpdateManager 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 ManagedServiceIdentity identity() {
        return this.innerModel().identity();
    }

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

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

    public PublicNetworkAccess publicNetworkAccess() {
        return this.innerModel().publicNetworkAccess();
    }

    public List<PrivateEndpointConnection> privateEndpointConnections() {
        List<PrivateEndpointConnectionInner> inner = this.innerModel().privateEndpointConnections();
        if (inner != null) {
            return Collections
                .unmodifiableList(
                    inner
                        .stream()
                        .map(inner1 -> new PrivateEndpointConnectionImpl(inner1, this.manager()))
                        .collect(Collectors.toList()));
        } else {
            return Collections.emptyList();
        }
    }

    public Sku sku() {
        return this.innerModel().sku();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.deviceupdate.DeviceUpdateManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String accountName;

    private AccountUpdate updateAccountUpdatePayload;

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

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

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

    AccountImpl(String name, com.azure.resourcemanager.deviceupdate.DeviceUpdateManager serviceManager) {
        this.innerObject = new AccountInner();
        this.serviceManager = serviceManager;
        this.accountName = name;
    }

    public AccountImpl update() {
        this.updateAccountUpdatePayload = new AccountUpdate();
        return this;
    }

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

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

    AccountImpl(AccountInner innerObject, com.azure.resourcemanager.deviceupdate.DeviceUpdateManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.accountName = Utils.getValueFromIdByName(innerObject.id(), "accounts");
    }

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

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

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

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

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

    public AccountImpl withIdentity(ManagedServiceIdentity identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateAccountUpdatePayload.withIdentity(identity);
            return this;
        }
    }

    public AccountImpl withPublicNetworkAccess(PublicNetworkAccess publicNetworkAccess) {
        this.innerModel().withPublicNetworkAccess(publicNetworkAccess);
        return this;
    }

    public AccountImpl withPrivateEndpointConnections(List<PrivateEndpointConnectionInner> privateEndpointConnections) {
        this.innerModel().withPrivateEndpointConnections(privateEndpointConnections);
        return this;
    }

    public AccountImpl withSku(Sku sku) {
        this.innerModel().withSku(sku);
        return this;
    }

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