package com.uas.report.controller;

import com.uas.report.schedule.model.TaskInformation;
import com.uas.report.schedule.service.TaskService;
import com.uas.report.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/schedule")
public class ScheduleController {

	@Autowired
	private FileService fileService;

	@Autowired
	private TaskService taskService;

	@RequestMapping("/deleteGeneratedFiles")
	@ResponseBody
	public String deleteGeneratedFiles(@RequestParam Integer taskPeriod, HttpServletRequest request) {
		String message = "已开启：" + fileService.newDeleteGeneratedFilesTask(taskPeriod);
		restart(request);
		return message;
	}

	@RequestMapping("/tasks")
	@ResponseBody
	public List<TaskInformation> allTaskInformations(HttpServletRequest request) {
		return taskService.allTaskInformations();
	}

	@RequestMapping("/start")
	@ResponseBody
	public String start(HttpServletRequest request) {
		return taskService.start();
	}

	@RequestMapping("/stop")
	@ResponseBody
	public String stop(HttpServletRequest request) {
		return taskService.stop();
	}

	@RequestMapping("/restart")
	@ResponseBody
	public String restart(HttpServletRequest request) {
		if (!taskService.isStopped()) {
			taskService.stop();
		}
		return taskService.start();
	}

	@RequestMapping("/isStopped")
	@ResponseBody
	public boolean isStopped(HttpServletRequest request) {
		return taskService.isStopped();
	}
}
