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

package com.azure.resourcemanager.hybridcompute.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.hybridcompute.fluent.models.MachineExtensionInner;
import com.azure.resourcemanager.hybridcompute.models.MachineExtension;
import com.azure.resourcemanager.hybridcompute.models.MachineExtensionProperties;
import com.azure.resourcemanager.hybridcompute.models.MachineExtensionUpdate;
import com.azure.resourcemanager.hybridcompute.models.MachineExtensionUpdateProperties;
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.hybridcompute.HybridComputeManager 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 MachineExtensionProperties properties() {
        return this.innerModel().properties();
    }

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

    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.hybridcompute.HybridComputeManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String machineName;

    private String extensionName;

    private MachineExtensionUpdate updateExtensionParameters;

    public MachineExtensionImpl withExistingMachine(String resourceGroupName, String machineName) {
        this.resourceGroupName = resourceGroupName;
        this.machineName = machineName;
        return this;
    }

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

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

    MachineExtensionImpl(String name, com.azure.resourcemanager.hybridcompute.HybridComputeManager 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, machineName, extensionName, updateExtensionParameters, Context.NONE);
        return this;
    }

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

    MachineExtensionImpl(
        MachineExtensionInner innerObject,
        com.azure.resourcemanager.hybridcompute.HybridComputeManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.machineName = Utils.getValueFromIdByName(innerObject.id(), "machines");
        this.extensionName = Utils.getValueFromIdByName(innerObject.id(), "extensions");
    }

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

    public MachineExtension refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMachineExtensions()
                .getWithResponse(resourceGroupName, machineName, 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 withProperties(MachineExtensionProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public MachineExtensionImpl withProperties(MachineExtensionUpdateProperties properties) {
        this.updateExtensionParameters.withProperties(properties);
        return this;
    }

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