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

package com.azure.resourcemanager.healthcareapis.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.healthcareapis.fluent.models.PrivateEndpointConnectionDescriptionInner;
import com.azure.resourcemanager.healthcareapis.models.PrivateEndpoint;
import com.azure.resourcemanager.healthcareapis.models.PrivateEndpointConnection;
import com.azure.resourcemanager.healthcareapis.models.PrivateEndpointConnectionDescription;
import com.azure.resourcemanager.healthcareapis.models.PrivateEndpointConnectionProvisioningState;
import com.azure.resourcemanager.healthcareapis.models.PrivateLinkServiceConnectionState;

public final class PrivateEndpointConnectionDescriptionImpl
    implements PrivateEndpointConnectionDescription,
        PrivateEndpointConnectionDescription.Definition,
        PrivateEndpointConnectionDescription.Update {
    private PrivateEndpointConnectionDescriptionInner innerObject;

    private final com.azure.resourcemanager.healthcareapis.HealthcareApisManager serviceManager;

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

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

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

    public PrivateEndpoint privateEndpoint() {
        return this.innerModel().privateEndpoint();
    }

    public PrivateLinkServiceConnectionState privateLinkServiceConnectionState() {
        return this.innerModel().privateLinkServiceConnectionState();
    }

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

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

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

    private com.azure.resourcemanager.healthcareapis.HealthcareApisManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String resourceName;

    private String privateEndpointConnectionName;

    private PrivateEndpointConnection createProperties;

    private PrivateEndpointConnection updateProperties;

    public PrivateEndpointConnectionDescriptionImpl withExistingService(String resourceGroupName, String resourceName) {
        this.resourceGroupName = resourceGroupName;
        this.resourceName = resourceName;
        return this;
    }

    public PrivateEndpointConnectionDescription create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpointConnections()
                .createOrUpdate(
                    resourceGroupName, resourceName, privateEndpointConnectionName, createProperties, Context.NONE);
        return this;
    }

    public PrivateEndpointConnectionDescription create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpointConnections()
                .createOrUpdate(
                    resourceGroupName, resourceName, privateEndpointConnectionName, createProperties, context);
        return this;
    }

    PrivateEndpointConnectionDescriptionImpl(
        String name, com.azure.resourcemanager.healthcareapis.HealthcareApisManager serviceManager) {
        this.innerObject = new PrivateEndpointConnectionDescriptionInner();
        this.serviceManager = serviceManager;
        this.privateEndpointConnectionName = name;
        this.createProperties = new PrivateEndpointConnection();
    }

    public PrivateEndpointConnectionDescriptionImpl update() {
        this.updateProperties = new PrivateEndpointConnection();
        return this;
    }

    public PrivateEndpointConnectionDescription apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpointConnections()
                .createOrUpdate(
                    resourceGroupName, resourceName, privateEndpointConnectionName, updateProperties, Context.NONE);
        return this;
    }

    public PrivateEndpointConnectionDescription apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpointConnections()
                .createOrUpdate(
                    resourceGroupName, resourceName, privateEndpointConnectionName, updateProperties, context);
        return this;
    }

    PrivateEndpointConnectionDescriptionImpl(
        PrivateEndpointConnectionDescriptionInner innerObject,
        com.azure.resourcemanager.healthcareapis.HealthcareApisManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.resourceName = Utils.getValueFromIdByName(innerObject.id(), "services");
        this.privateEndpointConnectionName = Utils.getValueFromIdByName(innerObject.id(), "privateEndpointConnections");
    }

    public PrivateEndpointConnectionDescription refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpointConnections()
                .getWithResponse(resourceGroupName, resourceName, privateEndpointConnectionName, Context.NONE)
                .getValue();
        return this;
    }

    public PrivateEndpointConnectionDescription refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateEndpointConnections()
                .getWithResponse(resourceGroupName, resourceName, privateEndpointConnectionName, context)
                .getValue();
        return this;
    }

    public PrivateEndpointConnectionDescriptionImpl withPrivateEndpoint(PrivateEndpoint privateEndpoint) {
        if (isInCreateMode()) {
            this.createProperties.withPrivateEndpoint(privateEndpoint);
            return this;
        } else {
            this.updateProperties.withPrivateEndpoint(privateEndpoint);
            return this;
        }
    }

    public PrivateEndpointConnectionDescriptionImpl withPrivateLinkServiceConnectionState(
        PrivateLinkServiceConnectionState privateLinkServiceConnectionState) {
        if (isInCreateMode()) {
            this.createProperties.withPrivateLinkServiceConnectionState(privateLinkServiceConnectionState);
            return this;
        } else {
            this.updateProperties.withPrivateLinkServiceConnectionState(privateLinkServiceConnectionState);
            return this;
        }
    }

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