package com.cmvr.web.controller.test;

import com.alibaba.fastjson2.JSONObject;
import com.cmvr.common.core.controller.BaseController;
import com.cmvr.common.core.domain.AjaxResult;
import com.cmvr.test.flow.control.FlowControlService;
import com.cmvr.test.flow.runtime.engine.FlowTaskRuntimeService;
import com.cmvr.test.flow.runtime.operator.edge.ti.TiTouchOperateService;
import com.cmvr.test.model.vo.FlowiseActionRequestVO;
import com.cmvr.test.model.vo.FlowiseChatRequestVO;
import com.cmvr.test.model.vo.FlowiseStartRequestVO;
import com.cmvr.test.model.vo.TeTaskExecuteNormalVO;
import com.cmvr.test.service.FlowActionExecutorService;
import com.cmvr.test.service.FlowiseActionService;
import com.google.gson.JsonObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "测试--flowise服务")
@RestController
@RequestMapping("/flowise")
@RequiredArgsConstructor
public class TeFlowiseController extends BaseController {

    private final FlowiseActionService flowiseActionService;
    private final FlowTaskRuntimeService flowTaskRuntimeService;
    private final FlowControlService flowControlService;
    private final FlowActionExecutorService flowActionExecutorService;
    private final TiTouchOperateService tiTouchOperateService;
    private final List<String> taskInstIdList = new ArrayList<>();

    @ApiOperation("调用flowise")
    @PostMapping("/start")
    public AjaxResult start(@RequestBody FlowiseStartRequestVO request) {
        JSONObject runParams = new JSONObject();

        JSONObject runParams1 = new JSONObject();
        runParams1
                .put("test", request.getQuestion());
        runParams.put("5bb26d52e1861c6936f3c3149eaf17e8", runParams1);
        TeTaskExecuteNormalVO taskExecuteNormalVO = TeTaskExecuteNormalVO.builder()
                .taskId("b6ab5ffff783d9b25d0dc34f13be222d")
                .terminalId("4ed1246c465b97975f96c9ef8371a3bd")
                .runParams(JSONObject.from(runParams)).build();
        if (!taskInstIdList.isEmpty()) {
            try {
                taskInstIdList.forEach(flowControlService::stop);
            } catch (Exception e) {
                // 忽略
            }
            taskInstIdList.clear();
        }
        String insId = flowTaskRuntimeService.executeTask(taskExecuteNormalVO);
        taskInstIdList.add(insId);
        return AjaxResult.success();
//        return AjaxResult.ok(flowiseActionService.start(request));
    }

    @ApiOperation("根据chatId查询执行结果")
    @GetMapping("/{chatId}")
    public AjaxResult query(@PathVariable String chatId) {
        return AjaxResult.ok(flowiseActionService.query(chatId));
    }

    @ApiOperation("终止flowise")
    @GetMapping("/abort")
    public AjaxResult abort() {
        if (!taskInstIdList.isEmpty()) {
            try {
                taskInstIdList.forEach(flowControlService::stop);
            } catch (Exception e) {
                // 忽略
            }
            taskInstIdList.clear();
        }
        logger.info("取消工作流");
        return AjaxResult.success();
//        return AjaxResult.ok(flowiseActionService.abort());
    }

    @ApiOperation("执行指令")
    @PostMapping("/command")
    public AjaxResult command(@RequestBody FlowiseActionRequestVO request) {
        return AjaxResult.ok(flowiseActionService.command(request));
    }

    @ApiOperation("闲聊")
    @PostMapping("/chat")
    public AjaxResult chat(@RequestBody FlowiseChatRequestVO request) {
        return AjaxResult.ok(flowiseActionService.chat(request));
    }

    @ApiOperation("动作（眨眼+动嘴+头部微动）")
    @GetMapping("/action/start")
    public AjaxResult actionStart() {
        return AjaxResult.ok(flowiseActionService.actionStart());
    }

    @ApiOperation("动作停止")
    @GetMapping("/action/stop")
    public AjaxResult actionStop() {
        return AjaxResult.ok(flowiseActionService.actionStop());
    }
}