package com.cmvr.test.flow.context;

import com.alibaba.fastjson2.JSONObject;
import com.cmvr.test.enums.NodeTypeEnum;
import com.cmvr.test.enums.TaskStatusEnum;
import com.cmvr.test.enums.TerminalStatusEnum;
import com.cmvr.test.service.ITeNodeInstService;
import com.cmvr.test.service.ITeTaskInstService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 任务实例状态协调器：负责同步日志记录、上下文状态、数据库状态
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TaskInstHolder {

    private final ITeNodeInstService nodeInstService;
    private final TaskContextManager taskContextManager;
    private final ITeTaskInstService taskInstService;

    public void register(TaskContext context) {
        taskContextManager.register(context);
    }

    public void unRegister(String instId) {
        taskContextManager.unregister(instId);
    }

    public TaskContext getContext(String instId) {
        return taskContextManager.get(instId);
    }

    public boolean isTerminalLocked(String terminalId) {
        return taskContextManager.isTerminalLocked(terminalId);
    }

    public void markRunning(String instId, String taskId, String itemId,
                            String nodeId, String nodeType, String operate,
                            String action, String paramsIn, List<Integer> iterations) {
        nodeInstService.logRunning(instId, taskId, itemId, nodeId, nodeType, operate, action, paramsIn, iterations);
    }

    public void markSuccess(String instId, String itemId, String nodeId, int pendingItemCount, String nodeType,
                            String paramsOut, String message, List<Integer> iterations) {
        nodeInstService.logSuccess(instId, itemId, nodeId, paramsOut, message,iterations);
        syncStatus(instId, TaskStatusEnum.SUCCESS);
        // 如果没有下一个检测项要执行 且是end节点 注销任务上下文
        if (pendingItemCount == 0 && nodeType.equalsIgnoreCase(NodeTypeEnum.END.getCode())) {
            taskContextManager.unregister(instId);
        }
    }

    public void markFailed(String instId, String taskId, String itemId,
                           String nodeId, String nodeType, String operate,
                           String action, String params, String message, List<Integer> iterations) {
        nodeInstService.logFailed(instId, taskId, itemId, nodeId, nodeType, operate, action, params, message,iterations);

        syncStatus(instId, TaskStatusEnum.FAILED);
        taskContextManager.unregister(instId);
    }

    public void syncStatus(String instId, TaskStatusEnum status) {
        TaskContext ctx = taskContextManager.get(instId);
        switch (status) {
            case PAUSED:
                ctx.setPaused(true);
                ctx.setStatus(TaskStatusEnum.PAUSED);
                ctx.setTerminalStatus(TerminalStatusEnum.PAUSED);
                break;
            case STOPPED:
                ctx.setStopped(true);
                ctx.setPaused(false);
                ctx.setTerminalLocked(false);
                break;
        }
        ctx.setStatus(status);
        ctx.updateTimestamp();

        taskInstService.updateStatus(instId, status);
    }

    /**
     * 存储当前节点的输出参数
     */
    public void setNodeOutParams(String instId, String nodeId, List<Integer> iterations, JSONObject params) {
        taskContextManager.setNodeOutParams(instId, nodeId, iterations, params);
    }

    /**
     * 根据 nodeId 和 iterations 获取节点的输出参数
     */
    public JSONObject getNodeOutParams(String instId, String nodeId, List<Integer> iterations) {
        return taskContextManager.getNodeOutParams(instId, nodeId, iterations);
    }
}
