package com.inspect.calender.controller;

import com.inspect.calender.domain.DailyTaskStatsDTO;
import com.inspect.calender.domain.MonthlyTaskStatsDTO;
import com.inspect.calender.service.CalenderService;
import com.inspect.calender.service.TaskCalendarGenerator;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/calender")
public class CalenderController {
    @Resource
    private CalenderService calenderService;
    @Resource
    private TaskCalendarGenerator taskCalendarGenerator;

    @ApiOperation("获取指定年份的月度信息")
    @GetMapping("/year")
    public List<MonthlyTaskStatsDTO> getMonthlyTaskStats(@RequestParam Integer year) {
        log.info("获取指定年份的信息, year: {}", year);
        if (year == null || year < 1900 || year > 2100) {
            throw new IllegalArgumentException("无效的年份");
        }
        List<MonthlyTaskStatsDTO> list = calenderService.getMonthlyStatsByYear(year);

        log.info("获取指定年份的信息成功, year: {}, list: {}", year, list);
        return list;
    }

    @ApiOperation("获取指定月份的日度信息")
    @GetMapping("/month")
    public List<DailyTaskStatsDTO> getDailyTaskStats(@RequestParam Integer year, @RequestParam Integer month) {
        log.info("获取指定年月份的信息, year: {}，month：{}", year, month);
        if (year == null || year < 1900 || year > 2100) {
            throw new IllegalArgumentException("无效的年份");
        }

        if (month == null || month < 1 || month > 12) {
            throw new IllegalArgumentException("无效的月份");
        }

        List<DailyTaskStatsDTO> list = calenderService.getDailyStatsByYearAndMonth(year, month);
        log.info("获取指定年月份的信息成功, year: {}, month: {}, list: {}", year,month, list);
        return list;
    }

    @GetMapping("/day")
    public List<DailyTaskStatsDTO> getDailyTaskStats(@RequestParam Integer year, @RequestParam Integer month, @RequestParam Integer day) {
        log.info("获取指定日期的信息, year: {}，month：{}，day：{}", year, month, day);
        if (year == null || year < 1900 || year > 2100) {
            throw new IllegalArgumentException("无效的年份");
        }

        if (month == null || month < 1 || month > 12) {
            throw new IllegalArgumentException("无效的月份");
        }

        if (day == null || day < 1 || day > 31) {
            throw new IllegalArgumentException("无效的日期");
        }

        List<DailyTaskStatsDTO> list = calenderService.getDailyStatsByYearAndMonthAndDay(year, month, day);
        log.info("获取指定日期的信息成功, year: {}, month: {}, list: {}", year,month, list);
        return list;
    }

    @GetMapping("/invalid")
    public String invalid() {
        taskCalendarGenerator.onTaskChange();
        return "ok";
    }
}
