package com.microstar.tap3.contact.transform;

import com.microstar.tap3.contact.api.ContactObject;
import com.microstar.tap3.contact.api.ContactObjectAddress;
import com.microstar.tap3.contact.api.ContactObjectContact;
import com.microstar.tap3.db.Address;
import com.microstar.tap3.db.Contact;

public class ContactTransformer
{

	public static Contact transform(ContactObject co)
	{
		return transform(co, new Contact());
	}
	
	public static ContactObject transform(Contact cc, Address ca)
	{
		return transform(cc, ca, new ContactObject());
	}
	
	public static ContactObject transform(Contact cc, Address ca, ContactObject co)
	{
		if ( co == null )
			co = new ContactObject();
		
		if ( cc != null )
		{
			ContactObjectContact coc = co.getContact();
			if ( coc == null )
				coc = co.createContact(cc.getId(), cc.getPrefix(), cc.getFirstName(), cc.getLastName(), cc.getSuffix(), 
						cc.getJobTitle(), cc.getDeskPhone(), cc.getCellPhone(), cc.getEmailAddress(), cc.isReceiveOrderUpdates(), 
						cc.isReceiveInvoice(), cc.isReceiveBill(), cc.isReceiveNewsletter(), cc.getSfdcid(), 
						cc.getNotes(), cc.isReceiveEmptyKegCountRequests(), cc.isReceiveAckRequests(), cc.getReceiveAckRequestDelayHrs(), 
						cc.isDeleted(), cc.isDefaultForType());
			else
			{
				if (cc.getCellPhone() != null)
					coc.setCellPhone(cc.getCellPhone());
				if (cc.getDeskPhone() != null)
					coc.setDeskPhone(cc.getDeskPhone());
				if (cc.getEmailAddress() != null)
					coc.setEmailAddress(cc.getEmailAddress());
				if (cc.getFirstName() != null)
					coc.setFirstName(cc.getFirstName());
				if (cc.getJobTitle() != null)
					coc.setJobTitle(cc.getJobTitle());
				if (cc.getLastName() != null)
					coc.setLastName(cc.getLastName());
				if (cc.getPrefix() != null)
					coc.setPrefix(cc.getPrefix());
				coc.setReceiveBill(cc.isReceiveBill());
				coc.setReceiveInvoice(cc.isReceiveInvoice());
				coc.setReceiveNewsletter(cc.isReceiveNewsletter());
				coc.setReceiveOrderUpdates(cc.isReceiveOrderUpdates());
				coc.setReceiveEmptyKegCountRequests(cc.isReceiveEmptyKegCountRequests());
				coc.setReceiveAckRequests(cc.isReceiveAckRequests());
				coc.setReceiveAckRequestsDelayHrs(cc.getReceiveAckRequestDelayHrs());
				coc.setDeleted(cc.isDeleted());
				coc.setDefaultForType(cc.isDefaultForType());
				if (cc.getSfdcid() != null)
					coc.setSdfdid(cc.getSfdcid());
				if (cc.getSuffix() != null)
					coc.setSuffix(cc.getSuffix());
			}
			co.setContact(coc);
			co.setId(cc.getId());
		}
		
		if ( ca != null )
		{
			ContactObjectAddress coa = co.getAddress();
			if ( coa == null )
			{
				coa = co.createAddress(ca.getId(), ca.getAddressName(), ca.getAddress1(), ca.getAddress2(), 
						ca.getAddress3(), ca.getCity(), ca.getStateOrProvince(), ca.getPostalCode(), ca.getCountry(), 
						ca.getLat(), ca.getLon(), ca.getSfdcid(), ca.getNotes(), ca.isDefaultForType());
				coa.setAddressTypeId(ca.getAddressTypeId());
			}	
			else
			{
				if ( ca.getAddress1() != null )
					coa.setAddress1(ca.getAddress1());
				if ( ca.getAddress2() != null )
					coa.setAddress2(ca.getAddress2());
				if ( ca.getAddress3() != null )
					coa.setAddress3(ca.getAddress3());
				if ( ca.getAddressName() != null )
					coa.setAddressName(ca.getAddressName());
				if ( ca.getCity() != null )
					coa.setCity(ca.getCity());
				if ( ca.getCountry() != null )
					coa.setCountry(ca.getCountry());
				if ( ca.getLat() != null )
					coa.setLat(ca.getLat());
				if ( ca.getLon() != null )
					coa.setLon(ca.getLon());
				if ( ca.getPostalCode() != null )
					coa.setPostalCode(ca.getPostalCode());
				if ( ca.getSfdcid() != null )
					coa.setSfid(ca.getSfdcid());
				if ( ca.getStateOrProvince() != null )
					coa.setStateOrProvince(ca.getStateOrProvince());
				if ( ca.getAddressTypeId() != null )
					coa.setAddressTypeId(ca.getAddressTypeId());
			}
			co.setAddress(coa);
		}
		
//		if ( cc.getCustomerContactLinks() != null && cc.getCustomerContactLinks().size() == 1 )
//		{
//			co.setCustomerId(cc.getCustomerContactLinks().iterator().next().getCustomer().getId());
//		}
		
		return co;
	}
	
