package com.jeelowcode.service.bpm.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jeelowcode.core.framework.controller.BaseController;
import com.jeelowcode.core.framework.entity.FormEntity;
import com.jeelowcode.core.framework.service.IFormService;
import com.jeelowcode.framework.exception.JeeLowCodeException;
import com.jeelowcode.framework.utils.model.ResultDataModel;
import com.jeelowcode.framework.utils.tool.CollectionUtil;
import com.jeelowcode.framework.utils.tool.NumberUtil;
import com.jeelowcode.tool.framework.common.pojo.CommonResult;
import com.jeelowcode.tool.framework.common.pojo.PageResult;
import com.jeelowcode.service.bpm.controller.vo.process.BpmProcessDefinitionListReqVO;
import com.jeelowcode.service.bpm.controller.vo.process.BpmProcessDefinitionPageItemRespVO;
import com.jeelowcode.service.bpm.controller.vo.process.BpmProcessDefinitionPageReqVO;
import com.jeelowcode.service.bpm.controller.vo.process.BpmProcessDefinitionRespVO;
import com.jeelowcode.service.bpm.service.IBpmProcessDefinitionService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.security.acl.LastOwnerException;
import java.util.*;

import static com.jeelowcode.tool.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 流程定义")
@RestController
@RequestMapping("/bpm/process-definition")
public class BpmProcessDefinitionController extends BaseController {

    @Resource
    private IBpmProcessDefinitionService bpmDefinitionService;

    @Resource
    private IFormService dbFormService;

    @GetMapping("/page")
    @Operation(tags = "流程管理",summary = "获得流程定义分页")
    @PreAuthorize("@ss.hasPermission('bpm:process-definition:query')")
    public CommonResult<PageResult<BpmProcessDefinitionPageItemRespVO>> getProcessDefinitionPage(
            BpmProcessDefinitionPageReqVO pageReqVO) {
        return success(bpmDefinitionService.getProcessDefinitionPage(pageReqVO));
    }

    @GetMapping ("/list")
    @Operation(tags = "流程管理",summary = "获得流程定义列表")
    @PreAuthorize("@ss.hasPermission('bpm:process-definition:query')")
    public CommonResult<List<BpmProcessDefinitionRespVO>> getProcessDefinitionList(
            BpmProcessDefinitionListReqVO listReqVO) {
        return success(bpmDefinitionService.getProcessDefinitionList(listReqVO));
    }

    @GetMapping ("/formlist")
    @ApiOperationSupport(order = 4)
    @Operation(tags = "流程管理",summary = "获得表单流程定义列表")
    public CommonResult<List<BpmProcessDefinitionRespVO>> getProcessDefinitionList(String formId) {
        return success(bpmDefinitionService.getProcessDefinitionFormList(NumberUtil.toLong(formId)));
    }

    @GetMapping ("/dbformlist")
    @ApiOperationSupport(order = 4)
    @Operation(tags = "流程管理",summary = "获得表单流程定义列表")
    public CommonResult<List<BpmProcessDefinitionRespVO>> getProcessDefinitionListUseDbFormId(String dbFormId) {
        FormEntity formEntity = dbFormService.getFormEntityById(NumberUtil.toLong(dbFormId));
        if(Objects.isNull(formEntity)){
            return success(new ArrayList<>());
        }
        List<BpmProcessDefinitionRespVO> list = bpmDefinitionService.getProcessDefinitionFormList(formEntity.getDesformWebId());
        return success(list);
    }

    @GetMapping ("/get-bpmn-xml")
    @Operation(tags = "流程管理",summary = "获得流程定义的 BPMN XML")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('bpm:process-definition:query')")
    public CommonResult<String> getProcessDefinitionBpmnXML(@RequestParam("id") String id) {
        String bpmnXML = bpmDefinitionService.getProcessDefinitionBpmnXML(id);
        return success(bpmnXML);
    }
}
