package com.blueland.ebpm.rest;

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

import com.blueland.ebpm.util.RestUtils;
import org.activiti.engine.impl.persistence.entity.HistoricProcessInstanceEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.blueland.bpm.engine.config.model.ActivityInfoConfig;
import com.blueland.bpm.engine.core.model.ActivityDefinitionModel;
import com.blueland.bpm.engine.core.model.ActivityInstanceModel;
import com.blueland.bpm.engine.core.model.ActivityResourceModel;
import com.blueland.bpm.engine.core.model.ExceptionErrorCode;
import com.blueland.bpm.engine.core.model.ProcessPageContext;
import com.blueland.bpm.engine.core.service.ProcessConfigService;
import com.blueland.bpm.engine.core.service.ProcessDefinitionService;
import com.blueland.bpm.engine.core.service.ProcessInstanceService;
import com.blueland.bpm.engine.util.WorkFlowContants;
import com.blueland.ebpm.bean.ActivityDefinitionBean;
import com.blueland.ebpm.bean.ActivityInstanceBean;
import com.blueland.ebpm.bean.ActivityOpBean;
import com.blueland.ebpm.bean.BeansBuilder;
import com.blueland.ebpm.exception.RestException;

@RestController
@RequestMapping(value = "/activityinstance")
public class ActivityInstanceController {
	@Autowired
	private ProcessConfigService processConfigService;
	
	@Autowired
	private ProcessInstanceService processInstanceService;
	@Autowired
	private ProcessDefinitionService processDefinitionService;
	@Autowired
	private BeansBuilder beansBuilder;

	@RequestMapping(value = "/{actInstId}")
	public ActivityInstanceBean getActInst(@PathVariable String actInstId) {
		ActivityInstanceModel model = processInstanceService.getHistoryTask(actInstId);
		return beansBuilder.build(model);
	}

	@RequestMapping(value = "/next/nextacts", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public List<ActivityDefinitionBean> getNextActivities(
			@RequestBody Map<String, String> param) {
		String curActInstId = param.get("curActInstId");
		String processInstId = null;
		String processDefinitionId = null;
		String curActDefId = null;
		if (RestUtils.isEmpty(curActInstId)) {
			processDefinitionId = param.get("procDefId");
			curActDefId = param.get("curActDefId");
		} else {
			ActivityInstanceModel model = processInstanceService
					.getTask(curActInstId);
			processDefinitionId = model.getProcDefId();
			processInstId = model.getProcInstId();
			curActDefId = model.getActDefId();
		}
		List<ActivityDefinitionModel> list = processDefinitionService
				.getNextActivity(processInstId, processDefinitionId,
						curActDefId, param);
		return beansBuilder.buildActDefList(list);
	}

	@RequestMapping(value = "/next/nextactusers", produces = "application/json;charset=UTF-8")
	public List<ActivityResourceModel> getNextActivityUserTree(
			@RequestBody Map<String, String> param) {
		String curActInstId = param.get("curActInstId");
		String destActDefId = param.get("destActDefId");
		String userId = param.get("userId");
		String userOrgId = param.get("userOrgId");
		String procInstId = null;
		String procDefId = null;
		String curActDefId = null;
		if (RestUtils.isEmpty(curActInstId)) {
			procDefId = param.get("procDefId");
			curActDefId = param.get("curActDefId");
		} else {
			ActivityInstanceModel model = processInstanceService
					.getTask(curActInstId);
			procInstId = model.getProcInstId();
			procDefId = model.getProcDefId();
			curActDefId = model.getActDefId();
		}

		return processDefinitionService.getActivityUserTree(procInstId,
				procDefId, curActInstId, curActDefId, destActDefId, userId,
				userOrgId, null, param);
	}

	@RequestMapping(value = "/oppower", produces = "application/json; charset=UTF-8")
	public List<ActivityOpBean> getOpPower(	@RequestBody Map<String, String> param) {
		String scope = param.get("scope");
		if (RestUtils.isEmpty(scope))
			throw new IllegalArgumentException("scope is null");

		if (scope.equals("1")) { // 新建
			String procDefId = param.get("procDefId");
			if (RestUtils.isEmpty(procDefId))
				throw new IllegalArgumentException("procDefId is null");
			String actDefId = param.get("actDefId");
			if (RestUtils.isEmpty(actDefId))
				throw new IllegalArgumentException("actDefId is null");
			Map<String, Object> pageDetailVaribale = new HashMap<String, Object>();
			ActivityInfoConfig actInfo = processConfigService
					.getActivityInfoConfig(procDefId, actDefId, scope,
							pageDetailVaribale);
			return beansBuilder.buildActOpList(actInfo
					.getActivityOperatePowers());
		} else {
			String actInstId = param.get("actInstId");
			if (RestUtils.isEmpty(actInstId))
				throw new IllegalArgumentException("actInstId is null");
			String userId = param.get("userId");
			if (RestUtils.isEmpty(userId))
				throw new IllegalArgumentException("userId is null");
			String userOrgId = param.get("userOrgId");
			if (RestUtils.isEmpty(userOrgId))
				throw new IllegalArgumentException("userOrgId is null");

			ActivityInstanceModel actInst = processInstanceService.getHistoryTask(actInstId);
			if (actInst == null)
				throw new RestException(ExceptionErrorCode.B2052.name(), "activity instance not found, id=" + actInstId);
			String procDefId = actInst.getProcDefId();
			String actDefId = actInst.getActDefId();
			Map<String, Object> pageDetailVaribale = new HashMap<String, Object>();
			pageDetailVaribale.put("task", actInst);
			HistoricProcessInstanceEntity processInstance = (HistoricProcessInstanceEntity) processInstanceService
					.getHistoryService().createHistoricProcessInstanceQuery()
					.processInstanceId(actInst.getProcInstId()).singleResult();
			if (processInstance == null)
				throw new RestException(ExceptionErrorCode.B2051.name(), "process instance not found, id=" + actInst.getProcInstId());
			pageDetailVaribale.put("processInstance", processInstance);
			ProcessPageContext processPageContext = new ProcessPageContext(
					scope, WorkFlowContants.ACTION_TYPE_EXECUTE_ACTIVITY, "",
					userId, userOrgId);
			pageDetailVaribale.put("processPageContext", processPageContext);
			ActivityInfoConfig actInfo = processConfigService
					.getActivityInfoConfig(procDefId, actDefId, scope,
							pageDetailVaribale);
			return beansBuilder.buildActOpList(actInfo.getActivityOperatePowers());
		}
	}
	
	@RequestMapping(value = "/{actInstId}/backactivities", produces = "application/json; charset=UTF-8")
	public List<ActivityInstanceBean> findBackActivity(@PathVariable String actInstId){
		List<ActivityInstanceModel> result = processInstanceService.findBackAvtivity(actInstId);
		return beansBuilder.buildActInstList(result);
	}
	
	

	/*
	@RequestMapping(value = "/{actInstId}/comment", produces = "application/json; charset=UTF-8")
	public List<ProcessLogModel> getActInstComment(@PathVariable String actInstId){
		return processInstanceService.getActInstComments(actInstId);
	}*/
}
