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.users.UserService;
import info.bukova.isspst.services.workgroups.WorkgroupService;
import info.bukova.isspst.ui.ListViewModel;
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;

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

public class TripRequirementList extends ListViewModel<TripRequirement> {
	
	@WireVariable
	private TripRequirementService tripRequirementService;
	@WireVariable
	private WorkgroupService workgroupService;
	@WireVariable
	private UserService userService;
	private List<Workgroup> myCentres;
	private boolean showCentre;
	private boolean showWorkgroup;
	private boolean showAll;

	@Init(superclass = true)
	public void init() {
		service = tripRequirementService;
		dataClass = TripRequirement.class;
		formZul = "requirementsForm.zul";
		
		showCentre = true;
		showWorkgroup = true;
		showAll = true;
		
		dataFilter = new TripRequirementFilter(getFilterTemplate());
		myCentres = workgroupService.getUserCentres(userService.getCurrent());
	}
	
	@GlobalCommand
	@NotifyChange("showCentre")
	public void disableCentre() {
		showCentre = false;
	}
	
	@GlobalCommand
	@NotifyChange("showWorkgroup")
	public void disableWorkgroup() {
		showWorkgroup = false;
	}
	
	@GlobalCommand
	@NotifyChange("showAll")
	public void disableAll() {
		showAll = false;
	}
	
	public boolean isShowCentre() {
		return showCentre;
	}

	public boolean isShowWorkgroup() {
		return showWorkgroup;
	}

	public boolean isShowAll() {
		return showAll;
	}

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

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

}
