package com.apobates.forum.thrones.controller;

import com.apobates.forum.core.ImageIOMeta;
import com.apobates.forum.core.entity.proxy.TopicCarouselReplica;
import com.apobates.forum.core.service.TopicCarouselService;
import com.apobates.forum.core.service.TopicCarouselSlideService;
import com.apobates.forum.thrones.vo.CarouselSlideItem;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 轮播图控制器
 * 
 * @author xiaofanku
 * @since 20200614
 */
@Controller
@RequestMapping(value = "/topic/carousel")
public class TopicCarouselController {
    @Autowired
    private TopicCarouselService topicCarouselService;
    @Autowired
    private TopicCarouselSlideService topicCarouselSlideService;
    @Autowired
    private ImageIOMeta imageIOMeta;
    
    @GetMapping(path = "/json")
    @ResponseBody
    public List<CarouselSlideItem> getCarouselPage(
            @RequestParam("title") String carouselTitle,
            HttpServletRequest request,
            Model model) {
        return topicCarouselSlideService.getAll(carouselTitle, imageIOMeta).map(CarouselSlideItem::new).collect(Collectors.toList());
    }
    
    @GetMapping(path = "/{volumes}-{board}.json")
    @ResponseBody
    public List<CarouselSlideItem> getBoardCarouselPage(
            @PathVariable("volumes") int boardGroupId,
            @PathVariable("board") long boardId,
            HttpServletRequest request,
            Model model) {
        Optional<TopicCarouselReplica> data = topicCarouselService.get(boardGroupId, boardId, imageIOMeta);
        if (data.isPresent()) {
            return data.get().getSlides().stream().map(CarouselSlideItem::new).collect(Collectors.toList());
        }
        return Collections.emptyList();
    }
}