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

package com.azure.resourcemanager.scvmm.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.scvmm.fluent.models.AvailabilitySetInner;
import com.azure.resourcemanager.scvmm.models.AvailabilitySet;
import com.azure.resourcemanager.scvmm.models.ExtendedLocation;
import com.azure.resourcemanager.scvmm.models.ResourcePatch;
import java.util.Collections;
import java.util.Map;

public final class AvailabilitySetImpl implements AvailabilitySet, AvailabilitySet.Definition, AvailabilitySet.Update {
    private AvailabilitySetInner innerObject;

    private final com.azure.resourcemanager.scvmm.ScvmmManager 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 ExtendedLocation extendedLocation() {
        return this.innerModel().extendedLocation();
    }

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

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

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

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

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

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

    private com.azure.resourcemanager.scvmm.ScvmmManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String availabilitySetName;

    private ResourcePatch updateBody;

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

    public AvailabilitySet create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAvailabilitySets()
                .createOrUpdate(resourceGroupName, availabilitySetName, this.innerModel(), Context.NONE);
        return this;
    }

    public AvailabilitySet create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAvailabilitySets()
                .createOrUpdate(resourceGroupName, availabilitySetName, this.innerModel(), context);
        return this;
    }

    AvailabilitySetImpl(String name, com.azure.resourcemanager.scvmm.ScvmmManager serviceManager) {
        this.innerObject = new AvailabilitySetInner();
        this.serviceManager = serviceManager;
        this.availabilitySetName = name;
    }

    public AvailabilitySetImpl update() {
        this.updateBody = new ResourcePatch();
        return this;
    }

    public AvailabilitySet apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAvailabilitySets()
                .update(resourceGroupName, availabilitySetName, updateBody, Context.NONE);
        return this;
    }

    public AvailabilitySet apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAvailabilitySets()
                .update(resourceGroupName, availabilitySetName, updateBody, context);
        return this;
    }

    AvailabilitySetImpl(AvailabilitySetInner innerObject, com.azure.resourcemanager.scvmm.ScvmmManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.availabilitySetName = Utils.getValueFromIdByName(innerObject.id(), "availabilitySets");
    }

    public AvailabilitySet refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAvailabilitySets()
                .getByResourceGroupWithResponse(resourceGroupName, availabilitySetName, Context.NONE)
                .getValue();
        return this;
    }

    public AvailabilitySet refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAvailabilitySets()
                .getByResourceGroupWithResponse(resourceGroupName, availabilitySetName, context)
                .getValue();
        return this;
    }

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

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

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

    public AvailabilitySetImpl withExtendedLocation(ExtendedLocation extendedLocation) {
        this.innerModel().withExtendedLocation(extendedLocation);
        return this;
    }

    public AvailabilitySetImpl withAvailabilitySetName(String availabilitySetName) {
        this.innerModel().withAvailabilitySetName(availabilitySetName);
        return this;
    }

    public AvailabilitySetImpl withVmmServerId(String vmmServerId) {
        this.innerModel().withVmmServerId(vmmServerId);
        return this;
    }

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