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

package com.azure.resourcemanager.sqlvirtualmachine.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.sqlvirtualmachine.fluent.models.SqlVirtualMachineInner;
import com.azure.resourcemanager.sqlvirtualmachine.models.AssessmentSettings;
import com.azure.resourcemanager.sqlvirtualmachine.models.AutoBackupSettings;
import com.azure.resourcemanager.sqlvirtualmachine.models.AutoPatchingSettings;
import com.azure.resourcemanager.sqlvirtualmachine.models.KeyVaultCredentialSettings;
import com.azure.resourcemanager.sqlvirtualmachine.models.LeastPrivilegeMode;
import com.azure.resourcemanager.sqlvirtualmachine.models.ResourceIdentity;
import com.azure.resourcemanager.sqlvirtualmachine.models.ServerConfigurationsManagementSettings;
import com.azure.resourcemanager.sqlvirtualmachine.models.SqlImageSku;
import com.azure.resourcemanager.sqlvirtualmachine.models.SqlManagementMode;
import com.azure.resourcemanager.sqlvirtualmachine.models.SqlServerLicenseType;
import com.azure.resourcemanager.sqlvirtualmachine.models.SqlVirtualMachine;
import com.azure.resourcemanager.sqlvirtualmachine.models.SqlVirtualMachineUpdate;
import com.azure.resourcemanager.sqlvirtualmachine.models.StorageConfigurationSettings;
import com.azure.resourcemanager.sqlvirtualmachine.models.TroubleshootingStatus;
import com.azure.resourcemanager.sqlvirtualmachine.models.WsfcDomainCredentials;
import java.util.Collections;
import java.util.Map;

