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

package com.azure.resourcemanager.connectedvmware.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.connectedvmware.fluent.models.MachineExtensionInner;
import com.azure.resourcemanager.connectedvmware.models.MachineExtension;
import com.azure.resourcemanager.connectedvmware.models.MachineExtensionPropertiesInstanceView;
import com.azure.resourcemanager.connectedvmware.models.MachineExtensionUpdate;
import java.util.Collections;
import java.util.Map;

public final class MachineExtensionImpl
    implements MachineExtension, MachineExtension.Definition, MachineExtension.Update {
    private MachineExtensionInner innerObject;

    private final com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager 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 forceUpdateTag() {
        return this.innerModel().forceUpdateTag();
    }

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

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

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

    public Boolean enableAutomaticUpgrade() {
        return this.innerModel().enableAutomaticUpgrade();
    }

    public Boolean autoUpgradeMinorVersion() {
        return this.innerModel().autoUpgradeMinorVersion();
    }

    public Object settings() {
        return this.innerModel().settings();
    }

    public Object protectedSettings() {
        return this.innerModel().protectedSettings();
    }

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

    public MachineExtensionPropertiesInstanceView instanceView() {
        return this.innerModel().instanceView();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String name;

    private String extensionName;

    private MachineExtensionUpdate updateExtensionParameters;

    public MachineExtensionImpl withExistingVirtualMachine(String resourceGroupName, String name) {
        this.resourceGroupName = resourceGroupName;
        this.name = name;
        return this;
    }

    public MachineExtension create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineExtensions()
                .createOrUpdate(resourceGroupName, name, extensionName, this.innerModel(), Context.NONE);
        return this;
    }

    public MachineExtension create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineExtensions()
                .createOrUpdate(resourceGroupName, name, extensionName, this.innerModel(), context);
        return this;
    }

    MachineExtensionImpl(String name, com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager serviceManager) {
        this.innerObject = new MachineExtensionInner();
        this.serviceManager = serviceManager;
        this.extensionName = name;
    }

    public MachineExtensionImpl update() {
        this.updateExtensionParameters = new MachineExtensionUpdate();
        return this;
    }

    public MachineExtension apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineExtensions()
                .update(resourceGroupName, name, extensionName, updateExtensionParameters, Context.NONE);
        return this;
    }

    public MachineExtension apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineExtensions()
                .update(resourceGroupName, name, extensionName, updateExtensionParameters, context);
        return this;
    }

    MachineExtensionImpl(
        MachineExtensionInner innerObject,
        com.azure.resourcemanager.connectedvmware.ConnectedVMwareManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.name = Utils.getValueFromIdByName(innerObject.id(), "virtualMachines");
        this.extensionName = Utils.getValueFromIdByName(innerObject.id(), "extensions");
    }

    public MachineExtension refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineExtensions()
                .getWithResponse(resourceGroupName, name, extensionName, Context.NONE)
                .getValue();
        return this;
    }

    public MachineExtension refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineExtensions()
                .getWithResponse(resourceGroupName, name, extensionName, context)
                .getValue();
        return this;
    }

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

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

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

    public MachineExtensionImpl withForceUpdateTag(String forceUpdateTag) {
        if (isInCreateMode()) {
            this.innerModel().withForceUpdateTag(forceUpdateTag);
            return this;
        } else {
            this.updateExtensionParameters.withForceUpdateTag(forceUpdateTag);
            return this;
        }
    }

    public MachineExtensionImpl withPublisher(String publisher) {
        if (isInCreateMode()) {
            this.innerModel().withPublisher(publisher);
            return this;
        } else {
            this.updateExtensionParameters.withPublisher(publisher);
            return this;
        }
    }

    public MachineExtensionImpl withTypePropertiesType(String typePropertiesType) {
        this.innerModel().withTypePropertiesType(typePropertiesType);
        return this;
    }

    public MachineExtensionImpl withTypeHandlerVersion(String typeHandlerVersion) {
        if (isInCreateMode()) {
            this.innerModel().withTypeHandlerVersion(typeHandlerVersion);
            return this;
        } else {
            this.updateExtensionParameters.withTypeHandlerVersion(typeHandlerVersion);
            return this;
        }
    }

    public MachineExtensionImpl withEnableAutomaticUpgrade(Boolean enableAutomaticUpgrade) {
        if (isInCreateMode()) {
            this.innerModel().withEnableAutomaticUpgrade(enableAutomaticUpgrade);
            return this;
        } else {
            this.updateExtensionParameters.withEnableAutomaticUpgrade(enableAutomaticUpgrade);
            return this;
        }
    }

    public MachineExtensionImpl withAutoUpgradeMinorVersion(Boolean autoUpgradeMinorVersion) {
        if (isInCreateMode()) {
            this.innerModel().withAutoUpgradeMinorVersion(autoUpgradeMinorVersion);
            return this;
        } else {
            this.updateExtensionParameters.withAutoUpgradeMinorVersion(autoUpgradeMinorVersion);
            return this;
        }
    }

    public MachineExtensionImpl withSettings(Object settings) {
        if (isInCreateMode()) {
            this.innerModel().withSettings(settings);
            return this;
        } else {
            this.updateExtensionParameters.withSettings(settings);
            return this;
        }
    }

    public MachineExtensionImpl withProtectedSettings(Object protectedSettings) {
        if (isInCreateMode()) {
            this.innerModel().withProtectedSettings(protectedSettings);
            return this;
        } else {
            this.updateExtensionParameters.withProtectedSettings(protectedSettings);
            return this;
        }
    }

    public MachineExtensionImpl withInstanceView(MachineExtensionPropertiesInstanceView instanceView) {
        this.innerModel().withInstanceView(instanceView);
        return this;
    }

    public MachineExtensionImpl withType(String type) {
        this.updateExtensionParameters.withType(type);
        return this;
    }

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