package com.blueland.ebpm.rest;

import com.blueland.bpm.common.util.SysConfig;
import com.blueland.bpm.engine.config.model.DataObjectRelation;
import com.blueland.bpm.engine.config.service.DataObjectRelationManager;
import com.blueland.bpm.engine.core.model.ActivityDefinitionModel;
import com.blueland.bpm.engine.core.model.ProcessDefinitionModel;
import com.blueland.bpm.engine.core.service.ProcessConfigService;
import com.blueland.bpm.engine.core.service.ProcessDefinitionService;
import com.blueland.bpm.identity.UserService;
import com.blueland.bpm.identity.model.User;
import com.blueland.ebpm.bean.ActivityDefinitionBean;
import com.blueland.ebpm.bean.BeansBuilder;
import com.blueland.ebpm.bean.ExtAttributBean;
import com.blueland.ebpm.bean.ProcessDefinitionBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 流程定义类服务
 * 
 * @author janus
 *
 */
@RestController
@RequestMapping(value = "/processdefinition")
public class ProcessDefineController {
	
	@Autowired
	private ProcessDefinitionService processDefinitionService;
	
	@Autowired
	private ProcessConfigService processConfigService;
	
	@Autowired
	private DataObjectRelationManager dataObjectRelationManager;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private BeansBuilder beansBuilder;

	@RequestMapping(value = "/{procDefId}/id", produces = "application/json;charset=UTF-8")
	public ProcessDefinitionBean getProcDef(@PathVariable String procDefId) {
		ProcessDefinitionModel model = processDefinitionService.getProcessDef(procDefId);
		model.setProcessInfoConfig(processConfigService.getProcessInfoConfig(procDefId));
		return beansBuilder.build(model);
	}
	
	@RequestMapping(value="/{procDefKey}/key", produces="application/json; charset=UTF-8")
	public ProcessDefinitionBean getProcDefByKey(@PathVariable String procDefKey) {
		ProcessDefinitionModel model = processDefinitionService.getProcessDefBykey(procDefKey);
		model.setProcessInfoConfig(processConfigService.getProcessInfoConfig(model.getProcDefId()));
		return beansBuilder.build(model);
	}

	@RequestMapping(value = "/{procDefId}/startactdef", produces = "application/json;charset=UTF-8")
	public ActivityDefinitionBean getStartActivity(
			@PathVariable String procDefId) {
		ActivityDefinitionModel model = processDefinitionService.getStartActivity(procDefId);
		model.setActivityInfoConfig(processConfigService.getActivityInfoConfig(procDefId, model.getActDefId()));
		return beansBuilder.build(model);
	}
	
	@RequestMapping(value = "/{procDefId}/extattrs", produces = "application/json; charset=UTF-8")
	public List<ExtAttributBean> getProcExtattrs(@PathVariable String procDefId) {
		List<DataObjectRelation> list = dataObjectRelationManager.findDataObjectRelationsByProcessDefId(procDefId);
		return beansBuilder.build(list);
	}

	@RequestMapping(value="/{tentantId}/{userId}/{userOrgId}/newlist", produces="application/json; charset=UTF-8")
	public List<ProcessDefinitionBean> getNewList(@PathVariable String tentantId, @PathVariable String userId, @PathVariable String userOrgId){
		User user = userService.getUserById(userId);
		Map<String, List<ProcessDefinitionModel>> maps = processDefinitionService.findProcessDefStartByUser(tentantId, user.getUserId());
		return beansBuilder.build(maps);
	}
	
	@ResponseBody
	@RequestMapping(value="/{tentantId}/{userId}/{userOrgId}/{flowType}/newlist", produces="application/json; charset=UTF-8")
	public List<ProcessDefinitionBean> getNewList(@PathVariable String tentantId, @PathVariable String userId, @PathVariable String userOrgId, @PathVariable String flowType){
		User user = userService.getUserById(userId);
		Map<String, List<ProcessDefinitionModel>> maps = processDefinitionService.findProcessDefStartByUser(tentantId, user.getUserId(),flowType);
		return beansBuilder.build(maps);
	}
	
	@RequestMapping(value="/{procDefId}/imgurl", produces="application/json; charset=UTF-8")
	public Map<String,String> getProcDefImgUrl(@PathVariable String procDefId) {
		String url = SysConfig.getValue("MANAGE_URL") + "/procDefMgr/resource.do?resourceType=image&procDefId=" + procDefId;
		Map<String,String> result = new HashMap<String,String>();
		result.put("url", url);
		return result;
	}

}
