package com.blueland.bpm.engine.core.power;

import java.util.List;
import java.util.Map;

import org.activiti.bpmn.constants.BpmnXMLConstants;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.impl.RepositoryServiceImpl;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.pvm.ReadOnlyProcessDefinition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.activiti.engine.task.Task;

import com.blueland.bpm.common.util.ApplicationContextHolder;
import com.blueland.bpm.engine.core.model.ActivityInstanceModel;
import com.blueland.bpm.engine.core.model.ProcessPageContext;
import com.blueland.bpm.engine.core.service.ProcessInstanceService;
import com.blueland.bpm.engine.util.ProcessDefinitionUtils;
/**
 * 撤回操作权限处理类
 * @version:  1.0
 * @author lw 
 */
public class CallBackButActivityOperatePower extends AbstractActivityOperatePower {
	ProcessInstanceService processInstanceService = (ProcessInstanceService) ApplicationContextHolder
			.getBean("processInstanceServiceImpl");
	RepositoryServiceImpl repositoryService = (RepositoryServiceImpl) ApplicationContextHolder
			.getBean("repositoryService");

	@Override
	public boolean isActivityOperatePower(Map<String, Object> pageDetailVaribale) {
		boolean isPower = false;
		ProcessPageContext context = (ProcessPageContext) pageDetailVaribale
				.get("processPageContext");
		ActivityInstanceModel prevActivityInstance = (ActivityInstanceModel) pageDetailVaribale
				.get("task");
		HistoricProcessInstance processInstance = (HistoricProcessInstance) pageDetailVaribale
				.get("processInstance");

		if (context == null || prevActivityInstance == null
				|| processInstance == null) {
			return isPower;
		}
		List<Task> tasks=processInstanceService.getProcessTasks(prevActivityInstance.getProcInstId(), prevActivityInstance.getReceiver(), prevActivityInstance.getActInstId());
		if(tasks!=null&&tasks.size()!=1){
			return isPower;
		}
		ActivityInstanceModel curActivityInstance=ActivityInstanceModel.buildAndActDef((TaskEntity)tasks.get(0),null);
		boolean isClaimTask = processInstanceService
				.isClaimTask(curActivityInstance.getActInstId());
		if (isClaimTask
				|| !curActivityInstance.getSender().equals(context.getUserId())) {
			return isPower;
		}
		RepositoryServiceImpl repositoryServiceImpl = (org.activiti.engine.impl.RepositoryServiceImpl) repositoryService;
		ReadOnlyProcessDefinition rpd = repositoryServiceImpl
				.getDeployedProcessDefinition(curActivityInstance.getProcDefId());
		ActivityImpl activity = (ActivityImpl) rpd
				.findActivity(curActivityInstance.getActDefId());
		String destActivityType = (String) activity
				.getProperty(BpmnXMLConstants.ATTRIBUTE_TYPE);
		ActivityImpl prevActivity = (ActivityImpl) rpd
				.findActivity(curActivityInstance.getPrevActDefId());
		String prevActivityType = (String) prevActivity
				.getProperty(BpmnXMLConstants.ATTRIBUTE_TYPE);

		boolean isDestActivityPower = !(ProcessDefinitionUtils
				.isSubProcessStartEvent(activity)
				|| ProcessDefinitionUtils.isSubProcessEndEvent(activity)
				|| ProcessDefinitionUtils.isCallSubProcessEndEvent(
						processInstance, activity)
				|| ProcessDefinitionUtils.isMultiInstance(activity)
				|| ProcessDefinitionUtils.isEndEvent(activity)
				|| BpmnXMLConstants.ELEMENT_GATEWAY_PARALLEL
						.equals(destActivityType) || BpmnXMLConstants.ELEMENT_GATEWAY_INCLUSIVE
				.equals(destActivityType));
		boolean isPrevActivityPower = !(ProcessDefinitionUtils
				.isSubProcessStartEvent(prevActivity)
				|| ProcessDefinitionUtils.isSubProcessEndEvent(prevActivity)
				|| ProcessDefinitionUtils.isCallSubProcessEndEvent(
						processInstance, prevActivity)
				|| ProcessDefinitionUtils.isMultiInstance(prevActivity)
				|| ProcessDefinitionUtils.isEndEvent(prevActivity)
				|| BpmnXMLConstants.ELEMENT_GATEWAY_PARALLEL
						.equals(prevActivityType)
				|| BpmnXMLConstants.ELEMENT_GATEWAY_INCLUSIVE
						.equals(prevActivityType)
				|| BpmnXMLConstants.ELEMENT_TASK_SERVICE
						.equals(prevActivityType) || BpmnXMLConstants.ELEMENT_TASK_SCRIPT
				.equals(prevActivityType));
		if (prevActivityInstance != null
				&& !(prevActivityInstance.getExecutionId()
						.equals(curActivityInstance.getExecutionId()))) {
			isPrevActivityPower = false;
		}

		return (isDestActivityPower && isPrevActivityPower);
	}

}
