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

package com.azure.resourcemanager.azureadexternalidentities.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.azureadexternalidentities.fluent.models.B2CTenantResourceInner;
import com.azure.resourcemanager.azureadexternalidentities.models.B2CResourceSku;
import com.azure.resourcemanager.azureadexternalidentities.models.B2CTenantResource;
import com.azure.resourcemanager.azureadexternalidentities.models.B2CTenantResourcePropertiesBillingConfig;
import com.azure.resourcemanager.azureadexternalidentities.models.B2CTenantUpdateRequest;
import com.azure.resourcemanager.azureadexternalidentities.models.CreateTenantRequestBody;
import java.util.Collections;
import java.util.Map;

public final class B2CTenantResourceImpl
    implements B2CTenantResource, B2CTenantResource.Definition, B2CTenantResource.Update {
    private B2CTenantResourceInner innerObject;

    private final com.azure.resourcemanager.azureadexternalidentities.ExternalIdentitiesConfigurationManager
        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 B2CResourceSku sku() {
        return this.innerModel().sku();
    }

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

    public B2CTenantResourcePropertiesBillingConfig billingConfig() {
        return this.innerModel().billingConfig();
    }

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

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

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

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

    private com.azure.resourcemanager.azureadexternalidentities.ExternalIdentitiesConfigurationManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String resourceName;

    private CreateTenantRequestBody createCreateTenantRequestBody;

    private B2CTenantUpdateRequest updateUpdateTenantRequestBody;

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

    public B2CTenantResource create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getB2CTenants()
                .create(resourceGroupName, resourceName, createCreateTenantRequestBody, Context.NONE);
        return this;
    }

    public B2CTenantResource create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getB2CTenants()
                .create(resourceGroupName, resourceName, createCreateTenantRequestBody, context);
        return this;
    }

    B2CTenantResourceImpl(
        String name,
        com.azure.resourcemanager.azureadexternalidentities.ExternalIdentitiesConfigurationManager serviceManager) {
        this.innerObject = new B2CTenantResourceInner();
        this.serviceManager = serviceManager;
        this.resourceName = name;
        this.createCreateTenantRequestBody = new CreateTenantRequestBody();
    }

    public B2CTenantResourceImpl update() {
        this.updateUpdateTenantRequestBody = new B2CTenantUpdateRequest();
        return this;
    }

    public B2CTenantResource apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getB2CTenants()
                .updateWithResponse(resourceGroupName, resourceName, updateUpdateTenantRequestBody, Context.NONE)
                .getValue();
        return this;
    }

    public B2CTenantResource apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getB2CTenants()
                .updateWithResponse(resourceGroupName, resourceName, updateUpdateTenantRequestBody, context)
                .getValue();
        return this;
    }

    B2CTenantResourceImpl(
        B2CTenantResourceInner innerObject,
        com.azure.resourcemanager.azureadexternalidentities.ExternalIdentitiesConfigurationManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.resourceName = Utils.getValueFromIdByName(innerObject.id(), "b2cDirectories");
    }

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

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

    public B2CTenantResourceImpl withRegion(Region location) {
        this.createCreateTenantRequestBody.withLocation(location.toString());
        return this;
    }

    public B2CTenantResourceImpl withRegion(String location) {
        this.createCreateTenantRequestBody.withLocation(location);
        return this;
    }

    public B2CTenantResourceImpl withSku(B2CResourceSku sku) {
        if (isInCreateMode()) {
            this.createCreateTenantRequestBody.withSku(sku);
            return this;
        } else {
            this.updateUpdateTenantRequestBody.withSku(sku);
            return this;
        }
    }

    public B2CTenantResourceImpl withTags(Map<String, String> tags) {
        if (isInCreateMode()) {
            this.createCreateTenantRequestBody.withTags(tags);
            return this;
        } else {
            this.updateUpdateTenantRequestBody.withTags(tags);
            return this;
        }
    }

    public B2CTenantResourceImpl withDisplayName(String displayName) {
        this.createCreateTenantRequestBody.withDisplayName(displayName);
        return this;
    }

    public B2CTenantResourceImpl withCountryCode(String countryCode) {
        this.createCreateTenantRequestBody.withCountryCode(countryCode);
        return this;
    }

    public B2CTenantResourceImpl withBillingConfig(B2CTenantResourcePropertiesBillingConfig billingConfig) {
        this.updateUpdateTenantRequestBody.withBillingConfig(billingConfig);
        return this;
    }

    public B2CTenantResourceImpl withTenantId(String tenantId) {
        this.updateUpdateTenantRequestBody.withTenantId(tenantId);
        return this;
    }

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