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

package com.azure.resourcemanager.alertsmanagement.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.alertsmanagement.fluent.models.AlertProcessingRuleInner;
import com.azure.resourcemanager.alertsmanagement.models.AlertProcessingRule;
import com.azure.resourcemanager.alertsmanagement.models.AlertProcessingRuleProperties;
import com.azure.resourcemanager.alertsmanagement.models.PatchObject;
import java.util.Collections;
import java.util.Map;

public final class AlertProcessingRuleImpl
    implements AlertProcessingRule, AlertProcessingRule.Definition, AlertProcessingRule.Update {
    private AlertProcessingRuleInner innerObject;

    private final com.azure.resourcemanager.alertsmanagement.AlertsManagementManager 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 AlertProcessingRuleProperties 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 AlertProcessingRuleInner innerModel() {
        return this.innerObject;
    }

    private com.azure.resourcemanager.alertsmanagement.AlertsManagementManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String alertProcessingRuleName;

    private PatchObject updateAlertProcessingRulePatch;

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

    public AlertProcessingRule create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAlertProcessingRules()
                .createOrUpdateWithResponse(resourceGroupName, alertProcessingRuleName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public AlertProcessingRule create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAlertProcessingRules()
                .createOrUpdateWithResponse(resourceGroupName, alertProcessingRuleName, this.innerModel(), context)
                .getValue();
        return this;
    }

    AlertProcessingRuleImpl(
        String name, com.azure.resourcemanager.alertsmanagement.AlertsManagementManager serviceManager) {
        this.innerObject = new AlertProcessingRuleInner();
        this.serviceManager = serviceManager;
        this.alertProcessingRuleName = name;
    }

    public AlertProcessingRuleImpl update() {
        this.updateAlertProcessingRulePatch = new PatchObject();
        return this;
    }

    public AlertProcessingRule apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAlertProcessingRules()
                .updateWithResponse(
                    resourceGroupName, alertProcessingRuleName, updateAlertProcessingRulePatch, Context.NONE)
                .getValue();
        return this;
    }

    public AlertProcessingRule apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAlertProcessingRules()
                .updateWithResponse(resourceGroupName, alertProcessingRuleName, updateAlertProcessingRulePatch, context)
                .getValue();
        return this;
    }

    AlertProcessingRuleImpl(
        AlertProcessingRuleInner innerObject,
        com.azure.resourcemanager.alertsmanagement.AlertsManagementManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.alertProcessingRuleName = Utils.getValueFromIdByName(innerObject.id(), "actionRules");
    }

    public AlertProcessingRule refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAlertProcessingRules()
                .getByResourceGroupWithResponse(resourceGroupName, alertProcessingRuleName, Context.NONE)
                .getValue();
        return this;
    }

    public AlertProcessingRule refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAlertProcessingRules()
                .getByResourceGroupWithResponse(resourceGroupName, alertProcessingRuleName, context)
                .getValue();
        return this;
    }

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

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

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

    public AlertProcessingRuleImpl withProperties(AlertProcessingRuleProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public AlertProcessingRuleImpl withEnabled(Boolean enabled) {
        this.updateAlertProcessingRulePatch.withEnabled(enabled);
        return this;
    }

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