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

package com.azure.resourcemanager.workloads.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.workloads.fluent.models.MonitorInner;
import com.azure.resourcemanager.workloads.models.ManagedRGConfiguration;
import com.azure.resourcemanager.workloads.models.Monitor;
import com.azure.resourcemanager.workloads.models.MonitorPropertiesErrors;
import com.azure.resourcemanager.workloads.models.RoutingPreference;
import com.azure.resourcemanager.workloads.models.UpdateMonitorRequest;
import com.azure.resourcemanager.workloads.models.UserAssignedServiceIdentity;
import com.azure.resourcemanager.workloads.models.WorkloadMonitorProvisioningState;
import java.util.Collections;
import java.util.Map;

public final class MonitorImpl implements Monitor, Monitor.Definition, Monitor.Update {
    private MonitorInner innerObject;

    private final com.azure.resourcemanager.workloads.WorkloadsManager 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 UserAssignedServiceIdentity identity() {
        return this.innerModel().identity();
    }

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

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

    public MonitorPropertiesErrors errors() {
        return this.innerModel().errors();
    }

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

    public RoutingPreference routingPreference() {
        return this.innerModel().routingPreference();
    }

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

    public ManagedRGConfiguration managedResourceGroupConfiguration() {
        return this.innerModel().managedResourceGroupConfiguration();
    }

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.workloads.WorkloadsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String monitorName;

    private UpdateMonitorRequest updateBody;

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

    public Monitor create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .create(resourceGroupName, monitorName, this.innerModel(), Context.NONE);
        return this;
    }

    public Monitor create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .create(resourceGroupName, monitorName, this.innerModel(), context);
        return this;
    }

    MonitorImpl(String name, com.azure.resourcemanager.workloads.WorkloadsManager serviceManager) {
        this.innerObject = new MonitorInner();
        this.serviceManager = serviceManager;
        this.monitorName = name;
    }

    public MonitorImpl update() {
        this.updateBody = new UpdateMonitorRequest();
        return this;
    }

    public Monitor apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .updateWithResponse(resourceGroupName, monitorName, updateBody, Context.NONE)
                .getValue();
        return this;
    }

    public Monitor apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .updateWithResponse(resourceGroupName, monitorName, updateBody, context)
                .getValue();
        return this;
    }

    MonitorImpl(MonitorInner innerObject, com.azure.resourcemanager.workloads.WorkloadsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.monitorName = Utils.getValueFromIdByName(innerObject.id(), "monitors");
    }

    public Monitor refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .getByResourceGroupWithResponse(resourceGroupName, monitorName, Context.NONE)
                .getValue();
        return this;
    }

    public Monitor refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMonitors()
                .getByResourceGroupWithResponse(resourceGroupName, monitorName, context)
                .getValue();
        return this;
    }

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

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

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

    public MonitorImpl withIdentity(UserAssignedServiceIdentity identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateBody.withIdentity(identity);
            return this;
        }
    }

    public MonitorImpl withAppLocation(String appLocation) {
        this.innerModel().withAppLocation(appLocation);
        return this;
    }

    public MonitorImpl withRoutingPreference(RoutingPreference routingPreference) {
        this.innerModel().withRoutingPreference(routingPreference);
        return this;
    }

    public MonitorImpl withZoneRedundancyPreference(String zoneRedundancyPreference) {
        this.innerModel().withZoneRedundancyPreference(zoneRedundancyPreference);
        return this;
    }

    public MonitorImpl withManagedResourceGroupConfiguration(ManagedRGConfiguration managedResourceGroupConfiguration) {
        this.innerModel().withManagedResourceGroupConfiguration(managedResourceGroupConfiguration);
        return this;
    }

    public MonitorImpl withLogAnalyticsWorkspaceArmId(String logAnalyticsWorkspaceArmId) {
        this.innerModel().withLogAnalyticsWorkspaceArmId(logAnalyticsWorkspaceArmId);
        return this;
    }

    public MonitorImpl withMonitorSubnet(String monitorSubnet) {
        this.innerModel().withMonitorSubnet(monitorSubnet);
        return this;
    }

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