package ogloszenia.rest;

import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import javax.servlet.ServletContext;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.MessageBodyWriter;
import javax.ws.rs.ext.Provider;

import ogloszenia.model.ListaOgloszen;
import ogloszenia.model.OgloszenieSamochodowe;
import ogloszenia.xsl.WsparcieXSL;

@Provider
public class HTMLWriter implements MessageBodyWriter<Object> {
	@Context
	private ServletContext servletContext;
	
	@Override
	public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
		
		return (type == OgloszenieSamochodowe.class
				|| type == ListaOgloszen.class)
				&& MediaType.TEXT_HTML_TYPE.isCompatible(mediaType);
	}

	@Override
	public void writeTo(Object obiekt, Class<?> type, Type genericType, Annotation[] annotations,
			MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream)
			throws IOException, WebApplicationException {

		WsparcieXSL wsparcieXSL = new WsparcieXSL(servletContext);
		wsparcieXSL.wypiszHtml(obiekt, entityStream);
	}

	@Override
	public long getSize(Object obiekt, Class<?> type, Type genericType, Annotation[] annotations,
			MediaType mediaType) {
		return -1;
	}
}
