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

package com.azure.resourcemanager.hybridnetwork.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.hybridnetwork.fluent.models.DeviceInner;
import com.azure.resourcemanager.hybridnetwork.models.Device;
import com.azure.resourcemanager.hybridnetwork.models.DevicePropertiesFormat;
import com.azure.resourcemanager.hybridnetwork.models.DeviceRegistrationKey;
import com.azure.resourcemanager.hybridnetwork.models.TagsObject;
import java.util.Collections;
import java.util.Map;

public final class DeviceImpl implements Device, Device.Definition, Device.Update {
    private DeviceInner innerObject;

    private final com.azure.resourcemanager.hybridnetwork.HybridNetworkManager 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 DevicePropertiesFormat 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 DeviceInner innerModel() {
        return this.innerObject;
    }

    private com.azure.resourcemanager.hybridnetwork.HybridNetworkManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String deviceName;

    private TagsObject updateParameters;

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

    public Device create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevices()
                .createOrUpdate(resourceGroupName, deviceName, this.innerModel(), Context.NONE);
        return this;
    }

    public Device create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevices()
                .createOrUpdate(resourceGroupName, deviceName, this.innerModel(), context);
        return this;
    }

    DeviceImpl(String name, com.azure.resourcemanager.hybridnetwork.HybridNetworkManager serviceManager) {
        this.innerObject = new DeviceInner();
        this.serviceManager = serviceManager;
        this.deviceName = name;
    }

    public DeviceImpl update() {
        this.updateParameters = new TagsObject();
        return this;
    }

    public Device apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevices()
                .updateTagsWithResponse(resourceGroupName, deviceName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Device apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevices()
                .updateTagsWithResponse(resourceGroupName, deviceName, updateParameters, context)
                .getValue();
        return this;
    }

    DeviceImpl(DeviceInner innerObject, com.azure.resourcemanager.hybridnetwork.HybridNetworkManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.deviceName = Utils.getValueFromIdByName(innerObject.id(), "devices");
    }

    public Device refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevices()
                .getByResourceGroupWithResponse(resourceGroupName, deviceName, Context.NONE)
                .getValue();
        return this;
    }

    public Device refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevices()
                .getByResourceGroupWithResponse(resourceGroupName, deviceName, context)
                .getValue();
        return this;
    }

    public Response<DeviceRegistrationKey> listRegistrationKeyWithResponse(Context context) {
        return serviceManager.devices().listRegistrationKeyWithResponse(resourceGroupName, deviceName, context);
    }

    public DeviceRegistrationKey listRegistrationKey() {
        return serviceManager.devices().listRegistrationKey(resourceGroupName, deviceName);
    }

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

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

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

    public DeviceImpl withProperties(DevicePropertiesFormat properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

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