package ink.wgink.module.activiti.controller.api;

import com.alibaba.fastjson.JSONObject;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.module.activiti.pojo.dtos.ActivitiFlowNodeDTO;
import ink.wgink.module.activiti.pojo.vos.ActivitiModelVO;
import ink.wgink.module.activiti.service.activiti.IActivitiModelService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: ActivitiModelController
 * @Description: 流程模型
 * @Author: WangGeng
 * @Date: 2021/7/12 21:56
 * @Version: 1.0
 **/
@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "流程模型接口")
@RestController
@RequestMapping(ISystemConstant.API_PREFIX + "/activiti/model")
public class ActivitiModelController extends DefaultBaseController {

    @Autowired
    private IActivitiModelService activitiModelService;

    @ApiOperation(value = "保存模型", notes = "保存模型接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "模型ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PutMapping("save/{modelId}/version/{modelVersion}")
    public SuccessResult saveModel(@PathVariable String modelId, @PathVariable("modelVersion") Integer modelVersion, @RequestBody ActivitiModelVO activitiModelVO) throws Exception {
        if (StringUtils.isBlank(activitiModelVO.getName())) {
            throw new ParamsException("流程名称不能为空");
        }
        if(StringUtils.isBlank(activitiModelVO.getDescription())) {
            throw new ParamsException("流程描述不能为空");
        }
        activitiModelService.save(modelId, modelVersion, activitiModelVO);
        return new SuccessResult();
    }

    @ApiOperation(value = "删除模型", notes = "删除模型接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "模型ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @DeleteMapping("delete/{modelId}")
    public SuccessResult delete(@PathVariable("modelId") String modelId) {
        activitiModelService.delete(modelId);
        return new SuccessResult();
    }

    @ApiOperation(value = "通过部署ID获取节点列表", notes = "通过部署ID获取节点列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deploymentId", value = "部署ID", paramType = "path"),
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("list-flow-node/deployment-id/{deploymentId}")
    public List<ActivitiFlowNodeDTO> listFlowNodeByDefineId(@PathVariable("deploymentId") String deploymentId) {
        return activitiModelService.listFlowNodeByDeploymentId(deploymentId);
    }

    @ApiOperation(value = "模型json数据", notes = "模型json数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "modelId", value = "模型ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("json/{modelId}")
    public JSONObject getEditorJson(@PathVariable String modelId) throws UnsupportedEncodingException {
        return activitiModelService.getEditorJson(modelId);
    }

    @ApiOperation(value = "汉化配置json数据", notes = "汉化配置json数据接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("editor/stencilset")
    public JSONObject getStencilset() throws IOException {
        return activitiModelService.getStencilset();
    }

    @ApiOperation(value = "流程XML数据", notes = "流程XML数据接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processDefinitionId", value = "流程定义ID", paramType = "path")
    })
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @GetMapping("get-process-xml/{processDefinitionId}")
    public SuccessResultData<String> getProcessXml(@PathVariable("processDefinitionId") String processDefinitionId) {
        String processXml = activitiModelService.getProcessXml(processDefinitionId);
        return new SuccessResultData<>(processXml);
    }

}
