package org.jboss.resteasy.test.providers.jackson2.jsonfilter.resource;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.PropertyFilter;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.jaxrs.cfg.EndpointConfigBase;
import com.fasterxml.jackson.jaxrs.cfg.ObjectWriterModifier;

import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;

public class ObjectFilterModifierConditional extends ObjectWriterModifier {
   @Override
   public ObjectWriter modify(EndpointConfigBase<?> endpointConfigBase, MultivaluedMap<String, Object> multivaluedMap,
                               Object o, ObjectWriter objectWriter, JsonGenerator jsonGenerator) throws IOException {

      PropertyFilter theFilter = new SimpleBeanPropertyFilter() {
         @Override
         public void serializeAsField
               (Object pojo, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer)
               throws Exception {
            if (include(writer)) {
               if (!writer.getName().equals("id")) {
                  writer.serializeAsField(pojo, jgen, provider);
                  return;
               }
               int intValue = ((Jackson2Product) pojo).getId();
               if (intValue >= 0) {
                  writer.serializeAsField(pojo, jgen, provider);
               }
            } else if (!jgen.canOmitFields()) { // since 2.3
               writer.serializeAsOmittedField(pojo, jgen, provider);
            }
         }
         @Override
         protected boolean include(BeanPropertyWriter writer) {
            return true;
         }
         @Override
         protected boolean include(PropertyWriter writer) {
            return true;
         }
      };

      FilterProvider filterProvider = new SimpleFilterProvider().addFilter(
            "nameFilter", theFilter);
      return objectWriter.with(filterProvider);
   }
}
