package com.dingtalk.h5app.quickstart.controller;


import com.dingtalk.h5app.quickstart.domain.ServiceResult;
import com.dingtalk.h5app.quickstart.entity.Grade;
import com.dingtalk.h5app.quickstart.mapper.CronMapper;
import com.dingtalk.h5app.quickstart.service.CiKuService;
import com.dingtalk.h5app.quickstart.service.QuartzService;
import com.dingtalk.h5app.quickstart.service.TokenService;
import com.dingtalk.h5app.quickstart.vo.CiKuVO;
import org.junit.Test;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.*;


@RestController
@CrossOrigin
public class CiKuController {

    @Resource
    private CiKuService ciKuService;

    @Resource
    private QuartzService quartzService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private CronMapper cronMapper;

    @Autowired
    private Scheduler scheduler;

    @GetMapping("/findByRandom")
    public List<CiKuVO> findByRandom() {
        return ciKuService.findByRandom();
    }

    @PostMapping("/submitGrade")
    public int submitGrade(@RequestParam("score") String score,@RequestParam("userName") String userName){
        return ciKuService.insertGrade(userName,score);
    }

    @PostMapping("/getGrade")
    public List<Grade> getGrade(@RequestParam("name") String name){
        return ciKuService.getGrade(name);
    }

    @PostMapping("/setTimer")
    public ServiceResult serTimer(@RequestParam("pinlv") String pinlv, @RequestParam("time") String time, @RequestParam("userid") String userid){
//        System.out.println(pinlv);
//        System.out.println(userid);
        String accessToken;
//        System.out.println(file);
        // 获取accessToken
        ServiceResult<String> accessTokenSr = tokenService.getAccessToken();
        if (!accessTokenSr.isSuccess()) {
            return ServiceResult.failure(accessTokenSr.getCode(), accessTokenSr.getMessage());
        }
        List<Integer> a = new ArrayList<>();

        accessToken = accessTokenSr.getResult();
        String cron = "";
        if ("day".equals(pinlv)){
            if(time.charAt(0) == '0'&& time.charAt(3) == '0'){
                cron = "0 " + time.charAt(4) + " " + time.charAt(1) + " * * ?";
            }else if (time.charAt(0) == '0'&& time.charAt(3) != '0'){
                cron = "0 " + time.substring(3,5) + " " + time.charAt(1) + " * * ?";
            }else if(time.charAt(0) != '0'&& time.charAt(3) == '0'){
                cron = "0 " + time.charAt(4) + " " + time.substring(0,2) + " * * ?";
            }else if(time.charAt(0) != '0'&& time.charAt(3) != '0'){
                cron = "0 " + time.substring(3,5) + " " + time.substring(0,2) + " * * ?";
            }
        }else{
            if ("00".equals(time.substring(3,5))){
                if("0".equals(time.substring(0,1))){
                    cron = "0 0 "+ time.charAt(1) +" ? * "+pinlv;
                }else{
                    cron = "0 0 "+ time.substring(0,2) +" ? * "+pinlv;
                }
            }else{
                if("0".equals(time.substring(0,1))){
                    if ("0".equals(time.substring(3,4))){
                        cron = "0 "+time.charAt(4)+" "+ time.charAt(1) +" ? * " +pinlv;
                    }else {
                        cron = "0 "+time.substring(3,5)+" "+ time.charAt(1) +" ? * " +pinlv;
                    }
                }else {
                    if ("0".equals(time.substring(3,4))){
                        cron = "0 "+time.charAt(4)+" "+ time.substring(0,2) +" ? * " +pinlv;
                    }else {
                        cron = "0 "+time.substring(3,5)+" "+ time.substring(0,2) +" ? * " +pinlv;
                    }
                }
            }
        }
        try {

            if("success".equals(quartzService.insertQuartz(userid, cron, accessToken,pinlv+"|"+time))){
                return ServiceResult.success("成功");
            }else {
                return ServiceResult.failure("失败");
            }
        } catch (SchedulerException | SQLException e) {
            return ServiceResult.failure(e.getMessage());
        }
    }

    @PostMapping("/selectTimer")
    public ServiceResult selectTimer(@RequestParam("userid") String userid){
        return ServiceResult.success(cronMapper.selectCron(userid));
    }

    @PostMapping("/deleteTimer")
    public ServiceResult deleteTimer(@RequestParam("userid") String userid,@RequestParam("frequency") String frequency) throws SchedulerException {
        String jobID = cronMapper.getJobID(userid, frequency);
//        System.out.println(jobID);
        try {
            JobKey jobKey = new JobKey(jobID,userid);
            if (!scheduler.checkExists(jobKey)) {
                System.out.println("job not exists, job name: "+jobKey.getName()+", group name: "+ jobKey.getGroup());
                return ServiceResult.failure("200");
            }
            TriggerKey triggerKey = TriggerKey.triggerKey(jobID,userid);
            scheduler.pauseTrigger(triggerKey);
            scheduler.unscheduleJob(triggerKey);
            boolean b = scheduler.deleteJob(jobKey);
            System.out.println(b);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
//        System.out.println(b);
        return ServiceResult.success(cronMapper.deleteTimer(userid,frequency));
    }

}
