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

import java.io.Reader;

import org.apache.abdera.model.Entry;
import org.apache.abdera.parser.ParseException;
import org.apache.commons.lang3.StringUtils;

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.helpers.ContactDeserializer;
import be.ac.ulb.code.wit.resources.serializers.helpers.MalformedRequestException;
import be.ac.ulb.code.wit.resources.serializers.helpers.NotFoundException;

/**
 * An atom deserializer that create, update or delete 
 * contacts, using data from atom entries.
 * 
 * @author fpicalausa
 *
 */
public class AtomContactDeserializer extends ContactDeserializer {
    public AtomContactDeserializer(Contacts contacts, String group)
			throws NotFoundException {
		super(contacts, group);
	}
    
    public AtomContactDeserializer(Contacts contacts,
			IdentifiedContact existingContact, String group)
			throws NotFoundException {
		super(contacts, existingContact, group);
	}

	/**
     * Update the current contact with data from the given atom entry.
     * If checkid is <tt>true</tt> the atom:id field of the entry
     * if checked for correspondence with an existing contact. If
     * no contact had been created and the atom:id corresponds to
     * a contact, that contact is update instead of creating a new one.
     * 
     * @param entry The atom entry to be parsed
     * @param checkid Whether the atom:id field should be parsed or not.
     */
	public void updateContact(Entry entry, boolean checkid) {
		String firstName, lastName, email, displayName;
        boolean hasDisplayName;

		//Fetch the data for building a new contact
		firstName = AtomHelpers.getExtensionElementText(entry, AtomHelpers.ExtensionElements.CONTACT_FIRSTNAME);
		lastName = AtomHelpers.getExtensionElementText(entry, AtomHelpers.ExtensionElements.CONTACT_LASTNAME);
		email = AtomHelpers.getExtensionElementText(entry, AtomHelpers.ExtensionElements.CONTACT_EMAIL);
        hasDisplayName = !StringUtils.isBlank(entry.getTitle());
		displayName = hasDisplayName ? entry.getTitle().trim() : null;

		if (checkid && entry.getId() != null) {
			updateContactFromId(entry.getId().toString());
		}
		
		//Create/Update the contact from the parsed information
		updateContact(firstName, lastName, email, displayName, hasDisplayName);
	}

    /**
     * Update the current contact with data from the given atom entry.
     * If checkid is <tt>true</tt> the atom:id field of the entry
     * if checked for correspondence with an existing contact. If
     * no contact had been created and the atom:id corresponds to
     * a contact, that contact is update instead of creating a new one.
     * 
     * @param entry The atom entry to be parsed
     * @param checkid Whether the atom:id field should be parsed or not.
     */
	public void updateContact(Reader data, boolean checkid) {
		try {
			updateContact(AtomHelpers.parseEntry(data), checkid);
		}
		catch (ParseException e) {
			throw new MalformedRequestException(e);
		}
	}
}
