package de.hsadmin.remote;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import de.hsadmin.core.model.AbstractEntity;
import de.hsadmin.core.util.TextUtil;
import de.hsadmin.mods.cust.Customer;
import de.hsadmin.mods.pac.BasePac;
import de.hsadmin.mods.pac.Hive;
import de.hsadmin.mods.pac.INetAddress;
import de.hsadmin.mods.pac.Pac;
import de.hsadmin.mods.pac.PacComponent;

public class PacRemote extends AbstractRemote {

	@Override
	protected void entity2map(AbstractEntity entity, Map<String, Object> resultMap) {
		Pac pac = (Pac) entity;
		resultMap.put("name", pac.getName());
		resultMap.put("id", Long.toString(pac.getId()));
		resultMap.put("hive", pac.getHiveName());
		resultMap.put("customer", pac.getCustomer().getName());
		resultMap.put("curinetaddr", pac.getCurINetAddr().getInetAddr());
		resultMap.put("created", TextUtil.format(pac.getCreated()));
		Date cancelled = pac.getCancelled();
		if (cancelled != null) {
			resultMap.put("cancelled", TextUtil.format(cancelled));
		}
		resultMap.put("basepac", pac.getBasepac().getName());
		Map<String, Object> components = new HashMap<String, Object>();
		Set<PacComponent> pacComponents = pac.getPacComponents();
		for (PacComponent comp : pacComponents) {
			int quantity = comp.getQuantity();
			if (quantity > 0) {
				components.put(comp.getBaseComponent().getFeature(), Integer.toString(quantity));
			}
		}
		resultMap.put("components", components);
	}

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

	@Override
	protected void map2entity(Map<String, Object> setParams, AbstractEntity entity) {
		Pac pac = (Pac) entity;
		BasePac basePac = pac.getBasepac();
		String basePacName = (String) setParams.get("basepac");
		if ((basePac == null || basePac.getName() == null) && assertNotNull(basePacName)) {
			basePac = new BasePac();
			basePac.setName(basePacName);
			pac.setBasepac(basePac);
		}
		String createdStr = (String) setParams.get("created");
		if (createdStr != null && !createdStr.isEmpty()) {
			Date createdDate = TextUtil.parse(createdStr);
			pac.setCreated(createdDate);
		}
		String cancelledStr = (String) setParams.get("cancelled");
		if (cancelledStr != null && !cancelledStr.isEmpty()) {
			Date cancelledDate = TextUtil.parse(cancelledStr);
			pac.setCancelled(cancelledDate);
		}
		INetAddress curINetAddr = pac.getCurINetAddr();
		String inetAddrString = (String) setParams.get("curinetaddr");
		if ((curINetAddr == null || curINetAddr.getInetAddr() == null) && assertNotNull(inetAddrString)) {
			curINetAddr = new INetAddress(inetAddrString);
			pac.setCurINetAddr(curINetAddr);
		}
		Customer customer = pac.getCustomer();
		String memberCode = (String) setParams.get("customer");
		if (customer == null && assertNotNull(memberCode)) {
			customer = new Customer();
			customer.setName(memberCode);
			pac.setCustomer(customer);
		}
		Hive hive = pac.getHive();
		String hiveName = (String) setParams.get("hive");
		if ((hive == null || hive.getName() == null) && assertNotNull(hiveName)) {
			hive = new Hive();
			hive.setName(hiveName);
			pac.setHive(hive);
		}
		Object componentsObj = setParams.get("components");
		if (componentsObj != null && componentsObj instanceof Map) {
			Map<?, ?> componentsMap = (Map<?, ?>) componentsObj;
			for (Object key : componentsMap.keySet()) {
				pac.getPacComponent(key.toString()).setQuantity(Integer.parseInt(componentsMap.get(key).toString()));
			}
		}
		Object nameParamObj = setParams.get("name");
		if (nameParamObj != null && nameParamObj instanceof String) {
			pac.setName((String) nameParamObj);
		}
	}

	@Override
	protected void regularizeKeys(Map<String, String> whereParams) {
		replaceKey(whereParams, "customer", "customer.name");
	}

}
