package com.lanyuanxiaoyao.service.web.controller;

import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.configuration.ExecutorProvider;
import com.lanyuanxiaoyao.service.forest.service.PulsarService;
import com.lanyuanxiaoyao.service.forest.service.TaskService;
import com.lanyuanxiaoyao.service.web.controller.base.AmisMapResponse;
import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 任务提交
 *
 * @author lanyuanxiaoyao
 * @date 2024-01-10
 */
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@RestController
@RequestMapping("task")
public class TaskController {
    private static final Logger logger = LoggerFactory.getLogger(TaskController.class);

    private final TaskService taskService;
    private final PulsarService pulsarService;

    public TaskController(TaskService taskService, PulsarService pulsarService) {
        this.taskService = taskService;
        this.pulsarService = pulsarService;
    }

    @GetMapping("scan")
    public AmisResponse<Object> scan(
            @RequestParam("key") String key,
            @RequestParam(value = "hdfs", required = false) String hdfs,
            @RequestParam(value = "pulsar", required = false) String pulsar,
            @RequestParam(value = "topic", required = false) String topic,
            @RequestParam(value = "mode", defaultValue = "") String mode
    ) {
        if (StrUtil.isBlank(key)) {
            throw new RuntimeException("Key cannot be blank");
        }
        boolean scanQueue = StrUtil.contains(mode, "queue");
        boolean scanLog = StrUtil.contains(mode, "log");
        boolean scanBase = StrUtil.contains(mode, "base");
        if (scanQueue) {
            if (StrUtil.isBlank(topic) || StrUtil.isBlank(pulsar)) {
                throw new RuntimeException("Pulsar topic or url cannot be empty");
            } else {
                if (!pulsarService.existsTopic(pulsar, topic)) {
                    throw new RuntimeException("Pulsar url is not contain the topic");
                }
            }
        }
        if ((scanLog || scanBase) && StrUtil.isBlank(hdfs)) {
            throw new RuntimeException("Hdfs path cannot be empty");
        }
        ExecutorProvider.EXECUTORS.submit(() -> {
            String applicationId = taskService.scan(key, hdfs, pulsar, topic, scanQueue, scanLog, scanBase);
            logger.info("Task: {}", applicationId);
        });
        return AmisResponse.responseSuccess();
    }

    @GetMapping("latest_op_ts")
    public AmisResponse<Object> latestOpTs(@RequestParam("hdfs") String hdfs) {
        if (StrUtil.isBlank(hdfs)) {
            throw new RuntimeException("Hdfs cannot be blank");
        }
        ExecutorProvider.EXECUTORS.submit(() -> {
            String applicationId = taskService.latestOpTs(hdfs);
            logger.info("Task: {}", applicationId);
        });
        return AmisResponse.responseSuccess();
    }

    @GetMapping("results")
    public AmisMapResponse results(@RequestParam("task_id") String taskId) {
        return AmisResponse.responseMapData()
                .setData("text", taskService.results(taskId).makeString("\n"));
    }
}
