package info.bukova.isspst.ui.requirement;

import info.bukova.isspst.data.TripRequirement;
import info.bukova.isspst.data.Workgroup;
import info.bukova.isspst.filters.TripRequirementFilter;
import info.bukova.isspst.services.requirement.TripRequirementService;
import info.bukova.isspst.services.workgroups.WorkgroupService;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.access.AccessDeniedException;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.select.annotation.WireVariable;

public class TripRequirementListWorkgroup extends RequirementSubpage<TripRequirement> {
	
	@WireVariable
	private TripRequirementService tripRequirementService;

	@WireVariable
	private WorkgroupService workgroupService;

	private List<Workgroup> myCentres;

	private List<Workgroup> myWorkgroups;

	@Init
	public void init() {
		service = tripRequirementService;
		dataClass = TripRequirement.class;
		formZul = "requirementsForm.zul";
		dataFilter = new TripRequirementFilter(getFilterTemplate());
		
		myCentres = workgroupService.getUserCentres(userService.getCurrent());
		myWorkgroups = workgroupService.getUserWorkgroups(userService.getCurrent());
	}

	@Override
	protected List<TripRequirement> getListFromService() {
		try {
			return tripRequirementService.getWorkgroupReq();
		} catch (AccessDeniedException e) {
			BindUtils.postGlobalCommand(null, null, "disableWorkgroup", null);
			return new ArrayList<TripRequirement>();
		}
	}

	public List<Workgroup> getMyCentres() {
		return myCentres;
	}

	public List<Workgroup> getMyWorkgroups() {
		return myWorkgroups;
	}

	@GlobalCommand
	@NotifyChange({ "dataList", "dataBean", "fullFill" })
	public void reloadRelated() {
		this.reload();
	}
	
	@Override
	protected void beforeSelectViaUrl() {
		BindUtils.postGlobalCommand(null, null, "selectWorkgroup", null);
	}

}
