package com.lanyuanxiaoyao.service.web.controller;

import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.configuration.entity.info.JobIdAndAlias;
import com.lanyuanxiaoyao.service.configuration.exception.TableNotFoundException;
import com.lanyuanxiaoyao.service.forest.service.InfoService;
import com.lanyuanxiaoyao.service.forest.service.ScheduleService;
import com.lanyuanxiaoyao.service.web.controller.base.AmisResponse;
import com.lanyuanxiaoyao.service.web.controller.base.BaseController;
import java.util.regex.Pattern;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

/**
 * 调度
 *
 * @author lanyuanxiaoyao
 * @date 2023-07-06
 */
@RestController
@RequestMapping("schedule")
public class ScheduleController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ScheduleController.class);

    private final ScheduleService scheduleService;
    private final InfoService infoService;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    public ScheduleController(ScheduleService scheduleService, InfoService infoService) {
        this.scheduleService = scheduleService;
        this.infoService = infoService;
    }

    @PostMapping("table_batch")
    public AmisResponse<Object> tableBatch(@RequestParam("lines") String lines) throws TableNotFoundException {
        Pattern p = Pattern.compile("^(\\d+)\\s*(\\S+)$");
        ImmutableList<JobIdAndAlias> jobIdAndAliases = Lists.immutable.of(lines.trim().split("\n"))
                .collect(line -> {
                    Long flinkJobId = Long.valueOf(ReUtil.get(p, line, 1));
                    String alias = ReUtil.get(p, line, 2);
                    return new JobIdAndAlias(flinkJobId, alias);
                });
        for (JobIdAndAlias jobIdAndAlias : jobIdAndAliases) {
            checkTableExists(infoService, jobIdAndAlias.getId(), jobIdAndAlias.getAlias());
        }
        jobIdAndAliases.forEach(item -> scheduleService.scheduleTable(item.getId(), item.getAlias()));
        return AmisResponse.responseSuccess();
    }

    @GetMapping("table")
    public AmisResponse<Object> table(
            @RequestParam("flink_job_id") Long flinkJobId,
            @RequestParam("alias") String alias,
            @RequestParam(value = "force", required = false) String forceCluster,
            @RequestParam(value = "recommend", required = false) String recommendCluster
    ) throws TableNotFoundException {
        checkTableExists(infoService, flinkJobId, alias);
        if (StrUtil.isNotBlank(forceCluster)) {
            scheduleService.scheduleTableForce(flinkJobId, alias, forceCluster);
        } else if (StrUtil.isNotBlank(recommendCluster)) {
            scheduleService.scheduleTableRecommend(flinkJobId, alias, recommendCluster);
        } else {
            scheduleService.scheduleTable(flinkJobId, alias);
        }
        return AmisResponse.responseSuccess();
    }

    @GetMapping("stop_all")
    public AmisResponse<Object> stopAll(
            @RequestParam("flink_job_id") Long flinkJobId,
            @RequestParam("alias") String alias,
            @RequestParam(value = "disable_meta", required = false) Boolean disableMeta
    ) throws TableNotFoundException {
        checkTableExists(infoService, flinkJobId, alias);
        scheduleService.stopAll(flinkJobId, alias, disableMeta);
        return AmisResponse.responseSuccess();
    }
}
