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

package com.azure.resourcemanager.iotcentral.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.iotcentral.fluent.models.AppInner;
import com.azure.resourcemanager.iotcentral.fluent.models.PrivateEndpointConnectionInner;
import com.azure.resourcemanager.iotcentral.models.App;
import com.azure.resourcemanager.iotcentral.models.AppSkuInfo;
import com.azure.resourcemanager.iotcentral.models.AppState;
import com.azure.resourcemanager.iotcentral.models.NetworkRuleSets;
import com.azure.resourcemanager.iotcentral.models.PrivateEndpointConnection;
import com.azure.resourcemanager.iotcentral.models.ProvisioningState;
import com.azure.resourcemanager.iotcentral.models.PublicNetworkAccess;
import com.azure.resourcemanager.iotcentral.models.SystemAssignedServiceIdentity;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class AppImpl implements App, App.Definition, App.Update {
    private AppInner innerObject;

    private final com.azure.resourcemanager.iotcentral.IotCentralManager 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 AppSkuInfo sku() {
        return this.innerModel().sku();
    }

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

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

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

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

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

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

    public AppState state() {
        return this.innerModel().state();
    }

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

    public NetworkRuleSets networkRuleSets() {
        return this.innerModel().networkRuleSets();
    }

    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 Region region() {
        return Region.fromName(this.regionName());
    }

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

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

    private com.azure.resourcemanager.iotcentral.IotCentralManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String resourceName;

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

    public App create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getApps()
                .createOrUpdate(resourceGroupName, resourceName, this.innerModel(), Context.NONE);
        return this;
    }

    public App create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getApps()
                .createOrUpdate(resourceGroupName, resourceName, this.innerModel(), context);
        return this;
    }

    AppImpl(String name, com.azure.resourcemanager.iotcentral.IotCentralManager serviceManager) {
        this.innerObject = new AppInner();
        this.serviceManager = serviceManager;
        this.resourceName = name;
    }

    public AppImpl update() {
        return this;
    }

    public App apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getApps()
                .createOrUpdate(resourceGroupName, resourceName, this.innerModel(), Context.NONE);
        return this;
    }

    public App apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getApps()
                .createOrUpdate(resourceGroupName, resourceName, this.innerModel(), context);
        return this;
    }

    AppImpl(AppInner innerObject, com.azure.resourcemanager.iotcentral.IotCentralManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.resourceName = Utils.getValueFromIdByName(innerObject.id(), "iotApps");
    }

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

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

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

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

    public AppImpl withSku(AppSkuInfo sku) {
        this.innerModel().withSku(sku);
        return this;
    }

    public AppImpl withTags(Map<String, String> tags) {
        this.innerModel().withTags(tags);
        return this;
    }

    public AppImpl withIdentity(SystemAssignedServiceIdentity identity) {
        this.innerModel().withIdentity(identity);
        return this;
    }

    public AppImpl withDisplayName(String displayName) {
        this.innerModel().withDisplayName(displayName);
        return this;
    }

    public AppImpl withSubdomain(String subdomain) {
        this.innerModel().withSubdomain(subdomain);
        return this;
    }

    public AppImpl withTemplate(String template) {
        this.innerModel().withTemplate(template);
        return this;
    }

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

    public AppImpl withNetworkRuleSets(NetworkRuleSets networkRuleSets) {
        this.innerModel().withNetworkRuleSets(networkRuleSets);
        return this;
    }
}
