package info.bukova.isspst.ui.requirement;

import info.bukova.isspst.data.*;
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.FormViewModel;
import info.bukova.isspst.validators.TripRequirementFormValidator;
import org.zkoss.bind.annotation.*;
import org.zkoss.zk.ui.event.UploadEvent;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zul.Filedownload;

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

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;
	@WireVariable
	private DocumentFileStorage documentStorage;
	private List<Workgroup> centres;
	private List<User> users;
	private List<User> passengers;
	private List<FileMetainfo> attachments;
	private List<FileMetainfo> forDelete;
	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();
		attachments = getDataBean().getAttachedFiles();
		forDelete = new ArrayList<FileMetainfo>(); // kolekce příloh na smazání v případě uložení záznamu
		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;
	}

	@Command
	@NotifyChange("attachments")
	public void uploadAttachment(@ContextParam(ContextType.TRIGGER_EVENT) UploadEvent upEvent) {
		FileMetainfo metaInfo = documentStorage.saveAndCreateInfo(upEvent.getMedia().getByteData(),
				upEvent.getMedia().getName());

		getDataBean().addAttachment(metaInfo);
	}

	@Command
	@NotifyChange("attachments")
	public void deleteAttachment(@BindingParam("attachment") FileMetainfo metaInfo) {
		getDataBean().removeAttachment(metaInfo);
		forDelete.add(metaInfo); // smazat až v případě uložení záznamu
	}

	@Command
	public void downloadAttachment(@BindingParam("attachment") FileMetainfo metaInfo) {
		Filedownload.save(documentStorage.fileData(metaInfo), metaInfo.getContentType(), metaInfo.getFileName());
	}

	public List<FileMetainfo> getAttachments() {
		return attachments;
	}

	@Override
	protected void doSave() {
		for (FileMetainfo info : forDelete) {
			documentStorage.removeFile(info);
		}

		super.doSave();
	}
}
