package de.hsadmin.remote;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;

import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.mods.cust.BankAccount;
import de.hsadmin.mods.cust.Contact;
import de.hsadmin.mods.cust.Customer;

public class CustomerRemote extends AbstractRemote {

	private static final DateFormat df = SimpleDateFormat.getDateInstance(DateFormat.SHORT);

	@Override
	protected Class<? extends AbstractEntity> getEntityClass() {
		return Customer.class;
	}

	@Override
	protected void entity2map(AbstractEntity entity,
			Map<String, String> resultMap) {
		Customer cust = (Customer) entity;
		resultMap.put("id", Long.toString(cust.getId()));
		resultMap.put("membercode", cust.getName());
		resultMap.put("memberno", Integer.toString(cust.getMemberNo()));
		Date memberSince = cust.getMemberSince();
		if (assertNotNull(memberSince)) {
			resultMap.put("membersince", df.format(memberSince));
		}
		Date memberUntil = cust.getMemberUntil();
		if (assertNotNull(memberUntil)) {
			resultMap.put("memberuntil", df.format(memberUntil));
		}
		resultMap.put("memberrole", cust.getMemberRole());
		Date authorContract = cust.getAuthorContract();
		if (assertNotNull(authorContract)) {
			resultMap.put("authorcontract", df.format(authorContract));
		}
		Date nonDiscContract = cust.getNonDiscContract();
		if (assertNotNull(nonDiscContract)) {
			resultMap.put("nondisccontract", df.format(nonDiscContract));
		}
		Date sharesUpdated = cust.getSharesUpdated();
		if (assertNotNull(sharesUpdated)) {
			resultMap.put("sharesupdated", df.format(sharesUpdated));
		}
		resultMap.put("sharessigned", Integer.toString(cust.getSharesSigned()));
		resultMap.put("uidvat", cust.getUidVAT());
		BankAccount bank = cust.getBankAccount();
		if (bank != null) {
			resultMap.put("autodebit_ga", bank.getAutoDebitGA() != null && bank.getAutoDebitGA() ? "Yes" : "No");
			resultMap.put("autodebit_ar", bank.getAutoDebitAR() != null && bank.getAutoDebitAR() ? "Yes" : "No");
			resultMap.put("autodebit_op", bank.getAutoDebitOP() != null && bank.getAutoDebitOP() ? "Yes" : "No");
			resultMap.put("bank_customer", bank.getBankCustomer());
			resultMap.put("bank_account", bank.getBankAccount());
			resultMap.put("bank_code", bank.getBankCode());
			resultMap.put("bank_name", bank.getBankName());
		}
		Set<Contact> contacts = cust.getContacts();
		if (contacts != null && !contacts.isEmpty()) {
			Contact c = contacts.iterator().next();
			resultMap.put("contact_salut", c.getSalut());
			resultMap.put("contact_title", c.getTitle());
			resultMap.put("contact_firstname", c.getFirstName());
			resultMap.put("contact_lastname", c.getLastName());
			resultMap.put("contact_firma", c.getFirma());
			resultMap.put("contact_co", c.getCo());
			resultMap.put("contact_street", c.getStreet());
			resultMap.put("contact_zipcode", c.getZipCode());
			resultMap.put("contact_city", c.getCity());
			resultMap.put("contact_country", c.getCountry());
			resultMap.put("contact_phone_private", c.getPhonePrivate());
			resultMap.put("contact_phone_office", c.getPhoneOffice());
			resultMap.put("contact_phone_mobile", c.getPhoneMobile());
			resultMap.put("contact_fax", c.getFax());
			resultMap.put("contact_email", c.getEmail());
		}
	}

