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

package com.azure.resourcemanager.dnsresolver.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.dnsresolver.fluent.models.ForwardingRuleInner;
import com.azure.resourcemanager.dnsresolver.models.ForwardingRule;
import com.azure.resourcemanager.dnsresolver.models.ForwardingRulePatch;
import com.azure.resourcemanager.dnsresolver.models.ForwardingRuleState;
import com.azure.resourcemanager.dnsresolver.models.ProvisioningState;
import com.azure.resourcemanager.dnsresolver.models.TargetDnsServer;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class ForwardingRuleImpl implements ForwardingRule, ForwardingRule.Definition, ForwardingRule.Update {
    private ForwardingRuleInner innerObject;

    private final com.azure.resourcemanager.dnsresolver.DnsResolverManager serviceManager;

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

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

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

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

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

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

    public List<TargetDnsServer> targetDnsServers() {
        List<TargetDnsServer> inner = this.innerModel().targetDnsServers();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

    public Map<String, String> metadata() {
        Map<String, String> inner = this.innerModel().metadata();
        if (inner != null) {
            return Collections.unmodifiableMap(inner);
        } else {
            return Collections.emptyMap();
        }
    }

    public ForwardingRuleState forwardingRuleState() {
        return this.innerModel().forwardingRuleState();
    }

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.dnsresolver.DnsResolverManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String dnsForwardingRulesetName;

    private String forwardingRuleName;

    private String createIfMatch;

    private String createIfNoneMatch;

    private String updateIfMatch;

    private ForwardingRulePatch updateParameters;

    public ForwardingRuleImpl withExistingDnsForwardingRuleset(
        String resourceGroupName, String dnsForwardingRulesetName) {
        this.resourceGroupName = resourceGroupName;
        this.dnsForwardingRulesetName = dnsForwardingRulesetName;
        return this;
    }

    public ForwardingRule create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getForwardingRules()
                .createOrUpdateWithResponse(
                    resourceGroupName,
                    dnsForwardingRulesetName,
                    forwardingRuleName,
                    this.innerModel(),
                    createIfMatch,
                    createIfNoneMatch,
                    Context.NONE)
                .getValue();
        return this;
    }

    public ForwardingRule create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getForwardingRules()
                .createOrUpdateWithResponse(
                    resourceGroupName,
                    dnsForwardingRulesetName,
                    forwardingRuleName,
                    this.innerModel(),
                    createIfMatch,
                    createIfNoneMatch,
                    context)
                .getValue();
        return this;
    }

    ForwardingRuleImpl(String name, com.azure.resourcemanager.dnsresolver.DnsResolverManager serviceManager) {
        this.innerObject = new ForwardingRuleInner();
        this.serviceManager = serviceManager;
        this.forwardingRuleName = name;
        this.createIfMatch = null;
        this.createIfNoneMatch = null;
    }

    public ForwardingRuleImpl update() {
        this.updateIfMatch = null;
        this.updateParameters = new ForwardingRulePatch();
        return this;
    }

    public ForwardingRule apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getForwardingRules()
                .updateWithResponse(
                    resourceGroupName,
                    dnsForwardingRulesetName,
                    forwardingRuleName,
                    updateParameters,
                    updateIfMatch,
                    Context.NONE)
                .getValue();
        return this;
    }

    public ForwardingRule apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getForwardingRules()
                .updateWithResponse(
                    resourceGroupName,
                    dnsForwardingRulesetName,
                    forwardingRuleName,
                    updateParameters,
                    updateIfMatch,
                    context)
                .getValue();
        return this;
    }

    ForwardingRuleImpl(
        ForwardingRuleInner innerObject, com.azure.resourcemanager.dnsresolver.DnsResolverManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.dnsForwardingRulesetName = Utils.getValueFromIdByName(innerObject.id(), "dnsForwardingRulesets");
        this.forwardingRuleName = Utils.getValueFromIdByName(innerObject.id(), "forwardingRules");
    }

    public ForwardingRule refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getForwardingRules()
                .getWithResponse(resourceGroupName, dnsForwardingRulesetName, forwardingRuleName, Context.NONE)
                .getValue();
        return this;
    }

    public ForwardingRule refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getForwardingRules()
                .getWithResponse(resourceGroupName, dnsForwardingRulesetName, forwardingRuleName, context)
                .getValue();
        return this;
    }

    public ForwardingRuleImpl withDomainName(String domainName) {
        this.innerModel().withDomainName(domainName);
        return this;
    }

    public ForwardingRuleImpl withTargetDnsServers(List<TargetDnsServer> targetDnsServers) {
        if (isInCreateMode()) {
            this.innerModel().withTargetDnsServers(targetDnsServers);
            return this;
        } else {
            this.updateParameters.withTargetDnsServers(targetDnsServers);
            return this;
        }
    }

    public ForwardingRuleImpl withMetadata(Map<String, String> metadata) {
        if (isInCreateMode()) {
            this.innerModel().withMetadata(metadata);
            return this;
        } else {
            this.updateParameters.withMetadata(metadata);
            return this;
        }
    }

    public ForwardingRuleImpl withForwardingRuleState(ForwardingRuleState forwardingRuleState) {
        if (isInCreateMode()) {
            this.innerModel().withForwardingRuleState(forwardingRuleState);
            return this;
        } else {
            this.updateParameters.withForwardingRuleState(forwardingRuleState);
            return this;
        }
    }

    public ForwardingRuleImpl withIfMatch(String ifMatch) {
        if (isInCreateMode()) {
            this.createIfMatch = ifMatch;
            return this;
        } else {
            this.updateIfMatch = ifMatch;
            return this;
        }
    }

    public ForwardingRuleImpl withIfNoneMatch(String ifNoneMatch) {
        this.createIfNoneMatch = ifNoneMatch;
        return this;
    }

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