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

package com.azure.resourcemanager.synapse.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.synapse.fluent.models.PrivateLinkHubInner;
import com.azure.resourcemanager.synapse.models.PrivateEndpointConnectionForPrivateLinkHubBasic;
import com.azure.resourcemanager.synapse.models.PrivateLinkHub;
import com.azure.resourcemanager.synapse.models.PrivateLinkHubPatchInfo;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class PrivateLinkHubImpl implements PrivateLinkHub, PrivateLinkHub.Definition, PrivateLinkHub.Update {
    private PrivateLinkHubInner innerObject;

    private final com.azure.resourcemanager.synapse.SynapseManager 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 provisioningState() {
        return this.innerModel().provisioningState();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.synapse.SynapseManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String privateLinkHubName;

    private PrivateLinkHubPatchInfo updatePrivateLinkHubPatchInfo;

    public PrivateLinkHubImpl withExistingResourceGroup(String resourceGroupName) {
        this.resourceGroupName = resourceGroupName;
        return this;
    }

    public PrivateLinkHub create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateLinkHubs()
                .createOrUpdateWithResponse(resourceGroupName, privateLinkHubName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public PrivateLinkHub create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateLinkHubs()
                .createOrUpdateWithResponse(resourceGroupName, privateLinkHubName, this.innerModel(), context)
                .getValue();
        return this;
    }

    PrivateLinkHubImpl(String name, com.azure.resourcemanager.synapse.SynapseManager serviceManager) {
        this.innerObject = new PrivateLinkHubInner();
        this.serviceManager = serviceManager;
        this.privateLinkHubName = name;
    }

    public PrivateLinkHubImpl update() {
        this.updatePrivateLinkHubPatchInfo = new PrivateLinkHubPatchInfo();
        return this;
    }

    public PrivateLinkHub apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateLinkHubs()
                .updateWithResponse(resourceGroupName, privateLinkHubName, updatePrivateLinkHubPatchInfo, Context.NONE)
                .getValue();
        return this;
    }

    public PrivateLinkHub apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateLinkHubs()
                .updateWithResponse(resourceGroupName, privateLinkHubName, updatePrivateLinkHubPatchInfo, context)
                .getValue();
        return this;
    }

    PrivateLinkHubImpl(
        PrivateLinkHubInner innerObject, com.azure.resourcemanager.synapse.SynapseManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.privateLinkHubName = Utils.getValueFromIdByName(innerObject.id(), "privateLinkHubs");
    }

    public PrivateLinkHub refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateLinkHubs()
                .getByResourceGroupWithResponse(resourceGroupName, privateLinkHubName, Context.NONE)
                .getValue();
        return this;
    }

    public PrivateLinkHub refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPrivateLinkHubs()
                .getByResourceGroupWithResponse(resourceGroupName, privateLinkHubName, context)
                .getValue();
        return this;
    }

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

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

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

    public PrivateLinkHubImpl withProvisioningState(String provisioningState) {
        this.innerModel().withProvisioningState(provisioningState);
        return this;
    }

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