package com.leanwo.activiti.listener.base;

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

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.delegate.Expression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.reflect.TypeToken;
import com.leanwo.activiti.exception.WorkflowException;
import com.leanwo.activiti.service.base.impl.ProdogServer;
import com.leanwo.activiti.util.FormatUtil;
import com.leanwo.activiti.util.SpringUtil;
import com.leanwo.activiti.util.StringUtil;


/**
 * 多实例任务（指定成员）创建监听器
 * <ul>
 * 		<li>分配用户</li>
 * </ul>.
 *
 * @author YangZhiJie
 */
public class MultiInstanceUserTaskByGroupIdsExecutionListener implements ExecutionListener{
	
	private static Logger logger = LoggerFactory.getLogger(MultiInstanceUserTaskByGroupIdsExecutionListener.class);

	/**  任务接受人(String). */
	private Expression assignListVariableName;
	
	/**  任务接受用户组Id集合(List<String>). */
	private Expression groupIdList;

	/** 节点名称. */
	private Expression nodeName;
	
	@Override
	public void notify(DelegateExecution execution) {
		String assignListName = assignListVariableName.getExpressionText();
		String groupIdListText = groupIdList.getExpressionText();
		String node = nodeName.getExpressionText();
		
		if(StringUtil.isEmpty(assignListName) || StringUtil.isEmpty(groupIdListText)) {
			throw new WorkflowException(String.format("多实例任务(%s)未定义变量assignListVariableName和groupIdList。", node));
		}
		
		List<String> groupIdList = FormatUtil.getGson().fromJson(groupIdListText,  new TypeToken<List<String>>() {}.getType());
		if (groupIdList != null && groupIdList.size() > 0) {
			List<Long> groupIdLongList = new ArrayList<Long>(); 
			for(String groupId : groupIdList) {
				groupIdLongList.add(Long.parseLong(groupId));
			}
			ProdogServer prodogServer = SpringUtil.getBean(ProdogServer.class.getName());
			List<Long> userIds = prodogServer.listUserIdByGroupIds(groupIdLongList);
			if(userIds == null || userIds.size() == 0) {
				throw new WorkflowException(String.format("节点(%s)根据用户组id:" + groupIdListText + "，查询不到用户数据Id.", node));
			}
			List<String> userIdStrings = new ArrayList<String>();
			userIds.forEach(userId -> {
				userIdStrings.add(userId.toString());
			});
			execution.setVariable(assignListName, userIdStrings);
		}else {
			throw new WorkflowException(String.format("节点(%s)是用户组审批，但是未选择用户组。", node));
		}
	}

	/**
	 * Gets the 任务接受人(String).
	 *
	 * @return the 任务接受人(String)
	 */
	public Expression getAssignListVariableName() {
		return assignListVariableName;
	}


	/**
	 * Sets the 任务接受人(String).
	 *
	 * @param assignListVariableName the new 任务接受人(String)
	 */
	public void setAssignListVariableName(Expression assignListVariableName) {
		this.assignListVariableName = assignListVariableName;
	}


	/**
	 * Gets the 任务接受用户组Id集合(List<String>).
	 *
	 * @return the 任务接受用户组Id集合(List<String>)
	 */
	public Expression getGroupIdList() {
		return groupIdList;
	}


	/**
	 * Sets the 任务接受用户组Id集合(List<String>).
	 *
	 * @param groupIdList the new 任务接受用户组Id集合(List<String>)
	 */
	public void setGroupIdList(Expression groupIdList) {
		this.groupIdList = groupIdList;
	}
	/**
	 * Gets the 节点名称.
	 *
	 * @return the 节点名称
	 */
	public Expression getNodeName() {
		return nodeName;
	}

	/**
	 * Sets the 节点名称.
	 *
	 * @param nodeName the new 节点名称
	 */
	public void setNodeName(Expression nodeName) {
		this.nodeName = nodeName;
	}
}
