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

package com.azure.resourcemanager.customerinsights.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.customerinsights.fluent.models.HubInner;
import com.azure.resourcemanager.customerinsights.models.Hub;
import com.azure.resourcemanager.customerinsights.models.HubBillingInfoFormat;
import java.util.Collections;
import java.util.Map;

public final class HubImpl implements Hub, Hub.Definition, Hub.Update {
    private HubInner innerObject;

    private final com.azure.resourcemanager.customerinsights.CustomerInsightsManager 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 apiEndpoint() {
        return this.innerModel().apiEndpoint();
    }

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

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

    public Integer tenantFeatures() {
        return this.innerModel().tenantFeatures();
    }

    public HubBillingInfoFormat hubBillingInfo() {
        return this.innerModel().hubBillingInfo();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.customerinsights.CustomerInsightsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String hubName;

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

    public Hub create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHubs()
                .createOrUpdateWithResponse(resourceGroupName, hubName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Hub create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHubs()
                .createOrUpdateWithResponse(resourceGroupName, hubName, this.innerModel(), context)
                .getValue();
        return this;
    }

    HubImpl(String name, com.azure.resourcemanager.customerinsights.CustomerInsightsManager serviceManager) {
        this.innerObject = new HubInner();
        this.serviceManager = serviceManager;
        this.hubName = name;
    }

    public HubImpl update() {
        return this;
    }

    public Hub apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHubs()
                .updateWithResponse(resourceGroupName, hubName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Hub apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHubs()
                .updateWithResponse(resourceGroupName, hubName, this.innerModel(), context)
                .getValue();
        return this;
    }

    HubImpl(HubInner innerObject, com.azure.resourcemanager.customerinsights.CustomerInsightsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.hubName = Utils.getValueFromIdByName(innerObject.id(), "hubs");
    }

    public Hub refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHubs()
                .getByResourceGroupWithResponse(resourceGroupName, hubName, Context.NONE)
                .getValue();
        return this;
    }

    public Hub refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getHubs()
                .getByResourceGroupWithResponse(resourceGroupName, hubName, context)
                .getValue();
        return this;
    }

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

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

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

    public HubImpl withTenantFeatures(Integer tenantFeatures) {
        this.innerModel().withTenantFeatures(tenantFeatures);
        return this;
    }

    public HubImpl withHubBillingInfo(HubBillingInfoFormat hubBillingInfo) {
        this.innerModel().withHubBillingInfo(hubBillingInfo);
        return this;
    }
}