	@Override
	protected void map2entity(Map<String, String> setParams,
			AbstractEntity entity) {
		Customer cust = (Customer) entity;
		String idStr = setParams.get("id");
		if (assertNotNull(idStr)) {
			cust.setId(Long.parseLong(idStr));
		}
		String memberCode = setParams.get("membercode");
		if (assertNotNull(memberCode)) {
			cust.setName(memberCode);
		}
		String password = setParams.get("password");
		if (assertNotNull(password)) {
			cust.setPassword(password);
		}
		String memberNo = setParams.get("memberno");
		if (assertNotNull(memberNo)) {
			cust.setMemberNo(Integer.parseInt(memberNo));
		}
		String memberSince = setParams.get("membersince");
		if (assertNotNull(memberSince)) {
			try {
				cust.setMemberSince(df.parse(memberSince));
			} catch (ParseException e) {
				// don't care
			}
		}
		String memberUntil = setParams.get("memberuntil");
		if (assertNotNull(memberUntil)) {
			try {
				cust.setMemberUntil(df.parse(memberUntil));
			} catch (ParseException e) {
				// don't care
			}
		}
		String memberRole = setParams.get("memberrole");
		if (assertNotNull(memberRole)) {
			cust.setMemberRole(memberRole);
		}
		String authorContract = setParams.get("authorcontract");
		if (assertNotNull(authorContract)) {
			try {
				cust.setAuthorContract(df.parse(authorContract));
			} catch (ParseException e) {
				// don't care
			}
		}
		String nonDiscContract = setParams.get("nondisccontract");
		if (assertNotNull(nonDiscContract)) {
			try {
				cust.setNonDiscContract(df.parse(nonDiscContract));
			} catch (ParseException e) {
				// don't care
			}
		}
		String sharesUpdated = setParams.get("sharesupdated");
		if (assertNotNull(sharesUpdated)) {
			try {
				cust.setSharesUpdated(df.parse(sharesUpdated));
			} catch (ParseException e) {
				// don't care
			}
		}
		String sharesSigned = setParams.get("sharessigned");
		if (assertNotNull(sharesSigned)) {
			cust.setId(Integer.parseInt(sharesSigned));
		}
		String uidVat = setParams.get("uidvat");
		if (assertNotNull(uidVat)) {
			cust.setUidVAT(uidVat);
		}
		BankAccount bank = cust.getBankAccount();
		if (bank == null) {
			bank = new BankAccount();
			bank.setCustomer(cust);
			cust.setBankAccount(bank);
		}
		String autoDebitGA = setParams.get("autodebit_ga");
		if (assertNotNull(autoDebitGA)) {
			autoDebitGA = autoDebitGA.toUpperCase();
			bank.setAutoDebitGA(new Boolean(autoDebitGA.startsWith("T") || autoDebitGA.startsWith("Y")));
		}
		String autoDebitAR = setParams.get("autodebit_ar");
		if (assertNotNull(autoDebitAR)) {
			autoDebitAR = autoDebitAR.toUpperCase();
			bank.setAutoDebitAR(new Boolean(autoDebitAR.startsWith("T") || autoDebitAR.startsWith("Y")));
		}
		String autoDebitOP = setParams.get("autodebit_op");
		if (assertNotNull(autoDebitOP)) {
			autoDebitOP = autoDebitOP.toUpperCase();
			bank.setAutoDebitOP(new Boolean(autoDebitOP.startsWith("T") || autoDebitOP.startsWith("Y")));
		}
		String bankCustomer = setParams.get("bank_customer");
		if (assertNotNull(bankCustomer)) {
			bank.setBankCustomer(bankCustomer);
		}
		String bankAccount = setParams.get("bank_account");
		if (assertNotNull(bankAccount)) {
			bank.setBankAccount(bankAccount);
		}
		String bankCode = setParams.get("bank_code");
		if (assertNotNull(bankCode)) {
			bank.setBankCode(bankCode);
		}
		String bankName = setParams.get("bank_name");
		if (assertNotNull(bankName)) {
			bank.setBankName(bankName);
		}
		Set<Contact> contacts = cust.getContacts();
		Contact c = null;
		if (contacts == null || contacts.isEmpty()) {
			c = new Contact(cust);
			contacts.add(c);
		} else {
			c = contacts.iterator().next();
		}
		String salut = setParams.get("contact_salut");
		if (assertNotNull(salut)) {
			c.setSalut(salut);
		}
		String title = setParams.get("contact_title");
		if (assertNotNull(title)) {
			c.setTitle(title);
		}
		String firstName = setParams.get("contact_firstname");
		if (assertNotNull(firstName)) {
			c.setFirstName(firstName);
		}
		String lastName = setParams.get("contact_lastname");
		if (assertNotNull(lastName)) {
			c.setLastName(lastName);
		}
		String firma = setParams.get("contact_firma");
		if (assertNotNull(firma)) {
			c.setLastName(firma);
		}
		String co = setParams.get("contact_co");
		if (assertNotNull(co)) {
			c.setCo(co);
		}
		String street = setParams.get("contact_street");
		if (assertNotNull(street)) {
			c.setStreet(street);
		}
		String zipCode = setParams.get("contact_zipcode");
		if (assertNotNull(zipCode)) {
			c.setZipCode(zipCode);
		}
		String city = setParams.get("contact_city");
		if (assertNotNull(city)) {
			c.setCity(city);
		}
		String country = setParams.get("contact_country");
		if (assertNotNull(country)) {
			c.setCountry(country);
		}
		String phonePrivate = setParams.get("contact_phone_private");
		if (assertNotNull(phonePrivate)) {
			c.setPhonePrivate(phonePrivate);
		}
		String phoneOffice = setParams.get("contact_phone_office");
		if (assertNotNull(phoneOffice)) {
			c.setPhoneOffice(phoneOffice);
		}
		String phoneMobile = setParams.get("contact_phone_mobile");
		if (assertNotNull(phoneMobile)) {
			c.setPhoneMobile(phoneMobile);
		}
		String fax = setParams.get("contact_fax");
		if (assertNotNull(fax)) {
			c.setFax(fax);
		}
		String eMail = setParams.get("contact_email");
		if (assertNotNull(eMail)) {
			c.setEmail(eMail);
		}
}

	@Override
	protected void regularizeKeys(Map<String, String> whereParams) {
		// nothing to do
		replaceKey(whereParams, "membercode", "name");
		replaceKey(whereParams, "memberno", "memberNo");
		replaceKey(whereParams, "membersince", "memberSince");
		replaceKey(whereParams, "memberuntil", "memberUntil");
	}

}
