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

package com.azure.resourcemanager.maps.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.maps.fluent.models.CreatorInner;
import com.azure.resourcemanager.maps.fluent.models.CreatorProperties;
import com.azure.resourcemanager.maps.models.Creator;
import com.azure.resourcemanager.maps.models.CreatorUpdateParameters;
import java.util.Collections;
import java.util.Map;

public final class CreatorImpl implements Creator, Creator.Definition, Creator.Update {
    private CreatorInner innerObject;

    private final com.azure.resourcemanager.maps.AzureMapsManager 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 CreatorProperties 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 CreatorInner innerModel() {
        return this.innerObject;
    }

    private com.azure.resourcemanager.maps.AzureMapsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String accountName;

    private String creatorName;

    private CreatorUpdateParameters updateCreatorUpdateParameters;

    public CreatorImpl withExistingAccount(String resourceGroupName, String accountName) {
        this.resourceGroupName = resourceGroupName;
        this.accountName = accountName;
        return this;
    }

    public Creator create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCreators()
                .createOrUpdateWithResponse(
                    resourceGroupName, accountName, creatorName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Creator create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCreators()
                .createOrUpdateWithResponse(resourceGroupName, accountName, creatorName, this.innerModel(), context)
                .getValue();
        return this;
    }

    CreatorImpl(String name, com.azure.resourcemanager.maps.AzureMapsManager serviceManager) {
        this.innerObject = new CreatorInner();
        this.serviceManager = serviceManager;
        this.creatorName = name;
    }

    public CreatorImpl update() {
        this.updateCreatorUpdateParameters = new CreatorUpdateParameters();
        return this;
    }

    public Creator apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCreators()
                .updateWithResponse(
                    resourceGroupName, accountName, creatorName, updateCreatorUpdateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Creator apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCreators()
                .updateWithResponse(resourceGroupName, accountName, creatorName, updateCreatorUpdateParameters, context)
                .getValue();
        return this;
    }

    CreatorImpl(CreatorInner innerObject, com.azure.resourcemanager.maps.AzureMapsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.accountName = Utils.getValueFromIdByName(innerObject.id(), "accounts");
        this.creatorName = Utils.getValueFromIdByName(innerObject.id(), "creators");
    }

    public Creator refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCreators()
                .getWithResponse(resourceGroupName, accountName, creatorName, Context.NONE)
                .getValue();
        return this;
    }

    public Creator refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCreators()
                .getWithResponse(resourceGroupName, accountName, creatorName, context)
                .getValue();
        return this;
    }

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

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

    public CreatorImpl withProperties(CreatorProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

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

    public CreatorImpl withStorageUnits(int storageUnits) {
        this.updateCreatorUpdateParameters.withStorageUnits(storageUnits);
        return this;
    }

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