package info.bukova.isspst.ui.settings;

import info.bukova.isspst.Constants;
import info.bukova.isspst.Module;
import info.bukova.isspst.ModuleUtils;
import info.bukova.isspst.data.NumberSeries;
import info.bukova.isspst.services.numberseries.NumberSeriesService;
import info.bukova.isspst.ui.DocumentViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

public class NumberSeriesVM extends DocumentViewModel
{
	
	@WireVariable
	private NumberSeriesService numericSeriesService;
	private List<NumberSeries> numberSeriesList;
	private Map<String, Module> moduleMap;
	
	@Init(superclass = true)
	public void init() {
		numberSeriesList = new ArrayList<NumberSeries>(numericSeriesService.getAll());
		moduleMap = new HashMap<String, Module>();
		for (Module m : ModuleUtils.getActiveModules()) {
			moduleMap.put(m.getId(), m);
		}
	}

	public List<NumberSeries> getNumberSeriesList() {
		return numberSeriesList;
	}
	
	public boolean isCanSave() {
		return true;
	}
	
	@Command
	public void save(@BindingParam("window") Window window) {
		for (NumberSeries ns : numberSeriesList) {
			numericSeriesService.update(ns);
		}
		
		window.detach();
	}

	public Map<String, Module> getModuleMap() {
		return moduleMap;
	}
}
