package de.hsadmin.mods.cust;

import java.util.Date;
import java.util.List;
import java.util.Set;

import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.model.AbstractModuleImpl;
import de.hsadmin.core.model.AuthorisationException;
import de.hsadmin.core.model.GenericModuleImpl;
import de.hsadmin.core.model.HSAdminException;
import de.hsadmin.core.model.Transaction;
import de.hsadmin.core.util.TextUtil;
import de.hsadmin.mods.pac.Pac;
import de.hsadmin.mods.user.UnixUser;

public class CustomerModuleImpl extends AbstractModuleImpl {

	@Override
	public AbstractEntity add(AbstractEntity newEntity) throws HSAdminException {
		Transaction transaction = getTransaction();
		if (!transaction.getLoginUser().hasHostmasterRole()) {
			throw new AuthorisationException(transaction.getLoginUser(), "add", newEntity);
		}
		Customer newCustomer = (Customer) newEntity;
		assertNotNull("membercode", newCustomer.getName());
		assertNotNull("password", newCustomer.getPassword());
		Contact contact = newCustomer.getContacts().iterator().next();
		assertNotNull("contact_lastname", contact.getLastName());
		String custComment = contact.getLastName();
		if (contact.getFirstName() != null && contact.getFirstName().length() > 0) {
			custComment = contact.getFirstName() + " " + contact.getLastName();
		}
		BankAccount bankAccount = newCustomer.getBankAccount();
		if (bankAccount == null) {
			bankAccount = new BankAccount();
			bankAccount.setCustomer(newCustomer);
			newCustomer.setBankAccount(bankAccount);
		}
		GenericModuleImpl helperModule = new GenericModuleImpl(getTransaction());
		UnixUser custAccount = new UnixUser();
		custAccount.setComment(TextUtil.replaceUmlautCharacters(custComment));
		custAccount.setName(newCustomer.getName());
		custAccount.setPassword(newCustomer.getPassword());
		custAccount.setShell("/usr/bin/passwd");
		custAccount.setQuotaSoftlimit(8);
		custAccount.setQuotaHardlimit(12);
		helperModule.add(custAccount);
		return super.add(newEntity);
	}

	@Override
	public List<AbstractEntity> search(
			Class<? extends AbstractEntity> entityClass, String condition,
			String orderBy) throws HSAdminException {
		return super.search(entityClass, condition, orderBy);
	}

	@Override
	public AbstractEntity update(AbstractEntity existingEntity)
			throws HSAdminException {
		Transaction transaction = getTransaction();
		if (!transaction.getLoginUser().hasHostmasterRole()) {
			throw new AuthorisationException(transaction.getLoginUser(), "update", existingEntity);
		}
		return super.update(existingEntity);
	}

	@Override
	public void delete(AbstractEntity existingEntity) throws HSAdminException {
		Transaction transaction = getTransaction();
		if (!transaction.getLoginUser().hasHostmasterRole()) {
			throw new AuthorisationException(transaction.getLoginUser(), "delete", existingEntity);
		}
		Customer cust = (Customer) existingEntity;
		
		// Pruefe, ob geloescht werden kann..
		// member_until < heute (nicht mehr Mitglied) oder member_since leer (nie Mitglied geworden) 
		Date memberSince = cust.getMemberSince();
		Date memberUntil = cust.getMemberUntil();
		if (memberSince != null) {
			if (memberUntil == null || memberUntil.after(new Date())) {
				throw new AuthorisationException(transaction.getLoginUser(), "delete", existingEntity);
			}
		}
		// keine Pakete mehr!
		Set<Pac> pacs = cust.getPacs();
		if (pacs != null && pacs.size() > 0) {
			throw new AuthorisationException(transaction.getLoginUser(), "delete", existingEntity);
		}
		
		GenericModuleImpl helper = new GenericModuleImpl(getTransaction());
		AbstractEntity custAccount = helper.findByString(UnixUser.class, cust.getName());
		helper.delete(custAccount);
		super.delete(existingEntity);
	}

	private void assertNotNull(String name, String value) throws HSAdminException {
		if (value == null || value.length() < 1) {
			throw new HSAdminException("field '" + name + "' is mandatory");
		}
	}

}
