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

package com.azure.resourcemanager.hybridcontainerservice.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.hybridcontainerservice.fluent.models.StorageSpacesInner;
import com.azure.resourcemanager.hybridcontainerservice.models.StorageSpaces;
import com.azure.resourcemanager.hybridcontainerservice.models.StorageSpacesExtendedLocation;
import com.azure.resourcemanager.hybridcontainerservice.models.StorageSpacesPatch;
import com.azure.resourcemanager.hybridcontainerservice.models.StorageSpacesProperties;
import java.util.Collections;
import java.util.Map;

public final class StorageSpacesImpl implements StorageSpaces, StorageSpaces.Definition, StorageSpaces.Update {
    private StorageSpacesInner innerObject;

    private final com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager 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 StorageSpacesProperties properties() {
        return this.innerModel().properties();
    }

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

    public StorageSpacesExtendedLocation extendedLocation() {
        return this.innerModel().extendedLocation();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String storageSpacesName;

    private StorageSpacesPatch updateStorageSpaces;

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

    public StorageSpaces create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageSpacesOperations()
                .createOrUpdate(resourceGroupName, storageSpacesName, this.innerModel(), Context.NONE);
        return this;
    }

    public StorageSpaces create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageSpacesOperations()
                .createOrUpdate(resourceGroupName, storageSpacesName, this.innerModel(), context);
        return this;
    }

    StorageSpacesImpl(
        String name, com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager serviceManager) {
        this.innerObject = new StorageSpacesInner();
        this.serviceManager = serviceManager;
        this.storageSpacesName = name;
    }

    public StorageSpacesImpl update() {
        this.updateStorageSpaces = new StorageSpacesPatch();
        return this;
    }

    public StorageSpaces apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageSpacesOperations()
                .update(resourceGroupName, storageSpacesName, updateStorageSpaces, Context.NONE);
        return this;
    }

    public StorageSpaces apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageSpacesOperations()
                .update(resourceGroupName, storageSpacesName, updateStorageSpaces, context);
        return this;
    }

    StorageSpacesImpl(
        StorageSpacesInner innerObject,
        com.azure.resourcemanager.hybridcontainerservice.HybridContainerServiceManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.storageSpacesName = Utils.getValueFromIdByName(innerObject.id(), "storageSpaces");
    }

    public StorageSpaces refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageSpacesOperations()
                .getByResourceGroupWithResponse(resourceGroupName, storageSpacesName, Context.NONE)
                .getValue();
        return this;
    }

    public StorageSpaces refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageSpacesOperations()
                .getByResourceGroupWithResponse(resourceGroupName, storageSpacesName, context)
                .getValue();
        return this;
    }

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

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

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

    public StorageSpacesImpl withProperties(StorageSpacesProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public StorageSpacesImpl withExtendedLocation(StorageSpacesExtendedLocation extendedLocation) {
        this.innerModel().withExtendedLocation(extendedLocation);
        return this;
    }

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