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

package com.azure.resourcemanager.storagemover.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.storagemover.fluent.models.StorageMoverInner;
import com.azure.resourcemanager.storagemover.models.ProvisioningState;
import com.azure.resourcemanager.storagemover.models.StorageMover;
import com.azure.resourcemanager.storagemover.models.StorageMoverUpdateParameters;
import java.util.Collections;
import java.util.Map;

public final class StorageMoverImpl implements StorageMover, StorageMover.Definition, StorageMover.Update {
    private StorageMoverInner innerObject;

    private final com.azure.resourcemanager.storagemover.StorageMoverManager 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 SystemData systemData() {
        return this.innerModel().systemData();
    }

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.storagemover.StorageMoverManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String storageMoverName;

    private StorageMoverUpdateParameters updateStorageMover;

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

    public StorageMover create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageMovers()
                .createOrUpdateWithResponse(resourceGroupName, storageMoverName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public StorageMover create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageMovers()
                .createOrUpdateWithResponse(resourceGroupName, storageMoverName, this.innerModel(), context)
                .getValue();
        return this;
    }

    StorageMoverImpl(String name, com.azure.resourcemanager.storagemover.StorageMoverManager serviceManager) {
        this.innerObject = new StorageMoverInner();
        this.serviceManager = serviceManager;
        this.storageMoverName = name;
    }

    public StorageMoverImpl update() {
        this.updateStorageMover = new StorageMoverUpdateParameters();
        return this;
    }

    public StorageMover apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageMovers()
                .updateWithResponse(resourceGroupName, storageMoverName, updateStorageMover, Context.NONE)
                .getValue();
        return this;
    }

    public StorageMover apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageMovers()
                .updateWithResponse(resourceGroupName, storageMoverName, updateStorageMover, context)
                .getValue();
        return this;
    }

    StorageMoverImpl(
        StorageMoverInner innerObject, com.azure.resourcemanager.storagemover.StorageMoverManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.storageMoverName = Utils.getValueFromIdByName(innerObject.id(), "storageMovers");
    }

    public StorageMover refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageMovers()
                .getByResourceGroupWithResponse(resourceGroupName, storageMoverName, Context.NONE)
                .getValue();
        return this;
    }

    public StorageMover refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getStorageMovers()
                .getByResourceGroupWithResponse(resourceGroupName, storageMoverName, context)
                .getValue();
        return this;
    }

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

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

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

    public StorageMoverImpl withDescription(String description) {
        if (isInCreateMode()) {
            this.innerModel().withDescription(description);
            return this;
        } else {
            this.updateStorageMover.withDescription(description);
            return this;
        }
    }

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