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

package com.azure.resourcemanager.automanage.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.automanage.fluent.models.ConfigurationProfileAssignmentInner;
import com.azure.resourcemanager.automanage.models.ConfigurationProfileAssignment;
import com.azure.resourcemanager.automanage.models.ConfigurationProfileAssignmentProperties;

public final class ConfigurationProfileAssignmentImpl
    implements ConfigurationProfileAssignment,
        ConfigurationProfileAssignment.Definition,
        ConfigurationProfileAssignment.Update {
    private ConfigurationProfileAssignmentInner innerObject;

    private final com.azure.resourcemanager.automanage.AutomanageManager serviceManager;

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

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

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

    public ConfigurationProfileAssignmentProperties properties() {
        return this.innerModel().properties();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.automanage.AutomanageManager manager() {
        return this.serviceManager;
    }

    private String configurationProfileAssignmentName;

    private String resourceGroupName;

    private String vmName;

    public ConfigurationProfileAssignmentImpl withExistingVirtualMachine(String resourceGroupName, String vmName) {
        this.resourceGroupName = resourceGroupName;
        this.vmName = vmName;
        return this;
    }

    public ConfigurationProfileAssignment create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConfigurationProfileAssignments()
                .createOrUpdateWithResponse(
                    configurationProfileAssignmentName, resourceGroupName, vmName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public ConfigurationProfileAssignment create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConfigurationProfileAssignments()
                .createOrUpdateWithResponse(
                    configurationProfileAssignmentName, resourceGroupName, vmName, this.innerModel(), context)
                .getValue();
        return this;
    }

    ConfigurationProfileAssignmentImpl(
        String name, com.azure.resourcemanager.automanage.AutomanageManager serviceManager) {
        this.innerObject = new ConfigurationProfileAssignmentInner();
        this.serviceManager = serviceManager;
        this.configurationProfileAssignmentName = name;
    }

    public ConfigurationProfileAssignmentImpl update() {
        return this;
    }

    public ConfigurationProfileAssignment apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConfigurationProfileAssignments()
                .createOrUpdateWithResponse(
                    configurationProfileAssignmentName, resourceGroupName, vmName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public ConfigurationProfileAssignment apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConfigurationProfileAssignments()
                .createOrUpdateWithResponse(
                    configurationProfileAssignmentName, resourceGroupName, vmName, this.innerModel(), context)
                .getValue();
        return this;
    }

    ConfigurationProfileAssignmentImpl(
        ConfigurationProfileAssignmentInner innerObject,
        com.azure.resourcemanager.automanage.AutomanageManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.configurationProfileAssignmentName =
            Utils.getValueFromIdByName(innerObject.id(), "configurationProfileAssignments");
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.vmName = Utils.getValueFromIdByName(innerObject.id(), "virtualMachines");
    }

    public ConfigurationProfileAssignment refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConfigurationProfileAssignments()
                .getWithResponse(resourceGroupName, configurationProfileAssignmentName, vmName, Context.NONE)
                .getValue();
        return this;
    }

    public ConfigurationProfileAssignment refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConfigurationProfileAssignments()
                .getWithResponse(resourceGroupName, configurationProfileAssignmentName, vmName, context)
                .getValue();
        return this;
    }

    public ConfigurationProfileAssignmentImpl withProperties(ConfigurationProfileAssignmentProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }
}
