package info.bukova.isspst.ui.requirement;

import info.bukova.isspst.StringUtils;
import info.bukova.isspst.data.RequirementState;
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.storage.DocumentFileStorage;
import info.bukova.isspst.ui.FormWithUpload;
import info.bukova.isspst.validators.TripRequirementFormValidator;
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.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

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

public class TripRequirementForm extends FormWithUpload<TripRequirement> {
	
	@WireVariable
	private UserService userService;
	@WireVariable
	private WorkgroupService workgroupService;
	@WireVariable
	private GlobalSettingsService settingsService;
	@WireVariable
	private RequirementTypeService reqTypeService; 
	@WireVariable
	private TripRequirementService tripRequirementService;
	@WireVariable
	private DocumentFileStorage documentStorage;
	private List<Workgroup> centres;

	@Override
	@NotifyChange("errMessages")
	@Command
	public void save(@BindingParam("window") Window win) {
		final Window editWin = win;

		if (getDataBean().getState() == RequirementState.APPROVED) {
			Messagebox.show(StringUtils.localize("TripRequirementChangeWarn"), StringUtils.localize("TripRequirementSave"), Messagebox.YES
					| Messagebox.NO, Messagebox.QUESTION, new EventListener<Event>() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (((Integer) event.getData()).intValue() == Messagebox.YES) {
						TripRequirementForm.super.save(editWin);
					} else {
						editWin.detach();
					}
				}
			});
		} else {
			super.save(win);
		}
	}

	private List<User> users;
	private List<User> passengers;
	private User selUser;
	private TripRequirementFormValidator validator;
	
	@Init(superclass = true)
	public void init() {
		tripRequirementService.loadType(getDataBean());
		if (isEditRec()) {
			List<Workgroup> current = new ArrayList<Workgroup>();
			current.add(getDataBean().getCentre());
			this.centres = current;
		} else {
			this.centres = reqTypeService.filterCentres(getDataBean().getType(), workgroupService.getUserCentres(userService.getCurrent(), true));
		}
		
		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;
	}

}