	public static Contact transform(ContactObject co, Contact c)
	{
		if ( co.getContact().getCellPhone() != null )
			c.setCellPhone(co.getContact().getCellPhone());
		if ( co.getContact().getDeskPhone() != null )
			c.setDeskPhone(co.getContact().getDeskPhone());
		if ( co.getContact().getEmailAddress() != null )
			c.setEmailAddress(co.getContact().getEmailAddress());
		if ( co.getContact().getFirstName() != null )
			c.setFirstName(co.getContact().getFirstName());
		if ( co.getContact().getId() != null )
			c.setId(co.getContact().getId());
		if ( co.getContact().getJobTitle() != null )
			c.setJobTitle(co.getContact().getJobTitle());
		if ( co.getContact().getLastName() != null )
			c.setLastName(co.getContact().getLastName());
		if ( co.getContact().getPrefix() != null )
			c.setPrefix(co.getContact().getPrefix());
		if (co.getContact().isReceiveBill() != null)
			c.setReceiveBill(co.getContact().isReceiveBill());
		if (co.getContact().isReceiveInvoice() != null)
			c.setReceiveInvoice(co.getContact().isReceiveInvoice());
		if (co.getContact().isReceiveNewsletter() != null)
			c.setReceiveNewsletter(co.getContact().isReceiveNewsletter());
		if (co.getContact().isReceiveOrderUpdates() != null)
			c.setReceiveOrderUpdates(co.getContact().isReceiveOrderUpdates());
		if ( co.getContact().getSdfdid() != null )
			c.setSfdcid(co.getContact().getSdfdid());
		if ( co.getContact().getSuffix() != null )
			c.setSuffix(co.getContact().getSuffix());
		if ( co.getContact().getContactTypeId() != null )
			c.setContactTypeId(co.getContact().getContactTypeId());
		
		return c;
	}
	
	public static Address transformAddress(ContactObject co)
	{
		return transform(co, new Address());
	}
	
	public static Address transform(ContactObject co, Address a)
	{
		if ( co.getAddress() == null )
			return null;
		
		if ( co.getAddress().getAddressTypeId() != null )
			a.setAddressTypeId(co.getAddress().getAddressTypeId());
		if ( co.getAddress().getAddress1() != null )
			a.setAddress1(co.getAddress().getAddress1());
		if ( co.getAddress().getAddress2() != null )
			a.setAddress2(co.getAddress().getAddress2());
		if ( co.getAddress().getAddress3() != null )
			a.setAddress3(co.getAddress().getAddress3());
		if ( co.getAddress().getAddressName() != null )
			a.setAddressName(co.getAddress().getAddressName());
		if ( co.getAddress().getCity() != null )
			a.setCity(co.getAddress().getCity());
		if ( co.getAddress().getCountry() != null )
			a.setCountry(co.getAddress().getCountry());
		if ( co.getAddress().getLat() != null )
			a.setLat(co.getAddress().getLat());
		if ( co.getAddress().getLon() != null )
			a.setLon(co.getAddress().getLon());
		if ( co.getAddress().getPostalCode() != null )
			a.setPostalCode(co.getAddress().getPostalCode());
		if ( co.getAddress().getSfid() != null )
			a.setSfdcid(co.getAddress().getSfid());
		if ( co.getAddress().getStateOrProvince() != null )
			a.setStateOrProvince(co.getAddress().getStateOrProvince());
		
		return a;
	}
}
