package com.blueland.ebpm.engine.core.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.blueland.bpm.engine.core.service.ProcessDefinitionService;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.interceptor.CommandContextFactory;
import org.activiti.engine.impl.persistence.deploy.DeploymentManager;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.util.io.InputStreamSource;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

import com.blueland.ebpm.engine.BaseWfServiceTest;
import com.blueland.bpm.engine.core.model.ActivityDefinitionModel;
import com.blueland.bpm.engine.core.model.ActivityResourceModel;
import com.blueland.bpm.engine.core.model.ProcessDefinitionModel;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class ProcessDefinitionServiceTest extends BaseWfServiceTest {
	@Autowired
	protected ProcessDefinitionService processDefinitionService;

	@Test
	public void getProcessDefBykey() {
		ProcessDefinitionModel processDefinition = processDefinitionService
				.getProcessDefBykey("Process_09UZD436");
		System.out.println(processDefinition);
	}
	
	
	//@Test
	public void getStartActivity() {
		ProcessDefinitionModel processDefinition = processDefinitionService
				.getProcessDefBykey("PROCESS_1");
		ActivityDefinitionModel pvmActivity=processDefinitionService.getStartActivity(processDefinition.getProcDefId());
		System.out.println(processDefinition);
	}
	@Test
	public void getNextActivity() {
		ProcessDefinitionModel processDefinition = processDefinitionService
				.getProcessDef("test2:1:f59e5263-862e-11e6-a6eb-005056b607a7");
		Map conditionMap=new HashMap();
		conditionMap.put("ACCESSORY_TYPE", "21201");
		List pvmActivity=processDefinitionService.getNextActivity(null,processDefinition.getProcDefId(),"sid-136B5934-839E-4E3A-ADB8-B9D6A7FA7C80",conditionMap);
		System.out.println(pvmActivity);
	}
	@Test
	public void getActivityUserTree() {
		ProcessDefinitionModel processDefinition = processDefinitionService
				.getProcessDef("my-leave-manual:3:21104");
		List<ActivityResourceModel> treeNode=processDefinitionService.getActivityUserTree("",processDefinition.getProcDefId(),null,"zgLeader_sp","sg_deptLeader_hq","","",null,null);
		System.out.println(treeNode);
	}

	@Test
	public void deploySingleProcess() throws Exception{
		processDefinitionService.deployFromClasspath("","cb_workdoc_file");
		
	}
	//@Test
	public void updateDeploymentResource(){
			processDefinitionService.updateDeploymentResource("17201","process1:1:17204","<a>1</a><b>2</b>".getBytes(),false);
	}
	@Test
	public void immediateUpdateBpmnFile() throws IOException{
		ProcessDefinitionModel processDefinition=processDefinitionService.getProcessDefBykey("cbCompanySend");
		DeploymentManager deploymentManager=this.processEngineConfiguration
        .getDeploymentManager();
		
		Context.setProcessEngineConfiguration(processEngineConfiguration);
		CommandContextFactory factory=new CommandContextFactory();
		factory.setProcessEngineConfiguration(processEngineConfiguration);
		Context.setCommandContext(factory.createCommandContext(null));
		ProcessDefinitionEntity pdfE=deploymentManager.findDeployedProcessDefinitionById(processDefinition.getProcDefId());
		
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("deployments/cbCompanySend.bpmn");
		byte[] updateBpmnByte=IOUtils.toByteArray(in);

		ProcessDefinitionEntity pdfEntity=this.processEngineConfiguration
	        .getDeploymentManager()
	        .getProcessDefinitionCache().get(processDefinition.getProcDefId());
		processDefinitionService.updateDeploymentResource(processDefinition.getDeploymentId(),processDefinition.getProcDefId(),updateBpmnByte,false);
		this.processEngineConfiguration
		        .getDeploymentManager()
		        .getProcessDefinitionCache().remove(processDefinition.getProcDefId());
	}
	@Test
		public void testBpmnModel(){
	
		InputStream in = processDefinitionService.loadProcessResource("my-leave-manual:7:25809", "xml");
		BpmnXMLConverter converter = new BpmnXMLConverter();

		boolean enableSafeBpmnXml = false;
		if (Context.getProcessEngineConfiguration() != null) {
			enableSafeBpmnXml = Context.getProcessEngineConfiguration()
					.isEnableSafeBpmnXml();
		}
		BpmnModel bpmnModel = converter.convertToBpmnModel(
				new InputStreamSource(in), true, enableSafeBpmnXml);
	

		}
	@Override
	protected void beforeTest() throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	protected void afterTest() throws Exception {
		// TODO Auto-generated method stub

	}

}
