package com.blueland.ebpm.rest;

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

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 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.ActivityResourceModel;
import com.blueland.bpm.engine.core.service.ProcessConfigService;
import com.blueland.bpm.engine.core.service.ProcessDefinitionService;
import com.blueland.ebpm.bean.ActivityDefinitionBean;
import com.blueland.ebpm.bean.BeansBuilder;
import com.blueland.ebpm.bean.ExtAttributBean;
import com.blueland.ebpm.util.ComparatorActivity;

@RestController
@RequestMapping(value = "/activitydefinition")
public class ActivityDefineController {
	
	@Autowired
	private ProcessDefinitionService processDefinitionService;
	
	@Autowired
	private ProcessConfigService processConfigService;
	
	@Autowired
	private DataObjectRelationManager dataObjectRelationManager;
	
	@Autowired
	private BeansBuilder beansBuilder;
	
	@RequestMapping(value = "/{procDefId}/{actDefId}", produces = "application/json;charset=UTF-8")
	public ActivityDefinitionBean getActivity(@PathVariable String procDefId,
			@PathVariable String actDefId) {
		ActivityDefinitionModel model = processDefinitionService.getActivity(
				procDefId, actDefId);
		model.setActivityInfoConfig(processConfigService.getActivityInfoConfig(
				procDefId, actDefId));
		ActivityDefinitionBean bean = beansBuilder.build(model);
		bean.setPageUrl(processConfigService.getWorkflowPage(procDefId, actDefId));
		return bean;
	}
	
	@RequestMapping(value = "/{procDefId}/useract", produces = "application/json;charset=UTF-8")
	public List<ActivityDefinitionBean> getUserActivitys(@PathVariable String procDefId) {
		List<ActivityDefinitionModel> models = processDefinitionService.getProcessUserActivitys(procDefId);
		List<ActivityDefinitionBean> beans=new ArrayList<ActivityDefinitionBean>();
		for (ActivityDefinitionModel model : models) {
			model.setActivityInfoConfig(processConfigService.getActivityInfoConfig(
					procDefId, model.getActDefId()));
			ActivityDefinitionBean bean = beansBuilder.build(model);
			bean.setPageUrl(processConfigService.getWorkflowPage(procDefId, model.getActDefId()));
			beans.add(bean);
		}
		try{
		Collections.sort(beans, new ComparatorActivity());
		}catch(Exception e){
			e.printStackTrace();
		}
		return beans;
	}
	
	@RequestMapping(value = "/{procDefId}", produces = "application/json;charset=UTF-8")
	public List<ActivityDefinitionBean> getActivitys(@PathVariable String procDefId) {
		List<ActivityDefinitionModel> models = processDefinitionService.getProcessActivitys(procDefId);
		List<ActivityDefinitionBean> beans=new ArrayList<ActivityDefinitionBean>();
		for (ActivityDefinitionModel model : models) {
			model.setActivityInfoConfig(processConfigService.getActivityInfoConfig(
					procDefId, model.getActDefId()));
			ActivityDefinitionBean bean = beansBuilder.build(model);
			bean.setPageUrl(processConfigService.getWorkflowPage(procDefId, model.getActDefId()));
			beans.add(bean);
		}
		return beans;
	}
	
	//@RequestMapping(value = "/{procDefId}/{actDefId}/page", produces = "text/plain;charset=UTF-8")
	@RequestMapping(value = "/{procDefId}/{actDefId}/page", produces = "application/json;charset=UTF-8")
	public Map<String,String> getWorkflowPage(@PathVariable String procDefId, @PathVariable String actDefId) {
		String page = processConfigService.getWorkflowPage(procDefId, actDefId);
		Map<String,String> result = new HashMap<String,String>();
		result.put("page", page);
		return result;
	}
	
	@RequestMapping(value = "/{procDefId}/{actDefId}/extattrs", produces = "application/json; charset=UTF-8")
	public List<ExtAttributBean> getActExtattrs(@PathVariable String procDefId,
			@PathVariable String actDefId) {
		List<DataObjectRelation> list = dataObjectRelationManager
				.findDataObjectRelationsByActivityDefId(procDefId, actDefId);
		return beansBuilder.build(list);
	}
	
	@RequestMapping(value = "/{procDefId}/allactextattrs", produces = "application/json; charset=UTF-8")
	public List<ExtAttributBean> getAllActExtattrs(@PathVariable String procDefId) {
		List<DataObjectRelation> list = dataObjectRelationManager.findAllActDataObjectRelations(procDefId);
		return beansBuilder.build(list);
	}
	
	@ResponseBody
	@RequestMapping(value = "/{procDefId}/{actDefId}/resource", produces = "application/json; charset=UTF-8")
	public List<ActivityResourceModel> getActResouce(
			@PathVariable String procDefId,
			@PathVariable String actDefId) {
		return processDefinitionService.getResource(procDefId, actDefId);
	}	
	
	@RequestMapping(value = "/{procDefId}/{sourceActDefId}/{destActDefId}/extattrs", produces = "application/json; charset=UTF-8")
	public List<ExtAttributBean> getTransExtattrs(
			@PathVariable String procDefId,
			@PathVariable String sourceActDefId,
			@PathVariable String destActDefId) {
		List<DataObjectRelation> list = dataObjectRelationManager
				.findDataObjectRelationsBySequenceFlow(procDefId,
						sourceActDefId, destActDefId);
		return beansBuilder.build(list);
	}
	
	
}
