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.service.activiti.IActivitiModelService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.util.RegexUtil;
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;

;

/**
 * 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}")
    public SuccessResult saveModel(@PathVariable String modelId, String name, String description, String json_xml, String svg_xml) throws Exception {
        name = name.trim().replace("\\s", "");
        if (StringUtils.isBlank(name)) {
            throw new ParamsException("名称不能为空");
        }
        if (RegexUtil.isChinese(name)) {
            throw new ParamsException("名称不能有中文");
        }
        activitiModelService.saveModel(modelId, name, description, json_xml, svg_xml);
        return new SuccessResult();
    }

    @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();
    }

}
