package io.quarkus.resteasy.reactive.qute.runtime;

import java.util.List;
import java.util.stream.Collectors;

import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.core.MediaType;

import org.jboss.resteasy.reactive.server.ServerResponseFilter;
import org.jboss.resteasy.reactive.server.spi.ResteasyReactiveContainerRequestContext;

import io.quarkus.qute.TemplateInstance;
import io.quarkus.qute.Variant;

public class TemplateResponseFilter {

    @ServerResponseFilter
    public void filter(ResteasyReactiveContainerRequestContext requestContext, ContainerResponseContext responseContext) {
        Object entity = responseContext.getEntity();
        if (!(entity instanceof TemplateInstance)) {
            return;
        }

        requestContext.suspend();
        MediaType mediaType;
        TemplateInstance instance = (TemplateInstance) entity;
        Object variantsAttr = instance.getAttribute(TemplateInstance.VARIANTS);
        if (variantsAttr != null) {
            @SuppressWarnings("unchecked")
            List<Variant> variants = (List<Variant>) variantsAttr;
            javax.ws.rs.core.Variant selected = requestContext.getRequest()
                    .selectVariant(variants.stream()
                            .map(v -> new javax.ws.rs.core.Variant(MediaType.valueOf(v.getMediaType()), v.getLocale(),
                                    v.getEncoding()))
                            .collect(Collectors.toList()));
            if (selected != null) {
                instance.setAttribute(TemplateInstance.SELECTED_VARIANT,
                        new Variant(selected.getLanguage(), selected.getMediaType().toString(), selected.getEncoding()));
                mediaType = selected.getMediaType();
            } else {
                // TODO we should use the default
                mediaType = null;
            }
        } else {
            // TODO how to get media type from non-variant templates?
            mediaType = null;
        }

        try {
            instance.renderAsync()
                    .whenComplete((r, t) -> {
                        if (t == null) {
                            // make sure we avoid setting a null media type because that causes
                            // an NPE further down
                            if (mediaType != null) {
                                responseContext.setEntity(r, null, mediaType);
                            } else {
                                responseContext.setEntity(r);
                            }
                            requestContext.resume();
                        } else {
                            requestContext.resume(t);
                        }
                    });
        } catch (Throwable t) {
            requestContext.resume(t);
        }
    }
}

@@ -420,16 +420,47 @@
 Variant;
+%0Aimport io.smallrye.mutiny.Uni;
 %0A%0Apublic
@@ -529,20 +529,25 @@
  public 
-v
+Uni%3CV
 oid
+%3E
  filter(
@@ -770,16 +770,21 @@
   return
+ null
 ;%0A      
@@ -792,42 +792,8 @@
  %7D%0A%0A
-        requestContext.suspend();%0A
     
@@ -2011,25 +2011,14 @@
     
-try %7B%0A           
+return
  ins
@@ -2027,272 +2027,33 @@
 nce.
+c
 re
-nderAsync()%0A                    .whenComplete((r, t) -%3E %7B%0A                        if (t == null) %7B%0A                            // make sure we avoid setting a null media type because that causes%0A                            // an NPE further down%0A                
+ateUni().chain(r -%3E %7B%0A
     
@@ -2085,32 +2085,16 @@
 null) %7B%0A
-                
         
@@ -2164,49 +2164,17 @@
     
-                %7D else %7B%0A                
+%7D else %7B%0A
     
@@ -2231,283 +2231,55 @@
     
-     
-           %7D%0A                            requestContext.resume();%0A                        %7D else %7B%0A                            requestContext.resume(t);%0A                        %7D%0A                    %7D);%0A        %7D catch (Throwable t) %7B%0A            requestContext.resume(t
+%7D%0A            return Uni.createFrom().nullItem(
 );%0A 
@@ -2282,21 +2282,23 @@
 );%0A        %7D
+);
 %0A    %7D%0A%7D%0A

Simplify TemplateResponseFilter by returning Uni and letting RR take care of the plumbing