package inc.monsters.mias.backing;

import inc.monsters.mias.data.Kid;
import inc.monsters.mias.data.KidsList;
import inc.monsters.mias.data.facade.KidService;

import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.apache.myfaces.orchestra.viewController.annotations.InitView;
import org.apache.myfaces.orchestra.viewController.annotations.ViewController;
import org.apache.myfaces.trinidad.component.UIXTable;
import org.apache.myfaces.trinidad.component.core.nav.CoreCommandButton;
import org.apache.myfaces.trinidad.context.RequestContext;
import org.apache.myfaces.trinidad.event.SelectionEvent;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@ViewController(viewIds={"Kids.xhtml"})
public class KidsTable {
	private KidService kidService;
	private static final Logger log = Logger.getLogger("inc.monsters.mias");
	private UIXTable table;
	private CoreCommandButton deleteButton;
	private boolean deleteEnabled = false;

	public KidService getKidService() {
		return kidService;
	}

	public void setKidService(KidService kidService) {
		this.kidService = kidService;
	}

	@InitView
	public void initView() {
		
	}
	
	public UIXTable getTable() {
		return table;
	}

	public void setTable(UIXTable table) {
		this.table = table;
	}
	
	public CoreCommandButton getDeleteButton() {
		return deleteButton;
	}

	public void setDeleteButton(CoreCommandButton deleteButton) {
		this.deleteButton = deleteButton;
	}

	public boolean isDeleteEnabled() {
		return deleteEnabled;
	}

	public void setDeleteEnabled(boolean deleteEnabled) {
		this.deleteEnabled = deleteEnabled;
	}

	public void selectionChanged(SelectionEvent e) {
		log.fine("selectionChanged(): " + getTable().getSelectedRowKeys().size());
		boolean oldValue = isDeleteEnabled();
		setDeleteEnabled(getTable().getSelectedRowKeys().size() > 0);
		if(oldValue != isDeleteEnabled()) {
			RequestContext rc = RequestContext.getCurrentInstance();
		    rc.addPartialTarget(getDeleteButton());			
		}
	}
	
	@Transactional(propagation=Propagation.REQUIRED, readOnly=true)
	public List<Kid> getKids() {

		log.fine("KidsTable.getKids() : service = " + kidService);
		List<Kid> list = kidService.getKids();
		log.fine("Number of Kids: " + list.size());
		if(list.size() < 1) {
			KidsList.fillDB(kidService);
			list = kidService.getKids();
		}
		return list;
	}	
	
	
	@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
	public String newKid() {
//	    RequestContext requestContext = RequestContext.getCurrentInstance();
//	    requestContext.getPageFlowScope().put("selectedKid", new Kid());
		FacesContext context = FacesContext.getCurrentInstance();
        context.getExternalContext().getRequestMap().put("selectedKid", new Kid());
	    return "edit";
	}
	
	@Transactional(propagation=Propagation.REQUIRED, readOnly=false)
	public void deleteSelected(ActionEvent event) {
		Object oldRowKey = getTable().getRowKey(); // Remember the current row key of the table.
		
		Iterator<Object> selectedKeys = getTable().getSelectedRowKeys().iterator();
		int deleted = 0;
		while(selectedKeys.hasNext()) {
			Object key = selectedKeys.next();
			log.finer("Selected key = " + key);
			getTable().setRowKey(key);
			Kid kid = (Kid) getTable().getRowData();
			log.finer("Removing kid: " + kid.getId() + ": " + kid.getFirstName() + " " + kid.getLastName() + ".");
			kidService.deleteKid(kid);
			deleted++;
		}
		
		getTable().setSelectedRowKeys(null); // Reset the selection
		getTable().setRowKey(oldRowKey); // Reset the current row key to the remembered value.
		
		if(deleted > 0) {
			RequestContext rc = RequestContext.getCurrentInstance();
		    rc.addPartialTarget(getTable());
		}
	}
}
