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

package com.azure.resourcemanager.newrelicobservability.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.newrelicobservability.fluent.models.MetricRulesInner;
import com.azure.resourcemanager.newrelicobservability.fluent.models.TagRuleInner;
import com.azure.resourcemanager.newrelicobservability.fluent.models.TagRuleUpdateInner;
import com.azure.resourcemanager.newrelicobservability.models.LogRules;
import com.azure.resourcemanager.newrelicobservability.models.MetricRules;
import com.azure.resourcemanager.newrelicobservability.models.ProvisioningState;
import com.azure.resourcemanager.newrelicobservability.models.TagRule;

public final class TagRuleImpl implements TagRule, TagRule.Definition, TagRule.Update {
    private TagRuleInner innerObject;

    private final com.azure.resourcemanager.newrelicobservability.NewRelicObservabilityManager serviceManager;

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

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

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

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

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

    public LogRules logRules() {
        return this.innerModel().logRules();
    }

    public MetricRules metricRules() {
        MetricRulesInner inner = this.innerModel().metricRules();
        if (inner != null) {
            return new MetricRulesImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.newrelicobservability.NewRelicObservabilityManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String monitorName;

    private String ruleSetName;

    private TagRuleUpdateInner updateProperties;

    public TagRuleImpl withExistingMonitor(String resourceGroupName, String monitorName) {
        this.resourceGroupName = resourceGroupName;
        this.monitorName = monitorName;
        return this;
    }

    public TagRule create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTagRules()
                .createOrUpdate(resourceGroupName, monitorName, ruleSetName, this.innerModel(), Context.NONE);
        return this;
    }

    public TagRule create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTagRules()
                .createOrUpdate(resourceGroupName, monitorName, ruleSetName, this.innerModel(), context);
        return this;
    }

    TagRuleImpl(
        String name, com.azure.resourcemanager.newrelicobservability.NewRelicObservabilityManager serviceManager) {
        this.innerObject = new TagRuleInner();
        this.serviceManager = serviceManager;
        this.ruleSetName = name;
    }

    public TagRuleImpl update() {
        this.updateProperties = new TagRuleUpdateInner();
        return this;
    }

    public TagRule apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTagRules()
                .updateWithResponse(resourceGroupName, monitorName, ruleSetName, updateProperties, Context.NONE)
                .getValue();
        return this;
    }

    public TagRule apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTagRules()
                .updateWithResponse(resourceGroupName, monitorName, ruleSetName, updateProperties, context)
                .getValue();
        return this;
    }

    TagRuleImpl(
        TagRuleInner innerObject,
        com.azure.resourcemanager.newrelicobservability.NewRelicObservabilityManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.monitorName = Utils.getValueFromIdByName(innerObject.id(), "monitors");
        this.ruleSetName = Utils.getValueFromIdByName(innerObject.id(), "tagRules");
    }

    public TagRule refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTagRules()
                .getWithResponse(resourceGroupName, monitorName, ruleSetName, Context.NONE)
                .getValue();
        return this;
    }

    public TagRule refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTagRules()
                .getWithResponse(resourceGroupName, monitorName, ruleSetName, context)
                .getValue();
        return this;
    }

    public TagRuleImpl withLogRules(LogRules logRules) {
        if (isInCreateMode()) {
            this.innerModel().withLogRules(logRules);
            return this;
        } else {
            this.updateProperties.withLogRules(logRules);
            return this;
        }
    }

    public TagRuleImpl withMetricRules(MetricRulesInner metricRules) {
        if (isInCreateMode()) {
            this.innerModel().withMetricRules(metricRules);
            return this;
        } else {
            this.updateProperties.withMetricRules(metricRules);
            return this;
        }
    }

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