package au.com.noojee.acceloapi.dao;

import java.util.List;

import au.com.noojee.acceloapi.AcceloException;
import au.com.noojee.acceloapi.AcceloFieldList;
import au.com.noojee.acceloapi.AcceloResponse;
import au.com.noojee.acceloapi.AcceloResponseList;
import au.com.noojee.acceloapi.EndPoint;
import au.com.noojee.acceloapi.entities.Affiliation;
import au.com.noojee.acceloapi.entities.Company;
import au.com.noojee.acceloapi.entities.Contact;
import au.com.noojee.acceloapi.entities.meta.Contact_;
import au.com.noojee.acceloapi.entities.types.AgainstType;
import au.com.noojee.acceloapi.filter.AcceloFilter;

public class ContactDao extends AcceloDao<Contact>
{

	public Contact getContact(String contact_firstname, String contact_lastname)
			throws AcceloException
	{
		Contact contact = null;

		if (contact_firstname != null && contact_lastname != null)
		{
			AcceloFilter<Contact> filter = new AcceloFilter<>();
			filter.where(filter.search(contact_firstname + " " + contact_lastname));
			List<Contact> contacts = getByFilter(filter);
			contact = contacts.size() > 0 ? contacts.get(0) : null;
		}

		return contact;
	}

	public List<Contact> getByPhone(String phone) throws AcceloException
	{
		AcceloFilter<Contact> filters = new AcceloFilter<>();
		filters.where(filters.eq(Contact_.contact_number, phone));

		List<Contact> contacts = getByFilter(filters);

		return contacts;

	}

	@Override
	protected AcceloFieldList getFieldList()
	{
		AcceloFieldList fields = new AcceloFieldList();
		fields.add(AcceloFieldList._ALL);
		fields.add(Company.FIELDS_ALL);
		return fields;
	}

	/**
	 * Gets the default company the contact is attached to.
	 * 
	 * @param contact
	 * @return
	 */
	public Company getDefaultCompany(Contact contact)
	{
		Affiliation affiliation = new AffiliationDao().getById(contact.getDefaultAffiliation());

		return new CompanyDao().getById(affiliation.getCompanyId());
	}

	/**
	 * Get the list of contacts attached to this company
	 * 
	 * @param id
	 */
	public List<Contact> getByCompany(int companyId)
	{
		AcceloFilter<Contact> filter = new AcceloFilter<>();
		filter.where(filter.against(AgainstType.company, companyId));

		return super.getByFilter(filter);
	}

	
	@Override
	protected EndPoint getEndPoint()
	{
		return EndPoint.contacts;
	}

	@Override
	protected Class<Contact> getEntityClass()
	{
		return Contact.class;
	}
	
	public class ResponseList extends AcceloResponseList<Contact>
	{
	}

	public class Response extends AcceloResponse<Contact>
	{
	}

	@Override
	protected Class<ContactDao.ResponseList> getResponseListClass()
	{
		return ContactDao.ResponseList.class;
	}

	@Override
	protected Class<? extends AcceloResponse<Contact>> getResponseClass()
	{
		return ContactDao.Response.class;
	}


}

@@ -1835,24 +1835,52 @@
 iation());%0A%0A
+%09%09if (affiliation != null)%0A%09
 %09%09return new
@@ -1930,16 +1930,31 @@
 yId());%0A
+%09%09return null;%0A
 %09%7D%0A%0A%09/**
@@ -2245,18 +2245,16 @@
 r);%0A%09%7D%0A%0A
-%09%0A
 %09@Overri
@@ -2413,10 +2413,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09pub
@@ -2790,11 +2790,439 @@
 ss;%0A%09%7D%0A%0A
+%09public String getMobile(Contact contact)%0A%09%7B%0A%09%09String mobile = null;%0A%0A%09%09AffiliationDao daoAffiliation = new AffiliationDao();%0A%0A%09%09int affiliationId = contact.getDefaultAffiliation();%0A%09%09if (affiliationId != 0)%0A%09%09%7B%0A%09%09%09Affiliation affiliation = daoAffiliation.getById(affiliationId);%0A%09%09%09mobile = affiliation.getMobile();%0A%09%09%7D%0A%0A%09%09if (mobile == null %7C%7C mobile.trim().length() == 0)%0A%09%09%09mobile = contact.getPhone();%0A%0A%09%09return mobile;%0A%09%7D%0A
 %0A%7D%0A

add null check if affiliation is null.