package org.springblade.flow.engine.controller;


import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import liquibase.pro.packaged.O;
import liquibase.pro.packaged.S;
import org.springblade.flow.base.ActionResult;
import org.springblade.flow.engine.entity.FlowBusRelation;
import org.springblade.flow.engine.enums.OperateType;
import org.springblade.flow.engine.service.FlowBusRelationService;
import org.springblade.flow.engine.service.FlowDynamicService;
import org.springblade.flow.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Date;
import java.util.Map;

@Api(tags = "流程业务关系", value = "FlowBusRelation")
@RestController
@RequestMapping("/flow/bus")
public class FlowBusRelationController {

    @Autowired
    private FlowBusRelationService flowBusRelationService;

    @Autowired
    private FlowDynamicService flowDynamicService;


//
//    @ApiOperation("增加")
//    @PostMapping("/add")
//    public ActionResult<Object> add(@RequestBody @Valid FlowBusRelation flowBusRelation) {
//        try {
//            flowBusRelation.setCreateTime(new Date());
//            flowBusRelation.setCreator(SecureUtil.getNickName());
//            flowBusRelationService.save(flowBusRelation);
//            flowDynamicService.submit(null, flowBusRelation.getFlowId(), flowBusRelation.getId().toString(),
//                    null);
//            return ActionResult.success("提交成功，请耐心等待");
//        } catch (Exception e) {
//            e.printStackTrace();
//            return ActionResult.fail("提交失败");
//        }
//    }

    @ApiOperation("增加")
    @PostMapping("/save")
    public ActionResult<Object> save(String busId, String busJson, String dataJson, String flowId, Integer isTable, String creatorId, String creatorName, OperateType operateType) {
        try {

            FlowBusRelation flowBusRelation = new FlowBusRelation();
            flowBusRelation.setCreateTime(new Date());
            if (StringUtil.isNotEmpty(busId)){
                flowBusRelation.setBusId(busId);
            }
            if (StringUtil.isNotEmpty(busJson)){
                flowBusRelation.setBusJson(new String(Base64.getDecoder().decode(busJson), StandardCharsets.UTF_8));
            }
            String dataStr = new String(Base64.getDecoder().decode(dataJson), StandardCharsets.UTF_8);
            Map<String , Object> bean;
            try {
                bean  = JSONUtil.toBean(dataStr, Map.class);
            } catch (Exception e) {
                return ActionResult.fail("提交失败,dataJson不能解析成 map");
            }
            if (StringUtil.isNotEmpty(dataJson)){
                flowBusRelation.setDataJson(dataStr);
            }
            if (isTable != null){
                flowBusRelation.setIsTable(isTable);
            }
            if (operateType!=null){
                flowBusRelation.setOperateType(operateType);
            }
            flowBusRelation.setFlowId(flowId);
            flowBusRelation.setCreatorId(creatorId);
            flowBusRelation.setCreator(creatorName);
            flowBusRelationService.save(flowBusRelation);
            flowDynamicService.submit(null, flowBusRelation.getFlowId(), flowBusRelation.getId().toString(),
                    null,creatorId,creatorName, bean);
            return ActionResult.success("提交成功，请耐心等待");
        } catch (Exception e) {
            e.printStackTrace();
            return ActionResult.fail("提交失败");
        }
    }
//    @ApiOperation("根据processId获取busId")
//    @GetMapping("/getBusIdByProcessId/{processId}")
//    public R getBusIdByProcessId(@PathVariable("processId") String processId) {
//        try {
//            QueryWrapper<FlowBusRelation> queryWrapper = new QueryWrapper<>();
//            queryWrapper.lambda().eq(FlowBusRelation::getProcessId, processId);
//            FlowBusRelation one = flowBusRelationService.getOne(queryWrapper);
//            return R.data(one.getBusId());
//        } catch (Exception e) {
//            e.printStackTrace();
//            return R.fail("查询失败");
//        }
//    }
}
