package com.blueland.ebpm.rest;

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

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.RestController;

import com.blueland.bpm.engine.config.model.ProcessInfoConfig;
import com.blueland.bpm.engine.config.model.SysGlType;
import com.blueland.bpm.engine.config.service.SysGlTypeManager;
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.ebpm.bean.BeansBuilder;
import com.blueland.ebpm.bean.ProcessDefinitionBean;
import com.blueland.ebpm.bean.SysGlTypeBean;

@RestController
@RequestMapping("/flowtype")
public class FlowTypeController {
	
	@Autowired
	private SysGlTypeManager sysGlTypeManager;
	
	@Autowired
	private ProcessConfigService processConfigService;
	
	@Autowired
	private ProcessDefinitionService processDefinitionService;
	
	@Autowired
	private BeansBuilder beansBuilder;

	@RequestMapping(value="/{tenantId}/list", produces = "application/json;charset=UTF-8")
	public List<SysGlTypeBean> getTypesList(@PathVariable String tenantId){
		
		SysGlType param = new SysGlType();
		param.setAppKey(tenantId);
		List<SysGlType> result = sysGlTypeManager.findByExample(param);
		return beansBuilder.buildTypeList(result);
	}
	
//	private void getTree(String typeId, List<SysGlType> result){
//		SysGlType sysGlType = sysGlTypeManager.getById(typeId);
//		if(sysGlType == null)
//			return;
//		result.add(sysGlType);
//		
//		SysGlType param = new SysGlType();
//		param.setTypeParentId(sysGlType.getTypeId());
//		List<SysGlType> list = sysGlTypeManager.findByExample(param);
//		if(list == null || list.size() == 0)
//			return;
//		for(SysGlType type : list)
//			getTree(type.getTypeId(), result);		
//	}
	
	@RequestMapping(value="/{typeId}", produces = "application/json;charset=UTF-8")
	public SysGlTypeBean getType(@PathVariable String typeId) {
		SysGlType type = sysGlTypeManager.getById(typeId);
		SysGlTypeBean bean = beansBuilder.build(type);
		return bean;
	}
	
	@RequestMapping(value="/{typeId}/children", produces = "application/json;charset=UTF-8")
	public List<SysGlTypeBean> getChildren(@PathVariable String typeId){
		SysGlType param = new SysGlType();
		param.setTypeParentId(typeId);
		List<SysGlType> list = sysGlTypeManager.findByExample(param);
		return beansBuilder.buildTypeList(list);
	}
	
	@RequestMapping(value="/{typeId}/flowlist", produces = "application/json;charset=UTF-8")
	public List<ProcessDefinitionBean> getFlowList(@PathVariable String typeId){
		List<ProcessDefinitionBean> result = new ArrayList<ProcessDefinitionBean>();
		ProcessInfoConfig params = new ProcessInfoConfig();
		params.setProcessTypeId(typeId);
		List<ProcessInfoConfig> list = processConfigService.findProcessInfoConfigs(params);
		for(ProcessInfoConfig config : list) {
			ProcessDefinitionModel model = processDefinitionService.getProcessDef(config.getProcessDefId());
			model.setProcessInfoConfig(config);
			result.add(beansBuilder.build(model));
		}
		return result;
	}
}
