package info.bukova.isspst.ui.requirement;

import java.util.List;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.select.annotation.WireVariable;

import info.bukova.isspst.data.SettingsData;
import info.bukova.isspst.data.TripRequirement;
import info.bukova.isspst.data.User;
import info.bukova.isspst.data.Workgroup;
import info.bukova.isspst.services.requirement.RequirementTypeService;
import info.bukova.isspst.services.requirement.TripRequirementService;
import info.bukova.isspst.services.settings.GlobalSettingsService;
import info.bukova.isspst.services.users.UserService;
import info.bukova.isspst.services.workgroups.WorkgroupService;
import info.bukova.isspst.ui.FormViewModel;
import info.bukova.isspst.validators.TripRequirementFormValidator;

public class TripRequirementForm extends FormViewModel<TripRequirement> {
	
	@WireVariable
	private UserService userService;
	@WireVariable
	private WorkgroupService workgroupService;
	@WireVariable
	private GlobalSettingsService settingsService;
	@WireVariable
	private RequirementTypeService reqTypeService; 
	@WireVariable
	private TripRequirementService tripRequirementService;
	private List<Workgroup> centres;
	private List<User> users;
	private List<User> passengers;
	private User selUser;
	private TripRequirementFormValidator validator;
	
	@Init(superclass = true)
	public void init() {
		tripRequirementService.loadType(getDataBean());
		centres = reqTypeService.filterCentres(getDataBean().getType(), workgroupService.getUserCentres(userService.getCurrent()));
		users = userService.getUsersForCombo();
		passengers = getDataBean().getPassengers();
		validator = new TripRequirementFormValidator();
	}
	
	@Command
	@NotifyChange("passengers")
	public void addPassanger() {
		getDataBean().getPassengers().add(selUser);
	}
	
	@Command
	@NotifyChange("passengers")
	public void removePassanger(@BindingParam("user") User user) {
		getDataBean().getPassengers().remove(user);
	}
	
	public List<Workgroup> getCentres() {
		return centres;
	}

	public List<User> getUsers() {
		return users;
	}

	public User getSelUser() {
		return selUser;
	}

	public void setSelUser(User selUser) {
		this.selUser = selUser;
	}
	
	public SettingsData getSettings() {
		return settingsService.getSettings();
	}

	public TripRequirementFormValidator getValidator() {
		return validator;
	}

	public List<User> getPassengers() {
		return passengers;
	}

}
