package org.fool.glorious.controller;

import org.fool.glorious.DataProcessor;
import org.fool.glorious.db.dao.RecordService;
import org.fool.glorious.db.entity.Record;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/webshell")
public class DataResources {

    @Autowired
    private RecordService recordService;

    @Autowired
    private DataProcessor dataProcessor;

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public List<Record> getAllRecords(@RequestParam(value = "type", required = false) String type,
                                      @RequestParam(value = "keyword", required = false) String keyword) {
        System.out.println(String.format("type: %s | keyword:%s", type, keyword));
        return recordService.selectRecords(type, keyword);
    }

    @RequestMapping(value = "/start", method = RequestMethod.GET)
    public void startTask() {
        dataProcessor.startTimerTask();
    }

    @RequestMapping(value = "/stop", method = RequestMethod.GET)
    public void stopTask() {
        dataProcessor.stopTimerTask();
    }

    @RequestMapping(value = "/trigger", method = RequestMethod.GET)
    public void runTaskOnce() {
        dataProcessor.runTaskOnce();
    }
}
