package cn.edu.ecnu.stu.bookstore.controller;

import cn.edu.ecnu.stu.bookstore.component.AppException;
import cn.edu.ecnu.stu.bookstore.component.Constants;
import cn.edu.ecnu.stu.bookstore.component.Result;
import cn.edu.ecnu.stu.bookstore.service.impl.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @GetMapping("/title")
    public Result getBookByTitle(@RequestParam("title") String title,
                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        if(!StringUtils.hasText(title))
            throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
        return Result.success(bookService.getBookByTitle(title, null, pageNum, pageSize));
    }

    @GetMapping("/title_in_store")
    public Result getBookByTitleInStore(@RequestParam("title") String title,
                                 @RequestParam("storeId") String storeId,
                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        if(!StringUtils.hasText(title))
            throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
        return Result.success(bookService.getBookByTitle(title, storeId, pageNum, pageSize));
    }

    @GetMapping("/author")
    public Result getBookByAuthor(@RequestParam("author") String author,
                                 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        if(!StringUtils.hasText(author))
            throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
        return Result.success(bookService.getBookByAuthor(author, null, pageNum, pageSize));
    }

    @GetMapping("/author_in_store")
    public Result getBookByAuthorInStore(@RequestParam("author") String author,
                                        @RequestParam("storeId") String storeId,
                                        @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                        @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        if(!StringUtils.hasText(author))
            throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
        return Result.success(bookService.getBookByAuthor(author, storeId, pageNum, pageSize));
    }

    @GetMapping("/tag")
    public Result getBookByTag(@RequestParam("tag") String tag,
                                  @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        if(!StringUtils.hasText(tag))
            throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
        return Result.success(bookService.getBookByTag(tag, null, pageNum, pageSize));
    }

    @GetMapping("/tag_in_store")
    public Result getBookByTagInStore(@RequestParam("tag") String tag,
                                         @RequestParam("storeId") String storeId,
                                         @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        if(!StringUtils.hasText(tag))
            throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
        return Result.success(bookService.getBookByTag(tag, storeId, pageNum, pageSize));
    }

    @GetMapping("/content_in_store")
    public Result getBookByContentInStore(@RequestParam("content") String content,
                                      @RequestParam("storeId") String storeId,
                                      @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        if(!StringUtils.hasText(content))
            throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
        return Result.success(bookService.getBookByContent(content, storeId, pageNum, pageSize));
    }

    @GetMapping("/content")
    public Result getBookByContent(@RequestParam("content") String content,
                                          @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize) {
        if(!StringUtils.hasText(content))
            throw new AppException(Constants.CLIENT_ERROR, Constants.PARAMETER_ERROR_MESSAGE);
        return Result.success(bookService.getBookByContent(content, null, pageNum, pageSize));
    }
}
