package info.bukova.isspst.ui.users;

import info.bukova.isspst.Module;
import info.bukova.isspst.ModuleUtils;
import info.bukova.isspst.StringUtils;
import info.bukova.isspst.data.Role;
import info.bukova.isspst.data.Workgroup;
import info.bukova.isspst.services.users.PermissionService;
import info.bukova.isspst.services.users.RoleService;
import info.bukova.isspst.ui.FormViewModel;

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

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

public class PermissionForm extends FormViewModel<Role> {
	
	@WireVariable
	private PermissionService permissionService;
	@WireVariable
	private RoleService roleService;
	private RolePermissions rolePerms;
	private boolean canChangeWorkgroup;
	private boolean canChangeCentre;
	private boolean inWorkflow;
	private List<String> usage;
	
	@Init(superclass = true)
	public void init() {
		rolePerms = new RolePermissions(getDataBean(), permissionService.getAllPermissions()); // Pro PermissionService nelze nastavit práva, musí se volat jiná metoda než getAll()
		canChangeWorkgroup = roleService.canChangeWorkgroup(getDataBean());
		canChangeCentre = roleService.canChangeCenter(getDataBean());
		inWorkflow = roleService.isInWorkflow(getDataBean());
		List<Workgroup> wgList = roleService.getRoleUsage(getDataBean());
		usage = new ArrayList<String>();		
		
		for (Workgroup w : wgList) {
			usage.add(w.toString());
		}
		
		if (inWorkflow) {
			usage.add(StringUtils.localize("Workflow"));
		}
	}
	
	public List<Module> getModules() {
		return ModuleUtils.getActiveModules();
	}
	
	public RolePermissions getRolePerms() {
		return rolePerms;
	}

	public boolean isCanChangeWorkgroup() {
		return canChangeWorkgroup;
	}

	public boolean isCanChangeCentre() {
		return canChangeCentre;
	}

	public boolean isInWorkflow() {
		return inWorkflow;
	}

	public List<String> getUsage() {
		return usage;
	}

}
