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

package com.azure.resourcemanager.networkcloud.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.networkcloud.fluent.models.RackInner;
import com.azure.resourcemanager.networkcloud.models.ExtendedLocation;
import com.azure.resourcemanager.networkcloud.models.Rack;
import com.azure.resourcemanager.networkcloud.models.RackDetailedStatus;
import com.azure.resourcemanager.networkcloud.models.RackPatchParameters;
import com.azure.resourcemanager.networkcloud.models.RackProvisioningState;
import java.util.Collections;
import java.util.Map;

public final class RackImpl implements Rack, Rack.Definition, Rack.Update {
    private RackInner innerObject;

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

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

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

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

    public RackDetailedStatus detailedStatus() {
        return this.innerModel().detailedStatus();
    }

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

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.networkcloud.NetworkCloudManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String rackName;

    private RackPatchParameters updateRackUpdateParameters;

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

    public Rack create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRacks()
                .createOrUpdate(resourceGroupName, rackName, this.innerModel(), Context.NONE);
        return this;
    }

    public Rack create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRacks()
                .createOrUpdate(resourceGroupName, rackName, this.innerModel(), context);
        return this;
    }

    RackImpl(String name, com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager) {
        this.innerObject = new RackInner();
        this.serviceManager = serviceManager;
        this.rackName = name;
    }

    public RackImpl update() {
        this.updateRackUpdateParameters = new RackPatchParameters();
        return this;
    }

    public Rack apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRacks()
                .update(resourceGroupName, rackName, updateRackUpdateParameters, Context.NONE);
        return this;
    }

    public Rack apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRacks()
                .update(resourceGroupName, rackName, updateRackUpdateParameters, context);
        return this;
    }

    RackImpl(RackInner innerObject, com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.rackName = Utils.getValueFromIdByName(innerObject.id(), "racks");
    }

    public Rack refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRacks()
                .getByResourceGroupWithResponse(resourceGroupName, rackName, Context.NONE)
                .getValue();
        return this;
    }

    public Rack refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getRacks()
                .getByResourceGroupWithResponse(resourceGroupName, rackName, context)
                .getValue();
        return this;
    }

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

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

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

    public RackImpl withAvailabilityZone(String availabilityZone) {
        this.innerModel().withAvailabilityZone(availabilityZone);
        return this;
    }

    public RackImpl withRackLocation(String rackLocation) {
        if (isInCreateMode()) {
            this.innerModel().withRackLocation(rackLocation);
            return this;
        } else {
            this.updateRackUpdateParameters.withRackLocation(rackLocation);
            return this;
        }
    }

    public RackImpl withRackSerialNumber(String rackSerialNumber) {
        if (isInCreateMode()) {
            this.innerModel().withRackSerialNumber(rackSerialNumber);
            return this;
        } else {
            this.updateRackUpdateParameters.withRackSerialNumber(rackSerialNumber);
            return this;
        }
    }

    public RackImpl withRackSkuId(String rackSkuId) {
        this.innerModel().withRackSkuId(rackSkuId);
        return this;
    }

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

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