public final class SqlVirtualMachineImpl
    implements SqlVirtualMachine, SqlVirtualMachine.Definition, SqlVirtualMachine.Update {
    private SqlVirtualMachineInner innerObject;

    private final com.azure.resourcemanager.sqlvirtualmachine.SqlVirtualMachineManager 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 ResourceIdentity identity() {
        return this.innerModel().identity();
    }

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

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

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

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

    public SqlServerLicenseType sqlServerLicenseType() {
        return this.innerModel().sqlServerLicenseType();
    }

    public SqlManagementMode sqlManagement() {
        return this.innerModel().sqlManagement();
    }

    public LeastPrivilegeMode leastPrivilegeMode() {
        return this.innerModel().leastPrivilegeMode();
    }

    public SqlImageSku sqlImageSku() {
        return this.innerModel().sqlImageSku();
    }

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

    public WsfcDomainCredentials wsfcDomainCredentials() {
        return this.innerModel().wsfcDomainCredentials();
    }

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

    public AutoPatchingSettings autoPatchingSettings() {
        return this.innerModel().autoPatchingSettings();
    }

    public AutoBackupSettings autoBackupSettings() {
        return this.innerModel().autoBackupSettings();
    }

    public KeyVaultCredentialSettings keyVaultCredentialSettings() {
        return this.innerModel().keyVaultCredentialSettings();
    }

    public ServerConfigurationsManagementSettings serverConfigurationsManagementSettings() {
        return this.innerModel().serverConfigurationsManagementSettings();
    }

    public StorageConfigurationSettings storageConfigurationSettings() {
        return this.innerModel().storageConfigurationSettings();
    }

    public TroubleshootingStatus troubleshootingStatus() {
        return this.innerModel().troubleshootingStatus();
    }

    public AssessmentSettings assessmentSettings() {
        return this.innerModel().assessmentSettings();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.sqlvirtualmachine.SqlVirtualMachineManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String sqlVirtualMachineName;

    private SqlVirtualMachineUpdate updateParameters;

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

    public SqlVirtualMachine create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlVirtualMachines()
                .createOrUpdate(resourceGroupName, sqlVirtualMachineName, this.innerModel(), Context.NONE);
        return this;
    }

    public SqlVirtualMachine create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlVirtualMachines()
                .createOrUpdate(resourceGroupName, sqlVirtualMachineName, this.innerModel(), context);
        return this;
    }

    SqlVirtualMachineImpl(
        String name, com.azure.resourcemanager.sqlvirtualmachine.SqlVirtualMachineManager serviceManager) {
        this.innerObject = new SqlVirtualMachineInner();
        this.serviceManager = serviceManager;
        this.sqlVirtualMachineName = name;
    }

    public SqlVirtualMachineImpl update() {
        this.updateParameters = new SqlVirtualMachineUpdate();
        return this;
    }

    public SqlVirtualMachine apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlVirtualMachines()
                .update(resourceGroupName, sqlVirtualMachineName, updateParameters, Context.NONE);
        return this;
    }

    public SqlVirtualMachine apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlVirtualMachines()
                .update(resourceGroupName, sqlVirtualMachineName, updateParameters, context);
        return this;
    }

    SqlVirtualMachineImpl(
        SqlVirtualMachineInner innerObject,
        com.azure.resourcemanager.sqlvirtualmachine.SqlVirtualMachineManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.sqlVirtualMachineName = Utils.getValueFromIdByName(innerObject.id(), "sqlVirtualMachines");
    }

    public SqlVirtualMachine refresh() {
        String localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlVirtualMachines()
                .getByResourceGroupWithResponse(resourceGroupName, sqlVirtualMachineName, localExpand, Context.NONE)
                .getValue();
        return this;
    }

    public SqlVirtualMachine refresh(Context context) {
        String localExpand = null;
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlVirtualMachines()
                .getByResourceGroupWithResponse(resourceGroupName, sqlVirtualMachineName, localExpand, context)
                .getValue();
        return this;
    }

    public void startAssessment() {
        serviceManager.sqlVirtualMachines().startAssessment(resourceGroupName, sqlVirtualMachineName);
    }

    public void startAssessment(Context context) {
        serviceManager.sqlVirtualMachines().startAssessment(resourceGroupName, sqlVirtualMachineName, context);
    }

    public void redeploy() {
        serviceManager.sqlVirtualMachines().redeploy(resourceGroupName, sqlVirtualMachineName);
    }

    public void redeploy(Context context) {
        serviceManager.sqlVirtualMachines().redeploy(resourceGroupName, sqlVirtualMachineName, context);
    }

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

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

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

    public SqlVirtualMachineImpl withIdentity(ResourceIdentity identity) {
        this.innerModel().withIdentity(identity);
        return this;
    }

    public SqlVirtualMachineImpl withVirtualMachineResourceId(String virtualMachineResourceId) {
        this.innerModel().withVirtualMachineResourceId(virtualMachineResourceId);
        return this;
    }

    public SqlVirtualMachineImpl withSqlImageOffer(String sqlImageOffer) {
        this.innerModel().withSqlImageOffer(sqlImageOffer);
        return this;
    }

    public SqlVirtualMachineImpl withSqlServerLicenseType(SqlServerLicenseType sqlServerLicenseType) {
        this.innerModel().withSqlServerLicenseType(sqlServerLicenseType);
        return this;
    }

    public SqlVirtualMachineImpl withSqlManagement(SqlManagementMode sqlManagement) {
        this.innerModel().withSqlManagement(sqlManagement);
        return this;
    }

    public SqlVirtualMachineImpl withLeastPrivilegeMode(LeastPrivilegeMode leastPrivilegeMode) {
        this.innerModel().withLeastPrivilegeMode(leastPrivilegeMode);
        return this;
    }

    public SqlVirtualMachineImpl withSqlImageSku(SqlImageSku sqlImageSku) {
        this.innerModel().withSqlImageSku(sqlImageSku);
        return this;
    }

    public SqlVirtualMachineImpl withWsfcDomainCredentials(WsfcDomainCredentials wsfcDomainCredentials) {
        this.innerModel().withWsfcDomainCredentials(wsfcDomainCredentials);
        return this;
    }

    public SqlVirtualMachineImpl withWsfcStaticIp(String wsfcStaticIp) {
        this.innerModel().withWsfcStaticIp(wsfcStaticIp);
        return this;
    }

    public SqlVirtualMachineImpl withAutoPatchingSettings(AutoPatchingSettings autoPatchingSettings) {
        this.innerModel().withAutoPatchingSettings(autoPatchingSettings);
        return this;
    }

    public SqlVirtualMachineImpl withAutoBackupSettings(AutoBackupSettings autoBackupSettings) {
        this.innerModel().withAutoBackupSettings(autoBackupSettings);
        return this;
    }

    public SqlVirtualMachineImpl withKeyVaultCredentialSettings(KeyVaultCredentialSettings keyVaultCredentialSettings) {
        this.innerModel().withKeyVaultCredentialSettings(keyVaultCredentialSettings);
        return this;
    }

    public SqlVirtualMachineImpl withServerConfigurationsManagementSettings(
        ServerConfigurationsManagementSettings serverConfigurationsManagementSettings) {
        this.innerModel().withServerConfigurationsManagementSettings(serverConfigurationsManagementSettings);
        return this;
    }

    public SqlVirtualMachineImpl withStorageConfigurationSettings(
        StorageConfigurationSettings storageConfigurationSettings) {
        this.innerModel().withStorageConfigurationSettings(storageConfigurationSettings);
        return this;
    }

    public SqlVirtualMachineImpl withAssessmentSettings(AssessmentSettings assessmentSettings) {
        this.innerModel().withAssessmentSettings(assessmentSettings);
        return this;
    }

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

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