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

package com.azure.resourcemanager.azurearcdata.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.azurearcdata.fluent.models.SqlServerInstanceInner;
import com.azure.resourcemanager.azurearcdata.models.SqlServerInstance;
import com.azure.resourcemanager.azurearcdata.models.SqlServerInstanceProperties;
import com.azure.resourcemanager.azurearcdata.models.SqlServerInstanceUpdate;
import java.util.Collections;
import java.util.Map;

public final class SqlServerInstanceImpl
    implements SqlServerInstance, SqlServerInstance.Definition, SqlServerInstance.Update {
    private SqlServerInstanceInner innerObject;

    private final com.azure.resourcemanager.azurearcdata.AzureArcDataManager 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 SqlServerInstanceProperties 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 SqlServerInstanceInner innerModel() {
        return this.innerObject;
    }

    private com.azure.resourcemanager.azurearcdata.AzureArcDataManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String sqlServerInstanceName;

    private SqlServerInstanceUpdate updateParameters;

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

    public SqlServerInstance create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlServerInstances()
                .create(resourceGroupName, sqlServerInstanceName, this.innerModel(), Context.NONE);
        return this;
    }

    public SqlServerInstance create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlServerInstances()
                .create(resourceGroupName, sqlServerInstanceName, this.innerModel(), context);
        return this;
    }

    SqlServerInstanceImpl(String name, com.azure.resourcemanager.azurearcdata.AzureArcDataManager serviceManager) {
        this.innerObject = new SqlServerInstanceInner();
        this.serviceManager = serviceManager;
        this.sqlServerInstanceName = name;
    }

    public SqlServerInstanceImpl update() {
        this.updateParameters = new SqlServerInstanceUpdate();
        return this;
    }

    public SqlServerInstance apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlServerInstances()
                .updateWithResponse(resourceGroupName, sqlServerInstanceName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public SqlServerInstance apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlServerInstances()
                .updateWithResponse(resourceGroupName, sqlServerInstanceName, updateParameters, context)
                .getValue();
        return this;
    }

    SqlServerInstanceImpl(
        SqlServerInstanceInner innerObject, com.azure.resourcemanager.azurearcdata.AzureArcDataManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.sqlServerInstanceName = Utils.getValueFromIdByName(innerObject.id(), "sqlServerInstances");
    }

    public SqlServerInstance refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlServerInstances()
                .getByResourceGroupWithResponse(resourceGroupName, sqlServerInstanceName, Context.NONE)
                .getValue();
        return this;
    }

    public SqlServerInstance refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getSqlServerInstances()
                .getByResourceGroupWithResponse(resourceGroupName, sqlServerInstanceName, context)
                .getValue();
        return this;
    }

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

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

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

    public SqlServerInstanceImpl withProperties(SqlServerInstanceProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

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