// 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.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.dnsresolver.fluent.models.InboundEndpointInner;
import com.azure.resourcemanager.dnsresolver.models.InboundEndpoint;
import com.azure.resourcemanager.dnsresolver.models.InboundEndpointPatch;
import com.azure.resourcemanager.dnsresolver.models.IpConfiguration;
import com.azure.resourcemanager.dnsresolver.models.ProvisioningState;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class InboundEndpointImpl implements InboundEndpoint, InboundEndpoint.Definition, InboundEndpoint.Update {
    private InboundEndpointInner 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 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 String etag() {
        return this.innerModel().etag();
    }

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String dnsResolverName;

    private String inboundEndpointName;

    private String createIfMatch;

    private String createIfNoneMatch;

    private String updateIfMatch;

    private InboundEndpointPatch updateParameters;

    public InboundEndpointImpl withExistingDnsResolver(String resourceGroupName, String dnsResolverName) {
        this.resourceGroupName = resourceGroupName;
        this.dnsResolverName = dnsResolverName;
        return this;
    }

    public InboundEndpoint create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInboundEndpoints()
                .createOrUpdate(
                    resourceGroupName,
                    dnsResolverName,
                    inboundEndpointName,
                    this.innerModel(),
                    createIfMatch,
                    createIfNoneMatch,
                    Context.NONE);
        return this;
    }

    public InboundEndpoint create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInboundEndpoints()
                .createOrUpdate(
                    resourceGroupName,
                    dnsResolverName,
                    inboundEndpointName,
                    this.innerModel(),
                    createIfMatch,
                    createIfNoneMatch,
                    context);
        return this;
    }

    InboundEndpointImpl(String name, com.azure.resourcemanager.dnsresolver.DnsResolverManager serviceManager) {
        this.innerObject = new InboundEndpointInner();
        this.serviceManager = serviceManager;
        this.inboundEndpointName = name;
        this.createIfMatch = null;
        this.createIfNoneMatch = null;
    }

    public InboundEndpointImpl update() {
        this.updateIfMatch = null;
        this.updateParameters = new InboundEndpointPatch();
        return this;
    }

    public InboundEndpoint apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInboundEndpoints()
                .update(
                    resourceGroupName,
                    dnsResolverName,
                    inboundEndpointName,
                    updateParameters,
                    updateIfMatch,
                    Context.NONE);
        return this;
    }

    public InboundEndpoint apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInboundEndpoints()
                .update(
                    resourceGroupName, dnsResolverName, inboundEndpointName, updateParameters, updateIfMatch, context);
        return this;
    }

    InboundEndpointImpl(
        InboundEndpointInner innerObject, com.azure.resourcemanager.dnsresolver.DnsResolverManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.dnsResolverName = Utils.getValueFromIdByName(innerObject.id(), "dnsResolvers");
        this.inboundEndpointName = Utils.getValueFromIdByName(innerObject.id(), "inboundEndpoints");
    }

    public InboundEndpoint refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInboundEndpoints()
                .getWithResponse(resourceGroupName, dnsResolverName, inboundEndpointName, Context.NONE)
                .getValue();
        return this;
    }

    public InboundEndpoint refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getInboundEndpoints()
                .getWithResponse(resourceGroupName, dnsResolverName, inboundEndpointName, context)
                .getValue();
        return this;
    }

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

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

    public InboundEndpointImpl withIpConfigurations(List<IpConfiguration> ipConfigurations) {
        this.innerModel().withIpConfigurations(ipConfigurations);
        return this;
    }

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

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

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

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