package io.wdd.rpc.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.wdd.common.response.R;
import io.wdd.rpc.scheduler.beans.ScriptSchedulerVO;
import io.wdd.rpc.scheduler.service.QuartzSchedulerService;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "定时任务控制中心的Controller", tags = "Scheduler")
@RequestMapping(value = "/octopus/server/scheduler")
public class SchedulerController {

    @Autowired
    QuartzSchedulerService octopusQuartzService;


    /**
     * --------------------------------------------------------------
     * 脚本--页面定时脚本任务
     * 应该只有脚本功能才可以定时，目前一阶段的功能
     */

    @ApiOperation(value = "[脚本]-新增一个定时脚本任务")
    @PostMapping(value = "/script/create")
    public R<HashMap<String, String>> createScriptScheduler(
            @ApiParam(name = "scheduleScript") @RequestBody() ScriptSchedulerVO scriptSchedulerVO
    ) {

        HashMap<String, String> map = octopusQuartzService.createScriptScheduledMission(scriptSchedulerVO);

        return R.ok(map);

    }

    @ApiOperation(value = "[脚本]-查询所有的定时脚本任务")
    @GetMapping(value = "/script/getAll")
    public R<List<JobDetail>> getAllScriptScheduler() {

        return R.ok(octopusQuartzService.queryAllScriptScheduleMission());
    }

    @ApiOperation(value = "[脚本]-删除定时脚本任务")
    @PostMapping(value = "/script/delete")
    public R<Boolean> deleteScriptScheduler(
            @RequestParam(value = "scriptJobUUID") @ApiParam(value = "定时脚本任务UUID", name = "scriptJobUUID") String scriptJobUUID
    ) {

        return R.ok(octopusQuartzService.deleteScriptScheduleMission(scriptJobUUID));
    }

    @ApiOperation(value = "[脚本]-修改定时脚本任务")
    @PostMapping(value = "/script/modify")
    public R<JobDetail> modifyScriptScheduler(
            @ApiParam(name = "scheduleScript") @RequestBody() ScriptSchedulerVO scriptSchedulerVO
    ) {

        return R.ok(octopusQuartzService.modifyScriptScheduleMission(scriptSchedulerVO));
    }

    /**
     * --------------------------------------------------------------
     * 普通的定时任务查询功能
     */

    @ApiOperation(value = "查询所有mission")
    @GetMapping(value = "/queryAllMission")
    public R<List<Map<String, Object>>> queryAllQuartzMission() {


        return R.ok(octopusQuartzService.queryAllMission());
    }


    @ApiOperation(value = "查询所有运行mission")
    @PostMapping(value = "/queryRunMission")
    public R<List<Map<String, Object>>> queryRunQuartzMission() {

        return R.ok(octopusQuartzService.queryRunMission());
    }


    @ApiOperation(value = "删除一个mission")
    @PostMapping(value = "/deleteMission/")
    public R<String> deleteMission(
            @ApiParam(name = "missionName") @RequestParam("missionName") String missionName
    ) {

        boolean deleteMission = octopusQuartzService.deleteMission(
                missionName,
                missionName
        );
        String result = String.format(
                "删除任务[ %s ]结果为 [ %s ]",
                missionName,
                deleteMission
        );

        if (deleteMission) {
            return R.ok(result);
        } else {
            return R.failed(result);
        }
    }


    @ApiOperation(value = "修改mission的cron时间")
    @PostMapping(value = "/updateMission/{missionName}")
    public void deleteMission(
            @ApiParam(name = "missionName") @RequestParam("missionName") String missionName,
            @ApiParam(name = "missionCronTime") @RequestParam("missionCronTime") String missionCronTime
    ) {

        octopusQuartzService.updateMission(
                missionName,
                missionName,
                missionCronTime
        );

    }


    @ApiOperation(value = "查询所有的触发器Trigger")
    @GetMapping(value = "/allTriggers")
    public R<List<Trigger>> queryAllTriggers() {

        return R.ok(
                octopusQuartzService.queryAllTrigger()
        );

    }
}


