// 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.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.healthcareapis.fluent.models.DicomServiceInner;
import com.azure.resourcemanager.healthcareapis.models.DicomService;
import com.azure.resourcemanager.healthcareapis.models.DicomServiceAuthenticationConfiguration;
import com.azure.resourcemanager.healthcareapis.models.DicomServicePatchResource;
import com.azure.resourcemanager.healthcareapis.models.PrivateEndpointConnection;
import com.azure.resourcemanager.healthcareapis.models.ProvisioningState;
import com.azure.resourcemanager.healthcareapis.models.PublicNetworkAccess;
import com.azure.resourcemanager.healthcareapis.models.ServiceManagedIdentityIdentity;
import java.util.Collections;
import java.util.List;
import java.util.Map;

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

    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 SystemData systemData() {
        return this.innerModel().systemData();
    }

    public ServiceManagedIdentityIdentity identity() {
        return this.innerModel().identity();
    }

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

    public DicomServiceAuthenticationConfiguration authenticationConfiguration() {
        return this.innerModel().authenticationConfiguration();
    }

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

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

    public PublicNetworkAccess publicNetworkAccess() {
        return this.innerModel().publicNetworkAccess();
    }

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

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

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

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

    private String resourceGroupName;

    private String workspaceName;

    private String dicomServiceName;

    private DicomServicePatchResource updateDicomservicePatchResource;

    public DicomServiceImpl withExistingWorkspace(String resourceGroupName, String workspaceName) {
        this.resourceGroupName = resourceGroupName;
        this.workspaceName = workspaceName;
        return this;
    }

    public DicomService create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDicomServices()
                .createOrUpdate(resourceGroupName, workspaceName, dicomServiceName, this.innerModel(), Context.NONE);
        return this;
    }

    public DicomService create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDicomServices()
                .createOrUpdate(resourceGroupName, workspaceName, dicomServiceName, this.innerModel(), context);
        return this;
    }

    DicomServiceImpl(String name, com.azure.resourcemanager.healthcareapis.HealthcareApisManager serviceManager) {
        this.innerObject = new DicomServiceInner();
        this.serviceManager = serviceManager;
        this.dicomServiceName = name;
    }

    public DicomServiceImpl update() {
        this.updateDicomservicePatchResource = new DicomServicePatchResource();
        return this;
    }

    public DicomService apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDicomServices()
                .update(
                    resourceGroupName, dicomServiceName, workspaceName, updateDicomservicePatchResource, Context.NONE);
        return this;
    }

    public DicomService apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDicomServices()
                .update(resourceGroupName, dicomServiceName, workspaceName, updateDicomservicePatchResource, context);
        return this;
    }

    DicomServiceImpl(
        DicomServiceInner innerObject, com.azure.resourcemanager.healthcareapis.HealthcareApisManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.dicomServiceName = Utils.getValueFromIdByName(innerObject.id(), "dicomservices");
        this.workspaceName = Utils.getValueFromIdByName(innerObject.id(), "workspaces");
    }

    public DicomService refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDicomServices()
                .getWithResponse(resourceGroupName, workspaceName, dicomServiceName, Context.NONE)
                .getValue();
        return this;
    }

    public DicomService refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDicomServices()
                .getWithResponse(resourceGroupName, workspaceName, dicomServiceName, context)
                .getValue();
        return this;
    }

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

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

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

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

    public DicomServiceImpl withIdentity(ServiceManagedIdentityIdentity identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateDicomservicePatchResource.withIdentity(identity);
            return this;
        }
    }

    public DicomServiceImpl withAuthenticationConfiguration(
        DicomServiceAuthenticationConfiguration authenticationConfiguration) {
        this.innerModel().withAuthenticationConfiguration(authenticationConfiguration);
        return this;
    }

    public DicomServiceImpl withPublicNetworkAccess(PublicNetworkAccess publicNetworkAccess) {
        this.innerModel().withPublicNetworkAccess(publicNetworkAccess);
        return this;
    }

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