package com.uas.search.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

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.ResponseBody;

import com.uas.search.schedule.TaskInformation;
import com.uas.search.schedule.TaskService;

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

	@Autowired
	private TaskService taskService;

	@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();
	}
}
