package cd.casic.ci.process.engine.worker.shuimu;

import cd.casic.ci.process.common.WorkAtom;
import cd.casic.ci.process.dto.resp.shuimu.ShuiMuProjectResourceResp;
import cd.casic.ci.process.engine.constant.ShuiMuConstant;
import cd.casic.ci.process.engine.runContext.TaskRunContext;
import cd.casic.ci.process.engine.worker.base.BaseWorker;
import cd.casic.ci.process.engine.worker.base.SshWorker;
import cd.casic.ci.process.process.dataObject.task.PipTask;
import cd.casic.ci.process.process.service.shuimu.ShuiMuService;
import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 *
 * */
@Slf4j
@WorkAtom(taskType = "SHUI_MU")
public class ShuiMuWorker extends BaseWorker {
    @Resource
    private ShuiMuService shuiMuService;
    @Override
    public void execute(TaskRunContext context) {
        PipTask contextDef = (PipTask) context.getContextDef();
        Map<String, Object> taskProperties = contextDef.getTaskProperties();
        String projectId = (String)taskProperties.get(ShuiMuConstant.SHUI_MU_PROJECT_ID_KEY);
        Integer fuzzMinutes = Integer.parseInt(String.valueOf(taskProperties.get(ShuiMuConstant.SHUI_MU_FUZZ_MINUTES)));
        shuiMuService.executeAllEntrypointsTest(projectId,fuzzMinutes);
        append(context,"水木模糊测试开始执行");
        while (true){
            ShuiMuProjectResourceResp projectResource = shuiMuService.getProjectResource(projectId);
            append(context,"当前执行情况：");
            append(context, JSON.toJSONString(projectResource));
            if (projectResource.getTestResource()+projectResource.getQueueResource()==0) {
                append(context,"水木模糊测试执行完毕");
                break;
            }
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
