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

package com.azure.resourcemanager.streamanalytics.implementation;

import com.azure.core.util.Context;
import com.azure.resourcemanager.streamanalytics.fluent.models.PrivateEndpointInner;
import com.azure.resourcemanager.streamanalytics.models.PrivateEndpoint;
import com.azure.resourcemanager.streamanalytics.models.PrivateLinkServiceConnection;
import java.util.Collections;
import java.util.List;

public final class PrivateEndpointImpl implements PrivateEndpoint, PrivateEndpoint.Definition, PrivateEndpoint.Update {
    private PrivateEndpointInner innerObject;

    private final com.azure.resourcemanager.streamanalytics.StreamAnalyticsManager 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 String createdDate() {
        return this.innerModel().createdDate();
    }

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

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

    private com.azure.resourcemanager.streamanalytics.StreamAnalyticsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String clusterName;

    private String privateEndpointName;

    private String createIfMatch;

    private String createIfNoneMatch;

    private String updateIfMatch;

    private String updateIfNoneMatch;

    public PrivateEndpointImpl withExistingCluster(String resourceGroupName, String clusterName) {
        this.resourceGroupName = resourceGroupName;
        this.clusterName = clusterName;
        return this;
    }

    public PrivateEndpoint create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpoints()
                .createOrUpdateWithResponse(
                    resourceGroupName,
                    clusterName,
                    privateEndpointName,
                    this.innerModel(),
                    createIfMatch,
                    createIfNoneMatch,
                    Context.NONE)
                .getValue();
        return this;
    }

    public PrivateEndpoint create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpoints()
                .createOrUpdateWithResponse(
                    resourceGroupName,
                    clusterName,
                    privateEndpointName,
                    this.innerModel(),
                    createIfMatch,
                    createIfNoneMatch,
                    context)
                .getValue();
        return this;
    }

    PrivateEndpointImpl(String name, com.azure.resourcemanager.streamanalytics.StreamAnalyticsManager serviceManager) {
        this.innerObject = new PrivateEndpointInner();
        this.serviceManager = serviceManager;
        this.privateEndpointName = name;
        this.createIfMatch = null;
        this.createIfNoneMatch = null;
    }

    public PrivateEndpointImpl update() {
        this.updateIfMatch = null;
        this.updateIfNoneMatch = null;
        return this;
    }

    public PrivateEndpoint apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpoints()
                .createOrUpdateWithResponse(
                    resourceGroupName,
                    clusterName,
                    privateEndpointName,
                    this.innerModel(),
                    updateIfMatch,
                    updateIfNoneMatch,
                    Context.NONE)
                .getValue();
        return this;
    }

    public PrivateEndpoint apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpoints()
                .createOrUpdateWithResponse(
                    resourceGroupName,
                    clusterName,
                    privateEndpointName,
                    this.innerModel(),
                    updateIfMatch,
                    updateIfNoneMatch,
                    context)
                .getValue();
        return this;
    }

    PrivateEndpointImpl(
        PrivateEndpointInner innerObject,
        com.azure.resourcemanager.streamanalytics.StreamAnalyticsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.clusterName = Utils.getValueFromIdByName(innerObject.id(), "clusters");
        this.privateEndpointName = Utils.getValueFromIdByName(innerObject.id(), "privateEndpoints");
    }

    public PrivateEndpoint refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpoints()
                .getWithResponse(resourceGroupName, clusterName, privateEndpointName, Context.NONE)
                .getValue();
        return this;
    }

    public PrivateEndpoint refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpoints()
                .getWithResponse(resourceGroupName, clusterName, privateEndpointName, context)
                .getValue();
        return this;
    }

    public PrivateEndpointImpl withManualPrivateLinkServiceConnections(
        List<PrivateLinkServiceConnection> manualPrivateLinkServiceConnections) {
        this.innerModel().withManualPrivateLinkServiceConnections(manualPrivateLinkServiceConnections);
        return this;
    }

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

    public PrivateEndpointImpl withIfNoneMatch(String ifNoneMatch) {
        if (isInCreateMode()) {
            this.createIfNoneMatch = ifNoneMatch;
            return this;
        } else {
            this.updateIfNoneMatch = ifNoneMatch;
            return this;
        }
    }

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