package com.xjrsoft.module.workflow.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.db.Session;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.form.dto.FormExecuteWorkflowUpdateDto;
import com.xjrsoft.module.workflow.constant.WorkflowConstant;
import com.xjrsoft.module.workflow.dto.RelationTaskPageDto;
import com.xjrsoft.module.workflow.model.FormConfig;
import com.xjrsoft.module.workflow.service.IWorkflowExecuteService;
import com.xjrsoft.module.workflow.utils.WorkFlowUtil;
import com.xjrsoft.module.workflow.vo.AllRecordListVo;
import com.xjrsoft.module.workflow.vo.ProcessInfoVo;
import com.xjrsoft.module.workflow.vo.RelationTaskPageVo;
import com.xjrsoft.module.workflow.vo.UserTaskInfoVo;
import org.apache.commons.lang3.tuple.Triple;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.history.HistoricVariableInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.variable.VariableMap;
import org.camunda.bpm.engine.variable.Variables;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

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

@SpringBootTest
class WorkflowExecuteServiceImplTest {

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private IWorkflowExecuteService workflowExecuteService;

    @Test
    public void testDeleteTask(){
        //taskService.deleteTask("8abd7148-e58b-11ee-a00e-0242c8000007");
        runtimeService.deleteProcessInstance("45a1a081-f8a0-11ee-92e5-0242d200000c", "该流程已经停止");
    }

    @Test
    public void testGetRelationTaskPage(){
        //taskService.deleteTask("8abd7148-e58b-11ee-a00e-0242c8000007");
        PageOutput<RelationTaskPageVo> pageOutput = workflowExecuteService.getRelationTaskPage(new RelationTaskPageDto(){{
            setSchemaId(1717071192907317250L);
        }});
        System.err.println(pageOutput.getList());
    }

    @Test
    public void testAll(){
//        List<HistoricProcessInstance> historicProcessInstances = historyService.createHistoricProcessInstanceQuery().processDefinitionId("process_hf6d97xl:37:f21a1fac-bb50-11ee-88a7-0242c8000006").list();
//        List<HistoricVariableInstance> list = historyService.createHistoricVariableInstanceQuery().processInstanceId("1a428758-bb1c-11ee-88a7-0242c8000006").list();
        HistoricVariableInstance formDataList = historyService.createHistoricVariableInstanceQuery()
                .processInstanceId("1a428758-bb1c-11ee-88a7-0242c8000006")
                .variableName("form_1717020683051728897_1702299402475")
                .singleResult();
        //List<HistoricVariableInstance> thisTaskFormData = formDataList.stream().filter(x -> x.getProcessInstanceId().equals("1a428758-bb1c-11ee-88a7-0242c8000006")).collect(Collectors.toList());
        //拼凑成表单 Map<String, Map<String, Object>> 类型
       // Map<String, Map<String, Object>> allFormData = new HashMap<>();
        VariableMap variableMap = Variables.createVariables();

//        for (HistoricVariableInstance variableInstance : thisTaskFormData) {
//            allFormData.put(variableInstance.getName(), Convert.toMap(String.class, Object.class, variableInstance.getValue()));
//        }
        Map<String, Object> allFormData =  Convert.toMap(String.class, Object.class, formDataList.getValue());
        System.err.println(formDataList.getValue());
    }

    @Test
    public void testGetProcessInfo(){
        ProcessInfoVo processInfo = workflowExecuteService.getProcessInfo("775eb10a-bb56-11ee-88a7-0242c8000006");
        System.err.println(processInfo);
    }
}