package com.uas.report.controller;

import java.util.List;

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 com.uas.report.schedule.model.TaskInformation;
import com.uas.report.schedule.service.TaskService;
import com.uas.report.service.FileService;

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

	@Autowired
	private FileService fileService;

	@Autowired
	private TaskService taskService;

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

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

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

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

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

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