package be.ac.ulb.code.wit.resources.serializers.helpers;

import javax.ws.rs.core.MediaType;

import be.ac.ulb.code.wit.contacts.Contacts;
import be.ac.ulb.code.wit.contacts.Contacts.IdentifiedContact;
import be.ac.ulb.code.wit.resources.serializers.atom.AtomContactDeserializer;
import be.ac.ulb.code.wit.resources.serializers.atom.AtomContactSerializer;
import be.ac.ulb.code.wit.resources.serializers.atom.AtomGroupDeserializer;
import be.ac.ulb.code.wit.resources.serializers.atom.AtomGroupSerializer;
import be.ac.ulb.code.wit.resources.serializers.json.JsonContactDeserializer;
import be.ac.ulb.code.wit.resources.serializers.json.JsonContactSerializer;
import be.ac.ulb.code.wit.resources.serializers.json.JsonGroupDeserializer;
import be.ac.ulb.code.wit.resources.serializers.json.JsonGroupSerializer;

public class SerializationFactory {
	private static boolean isAtomContentType(MediaType mediaType) {
		return MediaType.APPLICATION_ATOM_XML_TYPE.isCompatible(mediaType);
	}

	private static boolean isJsonContentType(MediaType mediaType) {
		return mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
	}

	public static ContactSerializer createSerializer(MediaType mediaType,
			Contacts contacts, String group) {

		if (isAtomContentType(mediaType)) {
			return new AtomContactSerializer(contacts, group);
		} else if (isJsonContentType(mediaType)) {
			return new JsonContactSerializer(contacts, group);
		}

		throw new NoRepresentationException();
	}

	public static ContactDeserializer createDeserializer(MediaType mediaType,
			Contacts contacts, IdentifiedContact contact, String group) {

		if (isAtomContentType(mediaType)) {
			return new AtomContactDeserializer(contacts, contact, group);
		} else if (isJsonContentType(mediaType)) {
			return new JsonContactDeserializer(contacts, contact, group);
		}

		throw new NoRepresentationException();
	}

	public static ContactDeserializer createDeserializer(MediaType mediaType,
			Contacts contacts, String group) {

		if (isAtomContentType(mediaType)) {
			return new AtomContactDeserializer(contacts, group);
		} else if (isJsonContentType(mediaType)) {
			return new JsonContactDeserializer(contacts, group);
		}

		throw new NoRepresentationException();
	}

	public static GroupSerializer createGroupSerializer(MediaType mediaType,
			Contacts contacts) {

		if (isAtomContentType(mediaType)) {
			return new AtomGroupSerializer(contacts);
		} else if (isJsonContentType(mediaType)) {
			return new JsonGroupSerializer(contacts);
		}

		throw new NoRepresentationException();
	}

	public static GroupDeserializer createGroupDeserializer(
			MediaType mediaType, Contacts contacts, String group) {

		if (isAtomContentType(mediaType)) {
			return new AtomGroupDeserializer(contacts, group);
		} else if (isJsonContentType(mediaType)) {
			return new JsonGroupDeserializer(contacts, group);
		}

		throw new NoRepresentationException();
	}

	public static GroupDeserializer createGroupDeserializer(
			MediaType mediaType, Contacts contacts) {

		if (isAtomContentType(mediaType)) {
			return new AtomGroupDeserializer(contacts);
		} else if (isJsonContentType(mediaType)) {
			return new JsonGroupDeserializer(contacts);
		}

		throw new NoRepresentationException();
	}

}
