package com.cmvr.web.controller.test;

import com.cmvr.common.annotation.Anonymous;
import com.cmvr.common.core.controller.BaseController;
import com.cmvr.common.core.domain.AjaxResult;
import com.cmvr.test.flow.context.TaskContextManager;
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.domain.TeDetectionItem;
import com.cmvr.test.model.vo.FlowActionRequestVO;
import com.cmvr.test.model.vo.TeFlowViewVO;
import com.cmvr.test.model.vo.TeTaskExecuteNormalVO;
import com.cmvr.test.model.vo.TeTaskExecuteTrailVO;
import com.cmvr.test.service.FlowActionExecutorService;
import com.cmvr.test.service.ITeDetectionItemService;
import com.cmvr.test.service.ITeNodeInstService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Api(tags = "测试--流程服务")
@RestController
@RequestMapping("/flow")
@RequiredArgsConstructor
public class TeFlowController extends BaseController {

    private final ITeDetectionItemService teDetectionItemService;
    private final ITeNodeInstService nodeInstService;
    private final FlowTaskRuntimeService flowTaskRuntimeService;
    private final TaskContextManager taskContextManager;
    private final FlowControlService flowControlService;
    private final FlowActionExecutorService flowActionExecutorService;
    private final TiTouchOperateService tiTouchOperateService;
    private final List<String> taskInstIdList = new ArrayList<>();
    @ApiOperation("流程发布")
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody TeDetectionItem detectionItem) {
        return toAjax(teDetectionItemService.publish(detectionItem.getId()));
    }


    @ApiOperation("演示任务执行入口")
    @PostMapping("/demo")
    @Anonymous
    public AjaxResult demo(@Valid @RequestBody TeTaskExecuteNormalVO taskExecuteNormalVO) {
        if (!taskInstIdList.isEmpty()) {
            try {
                taskInstIdList.forEach(flowControlService::stop);
            } catch (Exception e) {
                // 忽略
            }
            taskInstIdList.clear();
        }
        String insId = flowTaskRuntimeService.executeTask(taskExecuteNormalVO);
        taskInstIdList.add(insId);
        return AjaxResult.success();
    }

    @ApiOperation("任务执行入口")
    @PostMapping("/execute")
    @Anonymous
    public AjaxResult execute(@Valid @RequestBody TeTaskExecuteNormalVO taskExecuteNormalVO) {
        return AjaxResult.success(flowTaskRuntimeService.executeTask(taskExecuteNormalVO));
    }

    @ApiOperation("检测项试运行入口")
    @PostMapping("/executeTrial")
    public AjaxResult executeTrial(@Valid @RequestBody TeTaskExecuteTrailVO taskExecuteTrailVO) {
        return AjaxResult.success(flowTaskRuntimeService.executeTrialTask(taskExecuteTrailVO));
    }

    @ApiOperation("流程可视化")
    @PostMapping("/view")
    public AjaxResult view(@Valid @RequestBody TeFlowViewVO flowViewVO) {
        return AjaxResult.success(nodeInstService.view(flowViewVO));
    }

    @ApiOperation("清理终端")
    @GetMapping("/clear")
    public AjaxResult clear() {
        taskContextManager.clearAll();
        return AjaxResult.success();
    }

    @ApiOperation("终止任务")
    @PostMapping("/stop/{instId}")
    public AjaxResult stopTask(@PathVariable String instId) {
        flowControlService.stop(instId);
        return AjaxResult.success();
    }

    @ApiOperation("暂停任务")
    @PostMapping("/pause/{instId}")
    public AjaxResult pauseTask(@PathVariable String instId) {
        flowControlService.pause(instId);
        return AjaxResult.success();
    }

    @ApiOperation("恢复任务")
    @PostMapping("/resume/{instId}")
    public AjaxResult resumeTask(@PathVariable String instId) {
        flowControlService.resume(instId);
        return AjaxResult.success();
    }

    @PostMapping("/action")
    @ApiOperation("单个节点执行")
    public AjaxResult actionExecute(@RequestBody FlowActionRequestVO flowActionRequestVO) {
        return AjaxResult.ok(flowActionExecutorService.actionExecute(flowActionRequestVO));
    }

    @GetMapping("/testrun")
    @ApiOperation("测试接口")
    public AjaxResult testrun(@RequestParam("imageUrl") String imageUrl,
                              @RequestParam("iconUrl") String iconUrl) {
        return AjaxResult.ok(tiTouchOperateService.test(imageUrl,iconUrl));
    }
}