package info.bukova.isspst.ui.dashboard;

import info.bukova.isspst.data.Role;
import info.bukova.isspst.data.User;
import info.bukova.isspst.data.Workgroup;
import info.bukova.isspst.services.invoicing.InvoicingService;
import info.bukova.isspst.services.limits.LimitService;
import info.bukova.isspst.services.users.UserService;
import info.bukova.isspst.services.workgroups.WorkgroupService;
import info.bukova.isspst.ui.DocumentViewModel;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zk.ui.select.annotation.WireVariable;

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

public class DashBoardVM extends DocumentViewModel {
	
	@WireVariable
	private WorkgroupService workgroupService;
	@WireVariable
	private UserService userService;
	@WireVariable
	private InvoicingService invoicingService;
	@WireVariable
	private LimitService limitService;
	private User user;
	private Map<Workgroup, List<Role>> groupRoles;
	private Map<Workgroup, BigDecimal> workgroupSpent;
	private Map<Workgroup, BigDecimal> workgroupLimit;
	
	@Init(superclass = true)
	public void init() {
		user = userService.getCurrent();
		
		groupRoles = new HashMap<Workgroup, List<Role>>();
		workgroupSpent = new HashMap<Workgroup, BigDecimal>();
		workgroupLimit = new HashMap<Workgroup, BigDecimal>();
		
		List<Workgroup> wg = new ArrayList<Workgroup>();
		if (workgroupService.getUserCentres(user) != null) {
			wg.addAll(workgroupService.getUserCentres(user));
		}
		if (workgroupService.getUserWorkgroups(user) != null) {
			wg.addAll(workgroupService.getUserWorkgroups(user));
		}
		
		for (Workgroup w : wg) {
			List<Role> r = workgroupService.getUserWorkgroupRoles(w, user);
			groupRoles.put(w, r);
			workgroupSpent.put(w, invoicingService.totalInvoicedForWorkgroup(w));
			if (limitService.getForWorkgroup(w) != null) {
				workgroupLimit.put(w, limitService.getForWorkgroup(w).getLimit());
			}
		}
	}
	
	public User getUser() {
		return user;
	}
	
	public List<Workgroup> getCentres() {
		return workgroupService.getUserCentres(user);
	}
	
	public List<Workgroup> getWorkgroups() {
		return workgroupService.getUserWorkgroups(user);
	}
	
	public Map<Workgroup, List<Role>> getGroupRoles() {
		return groupRoles;
	}

	public Map<Workgroup, BigDecimal> getWorkgroupSpent() {
		return workgroupSpent;
	}

	public Map<Workgroup, BigDecimal> getWorkgroupLimit() {
		return workgroupLimit;
	}

}
