package de.hsadmin.mods.cust;

import java.util.Calendar;
import java.util.List;

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

public class CustomerModuleImpl extends AbstractModuleImpl {

	@Override
	public AbstractEntity add(AbstractEntity newEntity) throws HSAdminException {
		if (!getLoginUser().hasHostmasterRole()) {
			throw new HSAdminException("role hostmaster required to create new customer");
		}
		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();
		}
		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 {
		if (!getLoginUser().hasHostmasterRole()) {
			throw new HSAdminException("role hostmaster required to update customers");
		}
		return super.update(existingEntity);
	}

	@Override
	public void delete(AbstractEntity existingEntity) throws HSAdminException {
		if (!getLoginUser().hasHostmasterRole()) {
			throw new HSAdminException("role hostmaster required to delete customers");
		}
		Customer cust = (Customer) existingEntity;
//		Diese Zeilen loeschen den hsh00-Mitglieds-Account 
//		GenericModuleImpl helper = new GenericModuleImpl(getTransaction());
//		AbstractEntity custAccount = helper.findByString(UnixUser.class, cust.getName());
//		helper.delete(custAccount);
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(System.currentTimeMillis());
		cal.set(Calendar.MONTH, Calendar.DECEMBER);
		cal.set(Calendar.DAY_OF_MONTH, 31);
		cal.set(Calendar.HOUR_OF_DAY, 23);
		cal.set(Calendar.MINUTE, 59);
		cal.set(Calendar.SECOND, 59);
		cust.setMemberUntil(cal.getTime());
		super.update(existingEntity);
	}

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

}
