package info.bukova.isspst.services.addressbook;

import info.bukova.isspst.data.Address;
import info.bukova.isspst.services.AbstractOwnedService;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

public class AdbServiceImpl extends AbstractOwnedService<Address> implements AdbService {
	
	@Override
	public List<Address> lookForAddr(AddressFinder finder, Address condAddr) {
		return finder.findAddress(condAddr);
	}

	@Override
	public void mergeAddress(Address destination, Address source, boolean overwrite) {
		if (destination.getIc() == 0)
			destination.setIc(source.getIc());
		if (destination.getCompany() == null || destination.getCompany().isEmpty() || overwrite)
			destination.setCompany(source.getCompany());
		if (destination.getDic() == null || destination.getDic().isEmpty() || overwrite)
			destination.setDic(source.getDic());
		if (destination.getDepartment() == null || destination.getDepartment().isEmpty() || overwrite)
			destination.setDepartment(source.getDepartment());
		if (destination.getContactName() == null || destination.getContactName().isEmpty() || overwrite)
			destination.setContactName(source.getContactName());
		if (destination.getStreet() == null || destination.getStreet().isEmpty() || overwrite)
			destination.setStreet(source.getStreet());
		if (destination.getHouseNumber() == null || destination.getHouseNumber().isEmpty() || overwrite)
			destination.setHouseNumber(source.getHouseNumber());
		if (destination.getCity() == null || destination.getCity().isEmpty() || overwrite)
			destination.setCity(source.getCity());
		if (destination.getZipCode() == null || destination.getZipCode().isEmpty() || overwrite)
			destination.setZipCode(source.getZipCode());
		if (destination.getPhone() == null || destination.getPhone().isEmpty() || overwrite)
			destination.setPhone(source.getPhone());
		if (destination.getEmail() == null || destination.getEmail().isEmpty() || overwrite)
			destination.setEmail(source.getEmail());
	}

	@Override
	public void fillFoundData(AddressFinder finder, Address address) {
		List<Address> found = finder.findAddress(address);
		
		if (found.size() > 0) {
			this.mergeAddress(address, found.get(0), false);
		}
	}
	
	
	@Override
	public List<Address> queryToArrayList(String filter) {
		return new ArrayList<Address>(execQuery(filter));
	}

	@Override
	@Transactional
	public int count() {
//		Query q = queryDefaultFilter();
//		q.setResult("count(id)");
//		Object result = q.execute();
//				
//		return ((Long)result).intValue();
		return 0;
	}

}
